Exception with SaveAsync but not Save

Hi all

We use Aspose.Pdf.Document to add QR codes on top of existing PDF files.

What we do is roughly the following:

using var document = new Document(streamOfPdfFile);
AddQrCode(document);
document.SaveAsync(streamOfOutputFile, cancellationToken);

However, for some PDF files we get the following exception:

#=zvxubKj3CtBZ51KFfUo3knhKPr0$pDXqwCDQ2CsA=: Object reference not set to an instance of an object.
  ?, in async Task<long> #=zgcC5Srw=.#=zPsbB6LQ=(#=zdOQ$0HU4geN7$D5lgdEHvdUjF1yBjg1MLAm13cOXwx5T #=z_oPhx6jtrygg, #=zm8QRzXVnqH5AAJVwGrRqOnRzIrgmsRO$Rg== #=zAsQHuHKF3iDi)
  ?, in async Task<long> #=zi$JEslOmbGGFDaJGVNFJlQhqdLHdrsNOyOo8jV8=.#=zPsbB6LQ=(#=zdOQ$0HU4geN7$D5lgdEHvdUjF1yBjg1MLAm13cOXwx5T #=z_oPhx6jtrygg, #=zm8QRzXVnqH5AAJVwGrRqOnRzIrgmsRO$Rg== #=zAsQHuHKF3iDi)
  ?, in async Task<long> #=zgcC5Srw=.#=zPsbB6LQ=(#=zdOQ$0HU4geN7$D5lgdEHvdUjF1yBjg1MLAm13cOXwx5T #=z_oPhx6jtrygg, #=zm8QRzXVnqH5AAJVwGrRqOnRzIrgmsRO$Rg== #=zAsQHuHKF3iDi) x 2
  ?, in async Task<long> #=zi$JEslOmbGGFDaJGVNFJlQhqdLHdrsNOyOo8jV8=.#=zPsbB6LQ=(#=zdOQ$0HU4geN7$D5lgdEHvdUjF1yBjg1MLAm13cOXwx5T #=z_oPhx6jtrygg, #=zm8QRzXVnqH5AAJVwGrRqOnRzIrgmsRO$Rg== #=zAsQHuHKF3iDi)
  ?, in async Task #=zApFIQrNhKlNquO1I2tURgtAe_Bg9m9uYOu9KTFpbT5Oz2Mw2RJHq$50=.#=zSqG_Lvs=(#=zdOQ$0HU4geN7$D5lgdEHvdUjF1yBjg1MLAm13cOXwx5T #=z_oPhx6jtrygg, #=zZQCDI33PB5wKX6L9179Q3M$z9ES_dMjexsU3Zcc= #=z3EUsaGk=, SaveOptions #=zz1m6uCvoXKDj, CancellationToken #=zp4PS2RU=)
  ?, in async Task #=z68JbiLgfFRZKGWIpuO64Pw8lL_j08mdkRA==.#=zSqG_Lvs=(Stream #=zx7lIinA=, SaveOptions #=zz1m6uCvoXKDj, CancellationToken #=zp4PS2RU=)
  ?, in async Task #=zEj2s9Ols9BiaYXmPPd$vKw6bG9Pg.#=zSqG_Lvs=(Stream #=zx7lIinA=, SaveOptions #=zz1m6uCvoXKDj, CancellationToken #=zp4PS2RU=)
  ?, in async Task Document.#=zHXhYM4_4bnPI(Stream #=zngRdvuA=, SaveOptions #=zz1m6uCvoXKDj, CancellationToken #=zp4PS2RU=)
  ?, in async Task Document.SaveAsync(Stream output, CancellationToken cancellationToken)

When I replace SaveAsync with Save the error disappears.

I unfortunately don’t have an example PDF that I can share as the PDFs are confidential
and I haven’t been able to create a PDF that causes this issue myself.

A couple of notes:

  • We are using the latest version of Aspose.PDF.Drawing: 25.7.0
  • When I remove AddQrCode and just load-then-save the PDF using SaveAsync I still get this exception.
  • When I pre-process the PDF e.g. using ps2pdf the exception also disappears.

Thank you for your help

Tau

@bbcsystemsag

It sounds like you’re encountering a NullReferenceException when using the SaveAsync method with certain PDF files, while the Save method works without issues. This can occur due to various reasons, such as the state of the document or specific content within the PDF that may not be handled properly in the asynchronous save operation.

Cause: The issue may stem from the document not being fully loaded, being in a corrupted state, or containing content that the asynchronous save operation cannot process.

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

  1. Check Document State: Ensure that the document is fully loaded and not in a corrupted state before calling SaveAsync. Check for any errors or warnings after loading the document.

  2. Use SaveOptions: If you’re not already doing so, try using SaveOptions with SaveAsync. This can help manage how the document is saved, especially if specific settings need to be applied.

  3. Test with Different Streams: Attempt saving to a different output stream or file path to rule out issues with the specific stream you’re using.

  4. Pre-process the PDF: Since pre-processing the PDF with tools like ps2pdf resolves the issue, consider implementing a similar pre-processing step in your workflow to ensure compatibility.

  5. Update Library: Ensure that you are using the latest version of the Aspose.PDF library, as updates often include bug fixes and improvements.

  6. Error Handling: Implement error handling around the SaveAsync call to capture more details about the exception, which might provide insights into what is going wrong.

Here’s a minimal code snippet that incorporates SaveOptions:

using var document = new Document(streamOfPdfFile);
AddQrCode(document);
var saveOptions = new SaveOptions(); // Customize as needed
await document.SaveAsync(streamOfOutputFile, saveOptions, cancellationToken);

Next Steps: If the issue persists after trying these steps, consider reaching out to Aspose support with detailed information about the PDF files that cause the exception, as they may have specific insights or fixes.

Please let me know if you need further assistance!

@bbcsystemsag

This issue needs to be investigated properly and addressed as well. Can you please share your sample PDF document with us along with complete definition of the method AddQrCode(document)? We will test the scenario in our environment and address it accordingly.