We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

PDF Portfolio | The process cannot access the file 'C:\Sample_DOC.doc' because it is being used by another process

Hi Team,

I am trying the create the pdf portfolio file using the below code.
Reference:- Working with Portfolio in PDF|Aspose.PDF for .NET

            using (Aspose.Pdf.Document doc = new Aspose.Pdf.Document())
            {
                doc.Collection = new Aspose.Pdf.Collection();
                foreach (var fName in selectedFileNames)
                {
                    string ext = Path.GetExtension(fName);
                    
                    using (Aspose.Pdf.FileSpecification fileSpecification = new Aspose.Pdf.FileSpecification(fName))
                    {
                        fileSpecification.Description = $"{ext} File";
                        doc.Collection.Add(fileSpecification);
                    }
                }
                doc.Save(outputPDF);
                doc.Collection = null;
            }

Pdf Portfolio file created successfully. But files used for portfolio creation is not getting released. While trying to open any file after portfolio completion, it is throwing an error - “The process cannot access the file ‘C:\Sample_DOC.doc’ because it is being used by another process.”

foreach (var fName in selectedFileNames)
{
try
{
File.Open(fName, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch (IOException ex)
{
MessageBox.Show(ex.Message);
}
}

Could you please look into this?

Thanks,
Saurabh

@sranjan50

Is it happening with every type of file you are using in FileSpecificaton or only with of type .doc? Can you please share a bit more details so that we can test the scenario in our environment accordingly.

It is happening with all doctypes like pdf, msg, txt, doc, xlsx, docx…
Aspose Version - 21.9.0.0

I am able to resolve this by using the combination of thread and GC.Collect()
But this solution not looks feasible to me. Calling GC.Collect is always costly.

   main()
   {
       Task.Run(() => GeneratePdfPortfolio(selectedFileNames, outputPDF)).Wait();
       GC.Collect();
       GC.WaitForPendingFinalizers();

            foreach (var fName in selectedFileNames)
            {
                try
                {
                    File.Open(fName, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
                }
                catch (IOException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
   }

     private void GeneratePdfPortfolio(List<string> selectedFileNames, string outputPDF)
    {
        using (Aspose.Pdf.Document doc = new Aspose.Pdf.Document())
        {
            doc.Collection = new Aspose.Pdf.Collection();
            foreach (var fName in selectedFileNames)
            {
                string ext = Path.GetExtension(fName);
                using (Aspose.Pdf.FileSpecification fileSpecification = new Aspose.Pdf.FileSpecification(fName))
                {
                    fileSpecification.Description = $"{ext} File";
                    doc.Collection.Add(fileSpecification);
                }
            }
            doc.Save(outputPDF);
            doc.FreeMemory();
            doc.Collection = null;
        }
    }

@sranjan50

We would like to suggest you use 22.11 version of the API as this is the latest one and in case issue still persists, please let us know. Meanwhile, we are also testing the case in our environment and will let you know as soon as we have some results to share.

Verified in 22.11 Version and behavior is same.

@sranjan50

Can you please share some sample files for our reference which we can use to test the scenario along with the screenshot of the error? We will log an investigation ticket in our issue tracking system and share the ID with you.

Sample_DOCX.docx (14.3 KB)
Sample_JPG.jpg (55.2 KB)
Error.PNG (172.1 KB)

Source Code
using (doc = new Aspose.Pdf.Document())
{
doc.Collection = new Aspose.Pdf.Collection();
foreach (var fName in selectedFileNames)
{
string ext = Path.GetExtension(fName);
using (Aspose.Pdf.FileSpecification fileSpecification = new Aspose.Pdf.FileSpecification(fName))
{
fileSpecification.Description = $"{ext} File";
doc.Collection.Add(fileSpecification);
}
}
doc.Save(outputPDF);
doc.FreeMemory();
doc.Collection = null;

}

foreach (var fName in selectedFileNames)
{
try
{
//getting error while opening or deleting the same file
File.Open(fName, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch (IOException ex)
{
MessageBox.Show(ex.Message + ex.StackTrace);
}
}

@sranjan50

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

@asad.ali

Can you share any update with us please?

@sranjan50

We were able to reproduce this issue in our environment while testing the case using 22.12 version of the API. Therefore, it has been logged as PDFNET-53373 in our issue tracking system. We will further look into its details and keep you posted with the status of its correction. Please be patient and spare us some time.

We are sorry for the inconvenience.