Delete pdf after creation

Delete pdf after creation

Forum member · Oct 22, 2010
I'm using the Merger api to create a pdf document from a form template and then emailing the newly created pdf document to a user. What I need to do is delete the newly created document after it's been emailed but when my call to File.Delete(pdfDoc) is executed I'm getting an error that the document can't be deleted because it's in use by another process (the DynamicPDF api?). Is there a way to close the pdf document after the call to document.draw has been executed? Haven't been able to find anything yet in the help documentation but will keep looking. TIA, Bill Youngman

Re: Delete pdf after creation

Support · Oct 22, 2010
Hello Bill, 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 ); document.Draw( @"HelloWorld.pdf" ); // Code to delete the file Thanks, ceTe Software Support Team

Re: Delete pdf after creation

Forum member · Oct 22, 2010
Thanks for the quick reply and I'll definitely keep this code snippet for future reference but my problem actually was due to the email object resources not having been released - a simple call to email.dispose() solved my problem. As soon as I remembered this a Homer Simpson DOH!!!! went rushing through my head. Thanks again

Re: Delete pdf after creation

Forum member · Apr 25, 2011
I don't understand how this workaround closes the mergedocument before deleting... I still get the same error when I try to delete (The process cannot access the file ... because it is being used by another process.

Re: Delete pdf after creation

Support · Apr 26, 2011
Hello, The code which we gave above gets the file in the form of FileStream and then it is stored into an byte array object and FileStream object is closed. This will release the file which is used by the FileStream object. The PDF contents which are stored in byte array is used for merging using our DynamicPDF Merger product. If you continue getting the error then please send over the input PDF document and the code which you are using to our support team at:[^a|mailto:support@cete.com|opennewiwndow|support@cete.com^] so that they can look into it further? Thanks, ceTe Software Support Team.