Free Support Forum - groupdocs.com

Problem While opening the pdf file created by group docs.viewer dll(16.11.0)- file is in use error

Hi,

I am trying to generated a pdf document from a word document using below code. the pdf file is getting genenarted correctrly the location specified by me. but once the file is generated, I need to make use of the file, for example, open it for viewing purpose or copy it to some local folders through code. While i try to open the pdf file, the error comes saying “the file is in use by other process”. if I go to task manager and kill the aspnet process, the file is getting opened but not before that. Ideally, if the file is created, the resourses used by the viewer dll should be released. I tried clearing the objects in finally block but nothing is helping. pls check the sample code below that I am using
ViewerImageHandler imageHandler;
FileContainer container;
try
{
Utilities.ApplyLicense();
string DocumentName = “DOCXsample.docx”;
imageHandler = new ViewerImageHandler(Utilities.GetConfigurations());
container = imageHandler.GetPdfFile(DocumentName);
String filename = “DOCXsample.pdf”;
Utilities.SaveFile(filename, container.Stream);
}
catch (Exception ex)
{

}
finally
{
imageHandler = null;
container = null;
}

Hi Rkotak,


Thanks for using GroupDocs.Viewer for .NET.

We have investigated your scenario and found that every thing is working fine at the API level. The problem occurs when file stream object in Utilities.SaveFile function is not disposed off. Please make sure that your are disposing the file stream object like below.

// Create file stream.
FileStream fileStream = File.Create(filename,(int)content.Length);

// Write bytes of file stream into the file here.

// Dispose the object.
fileStream.Dispose();


You can also use “using” statement to dispose the file stream object automatically like below;

using (FileStream fs = File.Create(filename,(int)content.Length))
{
// Write bytes of file stream into the file here.
} 

Hope it helps.

We strongly recommend you to use the latest version of the API, GroupDocs.Viewer for .NET 16.12.0, for a better experience.

Warm Regards