Hello,
When I use ASPOSE.PDF to compress a PDF/A document that does not contain an attachment, I receive a NullReferenceException error when I try to save the document. This error did not occur in previous versions.
factur-x.pdf (101,9 Ko)
It sounds like you’re encountering a NullReferenceException
when trying to compress a PDF/A document without attachments using Aspose.PDF version 25.7. This issue is likely related to changes in the library’s handling of PDF/A documents in this version.
Cause: The NullReferenceException
typically indicates that you’re trying to access a member of an object that hasn’t been instantiated, which may be due to improper initialization of objects in your code.
Solution:
-
Check for Updates: Ensure that you are using the latest version of Aspose.PDF. Bugs are often fixed in newer releases, so updating to the latest version may resolve the issue.
-
Review Your Code: Double-check your code to ensure that all objects are properly initialized before use.
-
Test with Different Documents: Try compressing a different PDF/A document that contains attachments to see if the issue persists. This can help determine if the problem is specific to the document you’re currently using.
-
Error Handling: Implement error handling in your code to catch exceptions and log more details about the state of your application when the error occurs. This can provide insights into what might be going wrong.
-
Contact Support: If the issue persists after trying the above steps, reach out to Aspose support with details about your implementation and the specific document causing the issue. They may have more insights or a workaround.
Next Steps: Here’s a minimal code snippet to help you get started with compressing a PDF/A document:
using Aspose.Pdf;
Document pdfDocument = new Document("path/to/your/document.pdf");
PdfSaveOptions saveOptions = new PdfSaveOptions
{
Compliance = PdfCompliance.PdfA_1_B
};
// Save the document with compression
pdfDocument.Save("path/to/save/compressed_document.pdf", saveOptions);
Please try these steps and let me know if you need further assistance!
The error occurs when I update Aspose.PDF to version 25.7. The same code works fine with PDF/A files containing an attachment.
Would you please share the code snippet that you are using in your environment? We will test the scenario in our environment and address it accordingly. Please also share what was the previous version with which error was not happening?
Here’s the code I use to compress PDFs:
OptimizationOptions options = new()
{
AllowReusePageContent = true,
CompressObjects = false,
LinkDuplicateStreams = true,
RemovePrivateInfo = false,
RemoveUnusedObjects = true,
RemoveUnusedStreams = true,
SubsetFonts = false,
UnembedFonts = false,
};
options.ImageCompressionOptions.CompressImages = true;
options.ImageCompressionOptions.ImageQuality = 40;
options.ImageCompressionOptions.ResizeImages = false;
options.ImageCompressionOptions.MaxResolution = 300;
using Document currentPdf = new(pdfStream, true);
currentPdf.OptimizeResources(options);
currentPdf.OptimizeSize = true;
currentPdf.Optimize();
MemoryStream compressedPdfStream = new();
await currentPdf.SaveAsync(compressedPdfStream, cancellationToken);
This code worked until version 25.6.1.
It’s not just PDF/A documents, I’m adding another document that causes the same exception.
sample-pdf-with-images.pdf (3,8 Mo)
We tested with 25.7 version in our environment and could not replicate the Exception. Attached are the generated outputs for your kind reference:
output_factur-x.pdf (101.4 KB)
output_sample-pdf-with-images.pdf (918.6 KB)
Can you please try with the latest version and let us know in case you still notice the issue.
I tried again with version 25.7 and can confirm that I still get an error when using the asynchronous SaveAsync() function.
However, I don’t get any error when I use the synchronous Save() function.
I get the same error with these files when processing them in ways other than compression using the asynchronous function.
We need to further investigate this issue. Would you kindly share complete Stack Trace information and Exception message with us? We will generate a ticket in our issue tracking system and share the ID with you.
Here is the exception message: System.NullReferenceException : ‘Object reference not set to an instance of an object.’
And here is the call stack when this happens:
Aspose.PDF.dll!#=zlR692R0BgSkPSTZCk4yGf2GRiKTVJo_W1w==.#=zM3YYMpY=.#=zVIkP65nyYN9qn$VrRg==.MoveNext() | ||
---|---|---|
Aspose.PDF.dll!#=zK1IrDqHVhaiUaeABLGqtqK1dEzPmNJGl5Nn$O1A=.#=zMmNkw53Zo8Whm37agw==.MoveNext() | ||
Aspose.PDF.dll!#=z2qKtBjGsO19myExiMrV4zfsFLn690mTahg==.#=zM3YYMpY=.#=zpAp1kEguzwzkL8Vnsg==.MoveNext() | ||
[Resume Async method] | ||
System.Private.CoreLib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.AsyncStateMachineBox<#=z2qKtBjGsO19myExiMrV4zfsFLn690mTahg==.#=zM3YYMpY=.#=zpAp1kEguzwzkL8Vnsg==>.ExecutionContextCallback(object s) Line 131 | ||
System.Private.CoreLib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Line 138 | ||
System.Private.CoreLib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.AsyncStateMachineBox<#=z2qKtBjGsO19myExiMrV4zfsFLn690mTahg==.#=zM3YYMpY=.#=zpAp1kEguzwzkL8Vnsg==>.MoveNext(System.Threading.Thread threadPoolThread) Line 163 | ||
System.Private.CoreLib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.AsyncStateMachineBox<#=z2qKtBjGsO19myExiMrV4zfsFLn690mTahg==.#=zM3YYMpY=.#=zpAp1kEguzwzkL8Vnsg==>.MoveNext() Line 146 | ||
System.Private.CoreLib.dll!System.Runtime.CompilerServices.TaskAwaiter.OutputWaitEtwEvents.AnonymousMethod__12_0(System.Action innerContinuation, System.Threading.Tasks.Task innerTask) Line 140 | ||
System.Private.CoreLib.dll!System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining) Line 162 | ||
System.Private.CoreLib.dll!System.Threading.Tasks.Task.RunContinuations(object continuationObject) Line 2854 | ||
System.Private.CoreLib.dll!System.Threading.Tasks.Task.TrySetResult(long result) Line 171 | ||
System.Private.CoreLib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.SetExistingTaskResult(System.Threading.Tasks.Task task, long result) Line 389 | ||
System.Private.CoreLib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.SetResult(long result) Line 379 | ||
[Done] Aspose.PDF.dll!#=zDPbghukI1903OwrwyEAAvIT8coMV33_iMB27lt0=.#=zbmJRty8yUW3Uz4MgSGaTaKQ=.MoveNext() | ||
System.Private.CoreLib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.AsyncStateMachineBox<#=zDPbghukI1903OwrwyEAAvIT8coMV33_iMB27lt0=.#=zbmJRty8yUW3Uz4MgSGaTaKQ=>.ExecutionContextCallback(object s) Line 131 | ||
System.Private.CoreLib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Line 138 | ||
System.Private.CoreLib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.AsyncStateMachineBox<#=zDPbghukI1903OwrwyEAAvIT8coMV33_iMB27lt0=.#=zbmJRty8yUW3Uz4MgSGaTaKQ=>.MoveNext(System.Threading.Thread threadPoolThread) Line 163 | ||
System.Private.CoreLib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.AsyncStateMachineBox<#=zDPbghukI1903OwrwyEAAvIT8coMV33_iMB27lt0=.#=zbmJRty8yUW3Uz4MgSGaTaKQ=>.MoveNext() Line 146 | ||
System.Private.CoreLib.dll!System.Runtime.CompilerServices.TaskAwaiter.OutputWaitEtwEvents.AnonymousMethod__12_0(System.Action innerContinuation, System.Threading.Tasks.Task innerTask) Line 140 | ||
System.Private.CoreLib.dll!System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining) Line 162 | ||
System.Private.CoreLib.dll!System.Threading.Tasks.Task.RunContinuations(object continuationObject) Line 2854 | ||
System.Private.CoreLib.dll!System.Threading.Tasks.Task.FinishSlow(bool userDelegateExecute) Line 1971 | ||
System.Private.CoreLib.dll!System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref System.Threading.Tasks.Task currentTaskSlot, System.Threading.Thread threadPoolThread) Line 2205 | ||
System.Private.CoreLib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch() Line 740 | ||
System.Private.CoreLib.dll!System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart() Line 1338 |
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): PDFNET-60371
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.