Issues with Aspose.PDF version 24.3.0

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();
                }
            }
        }

Please advise.

@jimmy.rojas

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:

  1. 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.

  2. 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.

  3. Use Try-Catch: Implement error handling around the code that accesses the layers. This will help catch any exceptions thrown when accessing the layers.

    try
    {
        List<Aspose.Pdf.Layer> layers = page.Layers;
        if (layers != null && layers.Count > 0)
        {
            errors.Add("PDF contains layers.");
        }
    }
    catch (Exception ex)
    {
        errors.Add($"Error accessing layers: {ex.Message}");
    }
    
  4. 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.

  5. 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!

This doesn’t help. It’s similar to my code. It’s failing in this line:
List<Aspose.Pdf.Layer> layers = page.Layers;

It just keeps reading without any response.
I can submit some PDF files for testing purposes.

How can I contact Aspose support?

@jimmy.rojas,

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.

This is a PDF sample with layers that fails when I try to upload.

8 Count Original Beef_Dry Meat Bag_Lo-1_V7 (Original).pdf (3.3 MB)

Here is another funny case. I got this error when I try to upload the attach file:
Test TDP_Creo_Tetra4D_V03042024.pdf (1.7 MB)

Error:
Invalid index: index should be in the range [1…n] where n equals to the operators count.

It fails when it’s looping through the pages.

@jimmy.rojas

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?

I’d like to point out that these cases work properly with the version 19.6 that we are currently using in our project.

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.

@jimmy.rojas

You can download .NET Framework 4.0 compatible DLLs from our Downloads section at below link:

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.

All right. Let me try that DLL. I’ll let you know if we have the same issues. Thanks!

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();
                }
            }
        }

Please advise.

@jimmy.rojas

Please try initializing and using the PdfFileInfo as below and let us know if it helps in resolving the issue:

using (PdfFileInfo pdfFile = new PdfFileInfo())
{
    pdfFile.BindPdf("");
    // rest of the code
}

It seems that the issue was resolved by adding this line:
pdfFile.BindPdf(“”);

I smoke tested some scenarios and it worked.
Should I always include this line when I work with the class PdfFileInfo from now on?

Do I still need this to make it work?
System.Threading.Thread.Sleep(1000);

Also, I noticed another issue when I try upload and then download PDF files with data entries.
For instance:
PY 221 Label Notice_v8RE508 (Egg Form).pdf (179.1 KB)

How can I add a validation to prevent the users from uploading PDF files with data entries?

@jimmy.rojas

Yes, it is recommended and correct way to use PdfFileInfo Class.

No, it is not needed any more.

Do you mean to detect the PDF files with Form Fields?

I was referring to this line:
pdfFile.BindPdf(“”);

Yes, I meant to detect the PDF files with Form Fields.

Also, can I use this code snippet to check if a PDF file contains signatures?

public static bool IsSignedPdf(string filePath)
{
PdfFileSignature pdfSign = new PdfFileSignature();
pdfSign.BindPdf(filePath);
return pdfSign.ContainsSignature();
}

@jimmy.rojas

You can use below property to determine if PDF has any form fields or not:

Document.Form.Fields.Count

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)&#xD;&#xA; at #=z9W8OEM$p8$g7694whr8T0jRxJwvdctJ1R$EeZ3ITou1EZTgHRw==.#=zkksCAzmrsk_H(#=zF7$jGz6kYTSr2nSyKQalEmRNcx7aLt08LC2n_xNOhF9u #=z_Ip$DbYUI972)&#xD;&#xA; at #=ziN9ESXTDEHqQ9i28HzgngITTo0InGFGHbGPsVxGPvQoB.#=ziM4g4g1Qaq0P(#=zF7$jGz6kYTSr2nSyKQalEmRNcx7aLt08LC2n_xNOhF9u #=zX9TZRmo=, Int64 #=zwCgSo68=, Nullable1& #=zeWA7mAE=) at #=ziN9ESXTDEHqQ9i28HzgngITTo0InGFGHbGPsVxGPvQoB.#=zAdIg0khetjot(List1 #=z4SqYp6Y=, #=zF7$jGz6kYTSr2nSyKQalEmRNcx7aLt08LC2n_xNOhF9u #=zX9TZRmo=, Int64 #=zwCgSo68=)&#xD;&#xA; 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)&#xD;&#xA; at #=z9W8OEM$p8$g7694whr8T0jRxJwvdctJ1R$EeZ3ITou1EZTgHRw==.#=zkksCAzmrsk_H(#=zF7$jGz6kYTSr2nSyKQalEmRNcx7aLt08LC2n_xNOhF9u #=z_Ip$DbYUI972)&#xD;&#xA; at #=ziN9ESXTDEHqQ9i28HzgngITTo0InGFGHbGPsVxGPvQoB.#=ziM4g4g1Qaq0P(#=zF7$jGz6kYTSr2nSyKQalEmRNcx7aLt08LC2n_xNOhF9u #=zX9TZRmo=, Int64 #=zwCgSo68=, Nullable1& #=zeWA7mAE=) at #=ziN9ESXTDEHqQ9i28HzgngITTo0InGFGHbGPsVxGPvQoB.#=zAdIg0khetjot(List1 #=z4SqYp6Y=, #=zF7$jGz6kYTSr2nSyKQalEmRNcx7aLt08LC2n_xNOhF9u #=zX9TZRmo=, Int64 #=zwCgSo68=)&#xD;&#xA; 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)&#xD;&#xA; 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)&#xD;&#xA; 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)&#xD;&#xA; 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.

This is a PDF file that users have reported they are unable to upload into our application.

THLG-2025-Beef-Protocol.pdf (397.5 KB)