Error Optimizing PDF File

The System.NullReferenceException: ‘Object reference not set to an instance of an object.’ error occurs while optimizing a PDF file created using Aspose.PDF’s HTML-to-PDF functionality. However, due to compliance restrictions, the PDF file cannot be shared.

Aspose.PDF.Drawing Version: 24.10

Code

 Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(file);
 OptimizationOptions optimizationOptions = new OptimizationOptions();
 optimizationOptions.UnembedFonts = true;
 optimizationOptions.ImageCompressionOptions.CompressImages = true; 
 optimizationOptions.ImageCompressionOptions.ImageQuality = 50;
 optimizationOptions.RemoveUnusedObjects = true;
 optimizationOptions.RemoveUnusedStreams = false;
 optimizationOptions.ImageCompressionOptions.ResizeImages = true;
 optimizationOptions.ImageCompressionOptions.Version = ImageCompressionVersion.Mixed;
 pdfDocument.OptimizeSize = true;
 pdfDocument.OptimizeResources(optimizationOptions);
 pdfDocument.Save(outputPdfPath);

Stack trace

   at Aspose.Pdf.Page.#=ztaS8EQWQ4gKV$PZdrg==(Dictionary`2 #=zsquxkoc=, #=znb5qG7DHjCAgviA1ShxWdV75jIeqanGvPg== #=zHgGorAsjOSv2)
   at #=zAgwqFH0WaIElYnGpvv6w9j1cXDpu.#=zWp37Id0=(OptimizationOptions #=zKhZc_d0=)
   at Aspose.Pdf.Document.OptimizeResources(OptimizationOptions strategy)
   at Program.CompressPdfs() in C:\\Users\\ismai\\source\\repos\\PDFCompressor\\PDFCompressor\\Program.cs:line 34

@ibohri

The System.NullReferenceException you are encountering while optimizing a PDF file using Aspose.PDF’s HTML-to-PDF functionality may be due to an issue with the optimization options or the state of the PDF document itself. Here are some steps you can take to troubleshoot and potentially resolve the issue:

  1. Check the PDF Document: Ensure that the PDF document you are trying to optimize is not corrupted or empty. You can try opening it in a PDF viewer to confirm that it displays correctly.

  2. Review Optimization Options: The optimization options you are using seem to be set correctly, but you might want to simplify them to identify if a specific option is causing the issue. For example, try removing or commenting out some options temporarily to see if the error persists.

  3. Use Default Optimization: You can try using the default optimization settings to see if that resolves the issue. This can help determine if the problem lies within the specific options you are setting.

  4. Update Aspose.PDF Library: Ensure that you are using the latest version of the Aspose.PDF library. Sometimes, bugs are fixed in newer releases, and updating might resolve the issue.

  5. Error Handling: Implement error handling in your code to catch exceptions and log more details about the state of the application when the error occurs. This can provide more insight into what might be going wrong.

Here is a simplified version of your code that you can try:

Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(file);
OptimizationOptions optimizationOptions = new OptimizationOptions();
optimizationOptions.UnembedFonts = true;
optimizationOptions.ImageCompressionOptions.CompressImages = true; 
optimizationOptions.ImageCompressionOptions.ImageQuality = 50;
optimizationOptions.RemoveUnusedObjects = true;

try
{
    pdfDocument.OptimizeResources(optimizationOptions);
    pdfDocument.Save(outputPdfPath);
}
catch (Exception ex)
{
    Console.WriteLine("An error occurred: " + ex.Message);
}

By simplifying the optimization options and adding error handling, you may be able to identify the root cause of the NullReferenceException. If the problem persists, consider reaching out to Aspose support with detailed information about the PDF document and the error stack trace for further assistance.

For more information on optimizing PDF files using Aspose.PDF, you can refer to the official documentation on optimizing file size and resources in PDF documents.

Sources:
[1]: Optimize, Compress or Reduce PDF Size in C# - Aspose Documentation

This code also produces the same error.

@ibohri
Can you provide another example of document or send privately the original one that reproduces this issue?
the investigation would be much harder without recreation of scenario
Also, does issue reproduce on Aspose Drawing 24.11?