The requested operation cannot be performed on a file with a user-mapped section open. : 'test.pdf' Waiting for 1 seconds and retrying

Here the issue is with `

Document.Save`(filename)
PdfFileEditor.Insert (args)
Document.Pages.Delete(num)
any one of the method randomly throws an exception.

our app processes multiple PDFs sequentially. The issue happens with random files. This issue can be replicate when we process 100 documents. out of 100, 2 -4 documents failing with that error (i.e. The requested operation…). Randomly one above mentioned method failed with this error.

Any suggestion/thoughts would be appreciated.

MicrosoftTeams-image (3).png (9.0 KB)
MicrosoftTeams-image (2).png (11.5 KB)

@TZTCS,

I want to be sure I am trying to replicate this problem correctly.

If I copy a document 100, and in a loop, I delete a page, I can replicate the issue? or it is more than that?

Yes. We are inserting the attachments to PDF then deleting some pages and then trying to save that PDF at this point of time it’s failing any one of the above-mentioned methods randomly.

@TZTCS,

Can you share the whole snippet? with the iteration? please.

Hi Molina,
Below is the code snippet and highlighted the error throwing lines
Code Snippet:

            Aspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(completePath);                
            foreach (var attachment in lstAttachments)
            {                   
                if (attachment.pageNumber > 0)
                {
                    var kI = attachment.KI;
                    attachment.uniqueFileId = Guid.NewGuid();
                    // check if the attachment files are generated
                    attachmentFile = Path.Combine(pdfTempDir, String.Format("{0}_{1}_{2}.pdf" ,                            
                        kI.KDId.ToString(),
                        kI.Id.ToString(),
                        guid));
                    if (!File.Exists(attachmentFile))
                    {
                            // saving attachment file
                    }
                    else
                    {
                        // get the number of count
                        Aspose.Pdf.Document attachmentPDF = new Aspose.Pdf.Document(attachmentFile);
                        attachment.numberOfPages = attachmentPDF.Pages.Count;
                    }                        
                }
            }
            pdfDoc.Save(newFilePath);                
              docPageCount = pdfDoc.Pages.Count;
            int attachmentPageCount = 0;              
            for (int i = lstAttachments.Count - 1; i >= 0; i--)
            {
                var attachment = lstAttachments[i];
                var kI = attachment.KI;
                // check if the attachment files are generated
                attachmentFile = Path.Combine(pdfTempDir, String.Format("{0}_{1}_{2}.pdf",
                    kI.KDId.ToString(),
                    kI.Id.ToString(),
                    guid));
                Aspose.Pdf.Document attachmentDocument = new Aspose.Pdf.Document(attachmentFile);
                attachmentPageCount = attachmentDocument.Pages.Count;
                PdfFileEditor pdfEditor = new PdfFileEditor();
                if (attachment.pageNumber < docPageCount)
                {                     
                    while (attachmentPageCount != 0)
                    {
                        **pdfEditor.Insert(newFilePath, attachment.pageNumber, attachmentFile, attachmentPageCount, attachmentPageCount, newFilePath);**
                        attachmentPageCount--;
                    }
                }
                else
                {
                   //concatinate
                }
            }
            // Delete Marker Pages
            Aspose.Pdf.Document pageDeleteDoc = new Aspose.Pdf.Document(newFilePath);
            foreach (var attachment in lstAttachments)
            {
                // delete that page 
                pageDeleteDoc.Pages.Delete(attachment.pageNumber + pageNumberFactor - index);
                pageNumberFactor += attachment.numberOfPages;
                index++;
            }
            **pageDeleteDoc.Save(newFilePath);**

}

@TZTCS

We are checking it and will get back to you shortly.

Hi Team, Any update on this?

@TZTCS

We could not use your code snippet as it has some missing definitions and variables. Therefore, we tried to access multiple PDF files in a directory and manipulate them. We could not replicate this issue.

Can you please make sure that the file is not accessed by any other program or code snippet in your routine while Aspose.PDF is processing it? Also, please try using the latest version of the API. In case issue still persists, we request you please share a sample console application that we can use to reproduce the issue and address it accordingly.