I'm starting to get some trouble with some pdf documents when I try to add margins to them... it doesn't happen all the time, in fact it almost never happens... When debugging I can't see anything wrong until it throws the exception "Object reference not set to an instance of an object" when I try to use the following code:
--
// Add a new page to the output document
PdfPage page = outputDocument.AddPage(CreatePageFromSize(printSize));
XGraphics _grPage = XGraphics.FromPdfPage(page);
// Select current page from the original document
orgForm.PageNumber = i + 1;
// Create a box on the output docuement page that fits the current original document page.
XRect box = new XRect(((printSize.Width - orgForm.Page.Width) / 2), ((printSize.Height - orgForm.Page.Height) / 2), orgForm.Page.Width.Point, orgForm.Page.Height.Point);
_grPage.DrawImage(orgForm, box);
--
this is the stack trace:
at PdfSharp.Pdf.PdfObject.ImportClosure(PdfImportedObjectTable importedObjectTable, PdfDocument owner, PdfObject externalObject)
at PdfSharp.Pdf.Advanced.PdfFormXObject..ctor(PdfDocument thisDocument, PdfImportedObjectTable importedObjectTable, XPdfForm form)
at PdfSharp.Pdf.Advanced.PdfFormXObjectTable.GetForm(XForm form)
at PdfSharp.Pdf.PdfPage.GetFormName(XForm form)
at PdfSharp.Drawing.Pdf.XGraphicsPdfRenderer.GetFormName(XForm form)
at PdfSharp.Drawing.Pdf.XGraphicsPdfRenderer.Realize(XImage image)
at PdfSharp.Drawing.Pdf.XGraphicsPdfRenderer.DrawImage(XImage image, Double x, Double y, Double width, Double height)
at PdfSharp.Drawing.XGraphics.DrawImage(XImage image, Double x, Double y, Double width, Double height)
at PdfSharp.Drawing.XGraphics.DrawImage(XImage image, XRect rect)
at vulkan.filemanager.Business.PdfLogic.AddCutMarginToDocument(String documentPath, PrintPaperFormat paperFormat) in C:\folder\file.cs:line 395
---
Could it be that pdf files from some pdf-converters lacks some attributes that pdfSharp is looking for? and which could it be? and can I add it before running it through my method?
I can't find anything in general with the pdf that could be wrong, and I haven't created it myself. All I know is in the application attribute under the pdf's document properties it says "Application: Writer"... so I don't know what application was used to create the file...
|