Hi,
I’m upgrading Aspose library in a solution based on .NET Framework 4.7.2.
The version Aspose.PDF version 24.3.0 compiles properly, but I’m having problems when I work with PDF with layers.
I added a validation to prevent uploading PDF with layers but I’m getting an error when it’s trying to read the attribute “Layers” for each page. It just keeps reading without any response.
This is the code snippet:
var errors = new List<string>();
string fileExtension = Path.GetExtension(filePath);
if (fileExtension.ToLower() == ".pdf")
{
using (PdfFileInfo pdfFile = new PdfFileInfo(filePath))
{
if (pdfFile.IsEncrypted)
{
errors.Add(ValidationMessage.Keys.DocumentationInfoModelKeys.PDF_PASSWORD_PROTECTED);
}
else
{
Aspose.Pdf.Document document = new Aspose.Pdf.Document(filePath);
foreach (Page page in document.Pages)
{
List<Aspose.Pdf.Layer> layers = page.Layers;
if (layers != null && layers.Count > 0)
{
errors.Add(ValidationMessage.Keys.DocumentationInfoModelKeys.PDF_CONTAINS_LAYERS);
}
}
document.Dispose();
}
}
}
It sounds like you’re encountering an issue with reading the “Layers” attribute from PDF pages in Aspose.PDF version 24.3.0. This can occur due to complex PDF structures or compatibility issues with that version.
Cause: The problem may stem from the specific structure of the PDF files you are using or potential bugs in the version of Aspose.PDF you have.
Solution: Here’s a step-by-step approach to diagnose and resolve the issue:
Check PDF Structure: Ensure that the PDFs you are testing with are not corrupted and are structured correctly. Open them in a PDF viewer to verify their integrity.
Update Aspose.PDF: Upgrade to a newer version of Aspose.PDF if possible. Newer versions may contain bug fixes or improvements related to handling layers.
Use Try-Catch: Implement error handling around the code that accesses the layers. This will help catch any exceptions thrown when accessing the layers.
Test with Different PDFs: Use different PDF files to determine if the issue is specific to certain documents. This will help isolate whether the problem lies with the library or the specific PDFs.
Contact Support: If the issue persists, reach out to Aspose support with details about the PDFs and the specific error messages you are encountering. They can provide insights or solutions tailored to your situation.
Next Steps: Follow the steps outlined above to troubleshoot the issue. If you continue to experience problems, gather the necessary information and contact Aspose support for further assistance.
Please let me know if this helps or if you have any further questions!
Please provide sample PDF documents to evaluate your issue precisely on our end. Moreover, I am moving your thread to respective category where one of our colleagues from Aspose.PDF team will assist you soon.
We tested both of your documents using the latest API version in our environment and were unable to reproduce the issue you described. Could you please try using version 25.7 and let us know if you encounter any problems?
There is no Aspose.PDF DLL available for framework 4.0.
Only for framework 4.8 and 2.0. Neither of those versions compile in our solution.
That’s why we had to use the version 24.3.0.
However, please note that these framework version(s) have been outdated and sooner or later their support will also be discontinued. Therefore, you can please plan to upgrade your project to the higher and long-term supported framework versions.
This DLL resolved the previous issues with the two PDFs. However, I’m having another issue.
I’m getting this error after uploading several PDFs:
PdfFileInfo is not initialized. Use constructors with parameters or properties for initialization. (File: C:\LSAS_Data\TempResources\638899252055786883.PDF) (Reason: The process cannot access the file ‘C:\LSAS_Data\TempResources\638899252055786883.PDF’ because it is being used by another process.)
I’ve added the line below for testing purposes because minimize the “PdfFileInfo is not initialized” error:
System.Threading.Thread.Sleep(1000);
Without it, the error occurs after adding the second file onward.
Even though, I’m still getting that error from time to time after uploading several PDF files.
This is the code snippet:
var errors = new List<string>();
string fileExtension = Path.GetExtension(filePath);
if (fileExtension.ToLower() == ".pdf")
{
System.Threading.Thread.Sleep(1000);
using (PdfFileInfo pdfFile = new PdfFileInfo(filePath))
{
if (pdfFile.IsEncrypted)
{
errors.Add(ValidationMessage.Keys.DocumentationInfoModelKeys.PDF_PASSWORD_PROTECTED);
}
else
{
Aspose.Pdf.Document document = new Aspose.Pdf.Document(filePath);
foreach (Page page in document.Pages)
{
List<Aspose.Pdf.Layer> layers = page.Layers;
if (layers != null && layers.Count > 0)
{
errors.Add(ValidationMessage.Keys.DocumentationInfoModelKeys.PDF_CONTAINS_LAYERS);
break;
}
}
document.Dispose();
}
}
}
We upgraded the Aspose library to v25.7 recently, and deployed a new release of our application with this upgrade to PROD last Friday. Now, the users are reporting several issues uploading PDF files.
An item with the same key has already been added." source=“mscorlib” detail="System.ApplicationException: An item with the same key has already been added. StackTrace: at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary2.Insert(TKey key, TValue value, Boolean add)
 at #=z9W8OEM$p8$g7694whr8T0jRxJwvdctJ1R$EeZ3ITou1EZTgHRw==.#=zkksCAzmrsk_H(#=zF7$jGz6kYTSr2nSyKQalEmRNcx7aLt08LC2n_xNOhF9u #=z_Ip$DbYUI972)
 at #=ziN9ESXTDEHqQ9i28HzgngITTo0InGFGHbGPsVxGPvQoB.#=ziM4g4g1Qaq0P(#=zF7$jGz6kYTSr2nSyKQalEmRNcx7aLt08LC2n_xNOhF9u #=zX9TZRmo=, Int64 #=zwCgSo68=, Nullable1& #=zeWA7mAE=)
