PDFsharp & MigraDoc Forum
https://forum.pdfsharp.net/

There was an error reflecting type PdfSharp.Pdf.PdfDocument
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2714
Page 1 of 1

Author:  andrewodri [ Thu Jan 23, 2014 7:36 pm ]
Post subject:  There was an error reflecting type PdfSharp.Pdf.PdfDocument

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!

Author:  Thomas Hoevel [ Mon Jan 27, 2014 10:27 am ]
Post subject:  Re: There was an error reflecting type PdfSharp.Pdf.PdfDocum

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".

Author:  andrewodri [ Mon Jan 27, 2014 7:42 pm ]
Post subject:  Re: There was an error reflecting type PdfSharp.Pdf.PdfDocum

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!

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/