NullReferenceException when concatenating files

Hi All,

We have noticed issue when concatenating pdf files using Aspose.PDF version 24.7.0.0. This problem has been identified on our machines as well as in our clients’ production environments.

When calling Concatenate(Stream[] inputStream, Stream outputStream) for any file and the
Biocirc høringssvar til kommuneplan 2025 - original.pdf we get the following exception:

Exception occurred. Details: System.NullReferenceException: Object reference not set to an instance of an object. at Aspose.Pdf.Facades.PdfFileEditor.#=z45WyG7iz4Uwe(Document #=zfS$Zt1Q=, Document #=zS7x67v0=, #=zs4wCgDGbOmDHFi90YVdBXIj_IXqW #=zEx0jVQ8Yxduy, #=zH7rwp_Ou2rCYqZvt4U_Ld3bJwz2Upc_v6w== #=znaB425O1UqQe) at Aspose.Pdf.Facades.PdfFileEditor.#=zJXLRSAiGXko7(Document[] #=zODXQChw=, Document& #=zS7x67v0=) at Aspose.Pdf.Facades.PdfFileEditor.Concatenate(Stream[] inputStream, Stream outputStream)

I am attaching sample code and documents to reproduce the issue.

Could you help us resolve this issue?
Thanks in advance,
Marcin
Attachments.zip (5.6 MB)

@skrz

It seems you are encountering a NullReferenceException when trying to concatenate PDF files using Aspose.PDF version 24.7.0.0. This issue has been reported by other users as well, and it may be related to the specific PDF files you are working with or the version of the library.

Here are some steps you can take to troubleshoot and potentially resolve the issue:

  1. Update Aspose.PDF: If possible, consider updating to a newer version of Aspose.PDF. There have been fixes related to PDF concatenation in later versions, and upgrading might resolve the issue you are facing.

  2. Check PDF File Integrity: Ensure that the PDF files you are trying to concatenate are not corrupted. You can try opening them individually to verify their integrity.

  3. Use Sample Code: Here is a sample code snippet that demonstrates how to concatenate PDF files using streams. Make sure your implementation aligns with this example:

    using System.IO;
    using Aspose.Pdf.Facades;
    
    public class ConcatenatePDFs
    {
        public static void Run()
        {
            // Create PdfFileEditor object
            PdfFileEditor pdfEditor = new PdfFileEditor();
    
            // Output stream
            using (FileStream outputStream = new FileStream("output.pdf", FileMode.Create))
            {
                // Array of input streams
                using (FileStream inputStream1 = new FileStream("input1.pdf", FileMode.Open),
                                  inputStream2 = new FileStream("input2.pdf", FileMode.Open))
                {
                    // Concatenate files
                    pdfEditor.Concatenate(new Stream[] { inputStream1, inputStream2 }, outputStream);
                }
            }
        }
    }
    
  4. Error Handling: Implement error handling in your code to catch exceptions and log detailed information about the files being processed. This can help identify if a specific file is causing the issue.

  5. Review Similar Issues: You may want to check other forum posts related to PDF concatenation issues. Users have reported similar problems, and solutions or workarounds may have been shared.

If the problem persists after trying these steps, please provide additional details about the specific PDF files you are using, and any other relevant information that could help diagnose the issue further.

Sources:
[1]: Error concatenating pdf’s with invalid signature
[2]: How to Merge PDF using C#|Aspose.PDF for .NET - Aspose Documentation

Our implementation aligns with the sample code provided. I have also tested with the sample code and our files and issue is occurring. If it has been fixed in newer versions, please provide in which it the fix has been implemented.

@skrz

We tested using 25.2 version of the API and could not replicate the issue. Would you please try using it and let us know if you face any issues?
output.pdf (5.9 MB)

I can confirm that it works for that version, you can close this thread.

@skrz

Thanks for the feedback. Please feel free to create a new topic in case you face any issues.

Hello,
We have updated our solution to use Aspose.PDF 25.3 version, however the error still occurs. I did some testing and it looks like setting CopyLogicalStructure to True is what causes the error. Here is the full snippet:

		string[] filePaths;
		var dir = @"C:\PdfMerge\e93fc811-56c1-4044-bbec-791557255424";
		filePaths = Directory.GetFiles(dir);

		var outputFile = Path.Combine(dir, "output.pdf");

		var pdfFileEditor = new PdfFileEditor()
		{
			MergeDuplicateLayers = false,
			CopyOutlines = true,
			CopyLogicalStructure = true,
		};

		pdfFileEditor.Concatenate(filePaths, outputFile);

Could you please help us with this issue?

@skrz

Is it happening with the same files that you have shared previously? Did you try with 24.4 version as well? Please share the requested information. We will further proceed to assist you accordingly.

Hello,
Yes, it is happening for the same file. No, we haven’t checked any other versions beside 24.7 and 25.3.

@skrz

Below is the output that we generated in our environment using 25.4 version of the API and we could not notice any issues during testing:
output.pdf (5.9 MB)

Can you please try with 25.4 version and let us know if issue still persists.