Question:
I am unable to delete a PDF file with the following code.
using (PDFDoc doc = new PDFDoc()) { pdftron.PDF.Annots.RubberStamp.Create(doc, new Rect(0, 0, 100, 100)); doc.Save(output_path + "output1.pdf", SDFDoc.SaveOptions.e_remove_unused); } File.Delete(output_path + "output1.pdf");
The call to File.Delete will throw an exception that the file is locked by a process.
Answer:
The issue is that pdftron.PDF.Annots.RubberStamp.Create actually takes a SDFDoc object, and implicitly a SDFDoc object is being constructed. In turn, since this temp SDFDoc object is not in a using statement, and when the GC does not reclaim, it will typically outlast the using statement.
The solution is the following.
`
using (PDFDoc doc = new PDFDoc())
using (SDFDoc sdoc = doc.GetSDFDoc())
{
pdftron.PDF.Annots.RubberStamp.Create(sdoc, new Rect(0, 0, 100, 100)); // use sdoc variable to avoid implicit construction
doc.Save(output_path + “output1.pdf”, SDFDoc.SaveOptions.e_remove_unused);
}
File.Delete(output_path + “output1.pdf”);
`
We will look to modify our API so that is not an issue in a future release.