We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

NullReferenceException when resize and concatenate documents

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/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);
      }
    } 

@Alexander_Winkelmeyer

Thank you for contacting support.

We have been able to notice the exception. However, it can be avoided by using below code in your environment.

...
var outDoc = new Document();
outDoc.Pages.Add(doc1.Pages);
outDoc.Pages.Add(doc2.Pages);
outDoc.Save(outStream);
...

We hope this will be helpful. Please feel free to contact us if you need any further assistance.

Thank you for your anwser @Farhan.Raza!

Sure I can use your code, but then I have to copy/merge outlines, fields, etc. by myself. Therefore I prever to use PdfFileEditor instead, which provide those features.

The mentioned exception looks like a bug. Will it be fixed?
best regard
Alex

@Alexander_Winkelmeyer

A ticket with ID PDFNET-45744 has been logged in our issue management system for further investigation and resolution. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

We are sorry for the inconvenience.

The issues you have found earlier (filed as PDFNET-45744) have been fixed in Aspose.PDF for .NET 19.5.