at #=ziN9ESXTDEHqQ9i28HzgngITTo0InGFGHbGPsVxGPvQoB.#=zAdIg0khetjot(List1 #=z4SqYp6Y=, #=zF7$jGz6kYTSr2nSyKQalEmRNcx7aLt08LC2n_xNOhF9u #=zX9TZRmo=, Int64 #=zwCgSo68=)
 at #=ziN9ESXTDEHqQ9i28HzgngITTo04SqYp6Y=, List1 #=zT_TJ_pEutgW6)
at #=ziN9ESXTDEHqQ9i28HzgngITTo0InGFGHbGPsVxGPvQoB.#=zfSI77_wxPv0V(#=z_HFh1h9N21sQyH2FimgXElZP1Nonq7KQ_A== #=zS9hZG74=, #=zg4XhJOHcYaratJ4SSOf4w5XWCbJR9z_uxWwxf17D0AWA #=zW_iSz$GlvlBB5M7fLw==, Boolean #=zME0cRrIT6Zas, Boolean #=z7ygylo2byPvH)
at #=ziN9ESXTDEHqQ9i28HzgngITTo0InGFGHbGPsVxGPvQoB.#=zfSI77_wxPv0V(#=z_HFh1h9N21sQyH2FimgXElZP1Nonq7KQ_A== #=zS9hZG74=, #=zg4XhJOHcYaratInGFGHbGPsVxGPvQoB.#=zCKB5i7bs0YurnjOhLw==(Stream #=zHKyIkptxFHabpfL$3Q==, Boolean #=z7ygylo2byPvH, List`1 #=zJ4SSOf4w5XWCbJR9z_uxWwxf17D0AWA #=zW_iSz$GlvlBB5M7fLw==)
at Aspose.Pdf.OperatorCollection.#=zvq5X7UM6S3jG()
at Aspose.Pdf.OperatorCollection.#=zYCo173xa$Z8C()
at Aspose.Pdf.OperatorCollection.get_Count()
at Aspose.Pdf.OperatorCollection.#=zzU1PmlA=.MoveNext()
at Aspose.Pdf.Page.get_Layers()
When I check the logs, there are a lot of entries indicating issues with the product Aspose.PDF
The error in our log after trying to upload this PDF is:
An item with the same key has already been added." source=“mscorlib” detail="System.ApplicationException: An item with the same key has already been added. StackTrace: at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary2.Insert(TKey key, TValue value, Boolean add)
 at #=z9W8OEM$p8$g7694whr8T0jRxJwvdctJ1R$EeZ3ITou1EZTgHRw==.#=zkksCAzmrsk_H(#=zF7$jGz6kYTSr2nSyKQalEmRNcx7aLt08LC2n_xNOhF9u #=z_Ip$DbYUI972)
 at #=ziN9ESXTDEHqQ9i28HzgngITTo0InGFGHbGPsVxGPvQoB.#=ziM4g4g1Qaq0P(#=zF7$jGz6kYTSr2nSyKQalEmRNcx7aLt08LC2n_xNOhF9u #=zX9TZRmo=, Int64 #=zwCgSo68=, Nullable1& #=zeWA7mAE=)
