We have two PDF documents that we try to merge into one; unfortunately, we run into a NullReferenceException. I have written a simple NUnit Test that reproduces the error, using these documents:
The order in which these are merged is important: when we change the order of the two documents, everything works just fine.
Here is the code for the NUnit test:
[Test]
public void PdfFileEditor_Concatenate_NullReference()
{
using var stream1 = File.OpenRead(@"C:\temp\A1.pdf");
using var stream2 = File.OpenRead(@"C:\temp\A2.pdf");
var document1 = new Document(stream1);
var document2 = new Document(stream2);
var editor = new PdfFileEditor();
editor.Concatenate([document2], document1);
}
Can you help us identify the root cause of this exception and suggest a workaround or fix?
The NullReferenceException you are encountering when using the PdfFileEditor.Concatenate(Document[], Document) method may be related to the specific content or structure of the PDF documents you are trying to merge. Since the issue arises only when merging in a specific order, it suggests that there might be something in the first document that is causing the exception when it is processed in conjunction with the second document.
Here are a few steps you can take to troubleshoot and potentially resolve the issue:
Check Document Integrity: Ensure that both PDF documents are not corrupted. You can try opening them in a PDF viewer to verify that they display correctly.
Update Aspose.PDF Library: Make sure you are using the latest version of the Aspose.PDF library, as updates often include bug fixes and improvements.
Use Try-Catch Block: Implement a try-catch block around your concatenation code to capture and log the exception details. This can provide more insight into what might be causing the issue.
Test with Different Documents: Try merging other PDF documents to see if the issue is specific to the two documents you are currently using. This can help isolate whether the problem is with the documents themselves or with the code.
Alternative Concatenation Methods: If the issue persists, consider using alternative methods for concatenation, such as merging streams instead of document objects. For example, you can read the PDF files as streams and use the Concatenate(Stream[], Stream) method.
Here’s an example of how to concatenate using streams:
using (var stream1 = File.OpenRead(@"C:\temp\A1.pdf"))
using (var stream2 = File.OpenRead(@"C:\temp\A2.pdf"))
using (var outputStream = File.Create(@"C:\temp\Merged.pdf"))
{
var pdfEditor = new PdfFileEditor();
pdfEditor.Concatenate(new Stream[] { stream1, stream2 }, outputStream);
}
This approach may bypass the issue you are facing with the Document objects.
If none of these steps resolve the issue, consider reaching out to Aspose support with the specific details of the exception and the documents involved for further assistance.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.