Error -used by another process

Hi,
We are using Aspose version 17.5 to convert word document to pdf in our web application built using asp.net c#.
We are using it in the following way:
In our web application, users upload word document. Uploaded file is saved to our server and perform hash check for validation. If not valid the file is deleted, and error message is displayed.
After validation, we use Aspose to convert word document to pdf. It was working fine until August 23,2024. And no changes were made to the application.
Error we are getting:
From August 24,2024, word file is validated and saved as docx file successfully. In Aspose call to convert Microsoft word to pdf document step, we are getting “The process cannot access the file because it is used by another process” error and file is not converted to pdf.
When I try to open the saved docx file from server, it opens without any error.
The code we use is

String filepathtoread =””
String pdfilewithfilepath=””;
Aspose.Words.License lic = new Aspose.Words.License();

lic.SetLicense(new MemoryStream(File.ReadAllBytes(asposeLicenseLocation + "Aspose.Words.lic")));
// load the file to be converted
var document = new Aspose.Words.Document(filepathtoread);
// save in different formats
document.Save(pdfilewithfilepath, Aspose.Words.SaveFormat.Pdf);

Thanking you,
Nirmala Manipandian

@cio-20.ntsb.gov Looks like some another process blocks the file and Aspose.Words cannot read it. Please try reading the file into a stream or into a byte array and then open the document from stream. For example:

byte[] documentBydes = File.ReadAllBytes(filepathtoread);
using (MemoryStream docStream = new MemoryStream(documentBydes))
{
    // load the file to be converted
    var document = new Aspose.Words.Document(docStream);
    // save in different formats
    document.Save(pdfilewithfilepath, Aspose.Words.SaveFormat.Pdf);
}

If the same error will occur while reading file bytes, you can be sure some other process blocks the file.

I used your code and worked on Friday.

Today(Monday) it is not working.

I even added 2 try catch

  1. when copying the file to bytestream

  2. when calling Aspose.words.document

I got error in var document = new Aspose.Words.Document(docStream);

Here is the code I used:

string pathToSaveconvertedfile = "";
byte[] documentBytes = null;
try
{
    documentBytes = File.ReadAllBytes(docPath);
    if (documentBytes == null || documentBytes.Length <= 0)
    {
        throw new Exception("file is empty");
    }
}
catch (Exception ex)
{
    errorindocument.Append("read file to convert: ");
    errorindocument.Append(ex.StackTrace);
    errorindocument.Append(ex.Message);
    break;
}
try
{
    using (MemoryStream docStream = new MemoryStream(documentBytes))
    {
        // load the file to be converted
        var document = new Aspose.Words.Document(docStream);
        // save in different formats
        document.Save(pathToSaveconvertedfile, Aspose.Words.SaveFormat.Pdf);
    }
}
catch (Exception ex)
{
    errorindocument.Append("aspose convert and save: ");
    errorindocument.Append(ex.Message);
}

~WRD0002.jpg (357 Bytes)

@cio-20.ntsb.gov Could you please specify what exception is thrown by Aspose.Words.Document constructor? Does the error occur with some specific document or with all documents? If the problem occurs with a specific document, please attach it here for testing. We will check it and provide you more information.

Thanks. After I passed the bytes to memory stream it works. You can close this ticket.

1 Like