I’ve been creating PDFs by generating them in a headless browser using HTML and then saving to PDF. I then edit them using Aspose.PDF, such as joining multiple such PDFs together, removing or adding blank pages, and adding bookmarks.
This has worked fine while the code has been written in .Net 8.0, using that version of the Aspose.PDF library. To integrate with another application though, the code needs to work with .Net 4.7.2. I’ve tried copying all code across to an application using the Net 4.7.2 version of the Aspose.PDF library, or creating a Net Standard library using that version of Aspose.PDF, but both have the same problem - when loading the PDF, editing it, and then trying to save it, I get this error:
The type initializer for ‘Aspose.Pdf.XmpField’ threw an exception.
at :
at Aspose.Pdf.XmpField.get_Empty()
at #=zpXEIdHLgkPw3fKjOYyrucu6fwt4dd4T84WDqpK8=…ctor(#=z9hy_0p4Z4LUzlfl7t2QlpIYnQzI9_pDtPpOWAHo= #=zED89FWg=, Boolean #=zM2h5tgUxpdMv)
at #=zXypT$ksaYp24tjmzJ8ficb0Jo3Hu2GsXKk_7hcY=…ctor()
at #=zwQrdiNT4PE4SglHQd3fHO9XabPyvmfOm4VukXeA=…ctor()
at #=zrgh05faJJpAzO38O43LesOcAUoRKBz1wA_fOFsQ=.#=zKy33UWpywBGP()
at Aspose.Pdf.Document.get_Metadata()
at Aspose.Pdf.Document.get_IsPdfaCompliant()
at Aspose.Pdf.Document.#=z$BLJK39N59ot(Stream #=zbB05Mdw=, SaveOptions #=zVDJEEr4wyWHC)
at Aspose.Pdf.Document.#=z5M1wyJJj0io5(Stream #=zbB05Mdw=, SaveOptions #=zVDJEEr4wyWHC)
at Aspose.Pdf.Document.Save(Stream output)
at Paged.PdfFromPaged.CreatePdfFromHtml(String dmc, Int32 pageOffset, PdfFromPagedSettings settings, Boolean addToPdfList, Boolean separateOutFoldouts)
This has happened when trying to delete a page:
Document mainDoc = new Document(pdfPath);
mainDoc.Pages.Delete(numberOfPages);
using (MemoryStream ms = new MemoryStream())
{
mainDoc.Save(ms);
// Write the MemoryStream’s contents back to the original file
File.WriteAllBytes(pdfPath, ms.ToArray());
}
Previously I tried to delete the page using the PdfFileEditor.Delete function, the code shown is already an atttempt to get around the XmpField.get_Empty() exception, but it seems to happen no matter how I attempt to change the file using Aspose.PDF. If I avoid this piece of code altogether, I have the same exception when I later try to combine a set of PDFs together using PdfFileEditor. It doesn’t cause a problem with PDF files that Aspose has itself created, but seemingly with anything created from a browser, though I can’t tell what is different about them.