HI
I try to resize multiple documents and concatenate them together, but I get a NullReferenceException for one document.
sample documents:
https://www.netzausbau.de/SharedDocs/Downloads/DE/Vorhaben/BBPlG/03/Untersuchungsrahmen3A.pdf?__blob=publicationFile
https://www.netzausbau.de/SharedDocs/Downloads/DE/Publikationen/PCI-Verfahrenshandbuch.pdf;?__blob=publicationFile
sample code:
var inFileA = "TestFiles\\PCI-Verfahrenshandbuch.pdf";
var inFileB = "TestFiles\\Untersuchungsrahmen3A.pdf";
var outFile = "TestFiles\\Test.pdf";
var pdfFileEditor = new PdfFileEditor
{
AllowConcatenateExceptions = true,
CopyLogicalStructure = true,
CopyOutlines = true,
KeepFieldsUnique = true,
MergeDuplicateOutlines = true,
OptimizeSize = false,
IncrementalUpdates = true
};
using (var stream1 = File.Open(inFileA, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (var stream2 = File.Open(inFileA, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (var outStream = File.Open(outFile, FileMode.Create))
{
var doc1 = new Document(stream1);
ResizePdf(doc1, global::Aspose.Pdf.PageSize.A4);
var doc2 = new Document(stream2);
ResizePdf(doc2, global::Aspose.Pdf.PageSize.A4);
var outDoc = new Document();
pdfFileEditor.Concatenate(new[] { doc1 }, outDoc);
pdfFileEditor.Concatenate(new[] { doc2 }, outDoc); // Exception
outDoc.Save(outStream);
}
}
}
private void ResizePdf(Document doc, global::Aspose.Pdf.PageSize pageSize)
{
using (var pageEditor = new PdfPageEditor())
{
pageEditor.BindPdf(doc);
var pageCount = pageEditor.GetPages();
for (var i = 1; i <= pageCount; i++)
{
pageEditor.ProcessPages = new[] { i };
var sourcePageSize = pageEditor.GetPageSize(i);
var sourceRotation = pageEditor.GetPageRotation(i);
if (sourceRotation == 90 || sourceRotation == 270)
pageEditor.PageSize = new global::Aspose.Pdf.PageSize(pageSize.Height, pageSize.Width);
else
pageEditor.PageSize = new global::Aspose.Pdf.PageSize(pageSize.Width, pageSize.Height);
pageEditor.VerticalAlignmentType = VerticalAlignment.Center;
pageEditor.HorizontalAlignment = HorizontalAlignment.Center;
float zoom;
if (pageEditor.Rotation == 0)
zoom = Math.Min(pageEditor.PageSize.Width / sourcePageSize.Width, pageEditor.PageSize.Height / sourcePageSize.Height);
else
zoom = Math.Min(pageEditor.PageSize.Width / sourcePageSize.Height, pageEditor.PageSize.Height / sourcePageSize.Width);
pageEditor.Zoom = zoom;
pageEditor.ApplyChanges();
}
pageEditor.BindPdf((Document)null);
}
}