PDFsharp & MigraDoc Forum

PDFsharp - A .NET library for processing PDF & MigraDoc - Creating documents on the fly
It is currently Thu Oct 30, 2025 7:11 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules

Also see our new Tailored Support & Services site.



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Thu Jan 23, 2014 7:36 pm 
Offline

Joined: Thu Jan 23, 2014 7:21 pm
Posts: 2
Hi,

I have encountered the following error when running a script that has the PDFSharp 1.32 assembly DLL in the bin folder:

Code:
System.InvalidOperationException: There was an error reflecting type 'PdfSharp.Pdf.PdfDocument'. ---> System.InvalidOperationException: Cannot serialize member 'PdfSharp.Pdf.PdfObject.Reference' of type 'PdfSharp.Pdf.Advanced.PdfReference', see inner exception for more details. ---> System.InvalidOperationException: PdfSharp.Pdf.Advanced.PdfReference cannot be serialized because it does not have a parameterless constructor.
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.StructModel.CheckSupportedMember(TypeDesc typeDesc, MemberInfo member, Type type)
   at System.Xml.Serialization.StructModel.GetPropertyModel(PropertyInfo propertyInfo)
   at System.Xml.Serialization.StructModel.GetFieldModel(MemberInfo memberInfo)
   at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter)
   at System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter)
   at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter)
   at System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter)
   at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)
   at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, RecursionLimiter limiter)
   at System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel model, XmlRootAttribute root, String defaultNamespace, RecursionLimiter limiter)
   at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace)
   at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root)
   at System.Web.Services.Protocols.XmlReturn.GetInitializers(LogicalMethodInfo[] methodInfos)
   at System.Web.Services.Protocols.XmlReturnWriter.GetInitializers(LogicalMethodInfo[] methodInfos)
   at System.Web.Services.Protocols.MimeFormatter.GetInitializers(Type type, LogicalMethodInfo[] methodInfos)
   at System.Web.Services.Protocols.HttpServerType..ctor(Type type)
   at System.Web.Services.Protocols.HttpServerProtocol.Initialize()
   at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response)
   at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)


A few details that might be helpful:

[*] I am running on ASP.NET 2.0, and no I cannot upgrade due to requirements outside of my control
[*] This is an *.asmx web using the Newtonsoft JSON serializer and references some legacy DLLs
[*] I am not a .NET ninja so I could be missing something obvious

Everything has been functioning nicely until I added the code for PDF generation. Here is the main section of the code I am using:

Code:
using PdfSharp.Pdf;
using PdfSharp.Pdf.Advanced;
using PdfSharp.Drawing;


Note: I added the reference to PdfSharp.Pdf.Advanced based on the exception thrown. I also added a reference to PdfSharp alone which had no effect.

Code:
        MemoryStream stream = new MemoryStream();
        PdfDocument document = new PdfDocument();
        XImage image = XImage.FromGdiPlusImage(data);
        PdfPage imagePage = new PdfPage();
        imagePage.Height = image.PointHeight;
        imagePage.Width = image.PointWidth;
        document.Pages.Add(imagePage);
        int pageCount = 1;
        XGraphics graphics = XGraphics.FromPdfPage(document.Pages[pageCount]);
        graphics.DrawImage(image, 0, 0);
        document.Save(stream);

        Context.Response.Buffer = true;
        Context.Response.Clear();
        Context.Response.ContentType = "application/pdf";
        Context.Response.AddHeader("content-disposition", "attachment; filename=" + name);
        Context.Response.BinaryWrite(stream.ToArray());
        stream.Close();
        Context.Response.Flush();
        Context.Response.End();


The data object is a valid System.Drawing.Image object. All other variables/parameters have been tested as working that outside of the scope of the PdfSharp functions.

Thanks for help!


Top
 Profile  
Reply with quote  
PostPosted: Mon Jan 27, 2014 10:27 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3136
Location: Cologne, Germany
Hi!

I'm not an ASP.NET guru, but could it be that the code is trying to serialize the "document" object? Maybe as part of the VIEWSTATE?

You could try "using (PdfDocument document = new PdfDocument()){" with closing braces after "document.Save".

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Jan 27, 2014 7:42 pm 
Offline

Joined: Thu Jan 23, 2014 7:21 pm
Posts: 2
Hi Thomas,

Thanks for the reply; I did actually figure this one out, and it was as you explained. I was trying to return a PdfDocument object through a WebMethod enabled object (even though I was overriding the output with Context.Response.BinaryWrite/End) which was making the native XML Serializer object crap it's pants. I just returned a bool object instead and that fixed it :)

Thanks again!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 49 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group