at #=ziN9ESXTDEHqQ9i28HzgngITTo0InGFGHbGPsVxGPvQoB.#=zAdIg0khetjot(List1 #=z4SqYp6Y=, #=zF7$jGz6kYTSr2nSyKQalEmRNcx7aLt08LC2n_xNOhF9u #=zX9TZRmo=, Int64 #=zwCgSo68=)
 at #=ziN9ESXTDEHqQ9i28HzgngITTo04SqYp6Y=, List1 #=zT_TJ_pEutgW6)
at #=ziN9ESXTDEHqQ9i28HzgngITTo0InGFGHbGPsVxGPvQoB.#=zfSI77_wxPv0V(#=z_HFh1h9N21sQyH2FimgXElZP1Nonq7KQ_A== #=zS9hZG74=, #=zg4XhJOHcYaratJ4SSOf4w5XWCbJR9z_uxWwxf17D0AWA #=zW_iSz$GlvlBB5M7fLw==, Boolean #=zME0cRrIT6Zas, Boolean #=z7ygylo2byPvH)
at #=ziN9ESXTDEHqQ9i28HzgngITTo0InGFGHbGPsVxGPvQoB.#=zfSI77_wxPv0V(#=z_HFh1h9N21sQyH2FimgXElZP1Nonq7KQ_A== #=zS9hZG74=, #=zg4XhJOHcYaratInGFGHbGPsVxGPvQoB.#=zCKB5i7bs0YurnjOhLw==(Stream #=zHKyIkptxFHabpfL$3Q==, Boolean #=z7ygylo2byPvH, List`1 #=zJ4SSOf4w5XWCbJR9z_uxWwxf17D0AWA #=zW_iSz$GlvlBB5M7fLw==)
at Aspose.Pdf.OperatorCollection.#=zvq5X7UM6S3jG()
at Aspose.Pdf.OperatorCollection.#=zYCo173xa$Z8C()
at Aspose.Pdf.OperatorCollection.get_Count()
at Aspose.Pdf.OperatorCollection.#=zzU1PmlA=.MoveNext()
at Aspose.Pdf.Page.get_Layers()
When I check the logs, there are a lot of entries indicating issues with the product Aspose.PDF
Also, there are a lot of entries in our logs with this error message:
Object reference not set to an instance of an object." source=“Aspose.PDF” detail="System.ApplicationException: Object reference not set to an instance of an object. StackTrace: at Aspose.Pdf.Page.#=zWYq$LPbwgIUe()
at Aspose.Pdf.Page.get_Layers()
at LSAS_Web.Common.Support.DocumentHelper.ValidateFileAttributes(String filePath)
at LSAS_Web.Areas.LabelApplication.Controllers.Edit.Step4Controller.ValidateFileAttributes(String filePath)
at LSAS_Web.Areas.LabelApplication.Controllers.Edit.Step4Controller.UploadDocument(DocumentationInfoModel formModel)
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters)
 at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult)
 at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult)
 at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass2b.<BeginInvokeAction>b__1c()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) —> System.NullReferenceException: Object reference not set to an instance of an object.
at Aspose.Pdf.Page.#=zWYq$LPbwgIUe()
at Aspose.Pdf.Page.get_Layers()
Apparently, there is a problem with this get_Layers() method.