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!