The file is used by another process

The file is used by another process

Forum member · Mar 11, 2006
if i merger two pdf together and want to delete the original , i got the error " the file is used by another process" it is seemd that these two pdf are still open ,how can i close these pdf ? i am using asp.net .

Re: The file is used by another process

Support · Mar 13, 2006
Hello, Our product does not allow you to delete the file immediately after merging or appending. The reason for this is because our product, in an effort to be as efficient as possible, only reads in the part of the merged or appended PDF that it needs and then uses the rest of it right at the time it draws out the new file. So when you try to delete the file after drawing the PDF, the file you are trying to delete is still in use. There is however a way around this but keep in mind you will loose some efficiency by doing it this way (not drastic inefficiency but rather a slight decrease). The work around this is to open the file and read the contents of that file into a byte array and then close and delete that file. Then you can create a PdfDocument out of that byte array and create a MergeDocument out of that PdfDocument. Now when you are going to draw the file, you can specify the filepath to your original file because the file is no longer being used by any other processes. The code for this work around would be something like this: //Create a filestream out of your file FileStream fs = new FileStream(@"C:\temp\pdfs\input\HelloWorld.pdf", FileMode.Open ); byte[] contents = new byte[ fs.Length ]; //Get the contents of the filestream fs.Read( contents, 0, (int)fs.Length ); //Close the filestream, releasing any resources fs.Close(); PdfDocument pdf = new PdfDocument( contents ); MergeDocument document = new MergeDocument( pdf ); //Add any content here you would like Page page = document.Pages[0]; page.Elements.Add( new Circle( 0, 0, 100, Color.Blue ) ); //Save the file document.Draw( @"C:\temp\pdfs\input\HelloWorld.pdf" ); // Code to delete the file