I would like to create a PDF document with iTextSharp and preview it directly in the application. And this not only once, but as often as I like, during runtime, when the user makes changes to the text input.
So far it works, but as I said before only once, when the program is started. When I try to generate the PDF file again, I get an error message, that the process cannot access the saved PDF document because it is currently being used by another process.
I have already tried to prevent access, but without success so far.
private void CreateDocument()
{
//my attempt to stop the browser from blocking the file acces
if (browser.IsBusy())
{
browser.Stop();
}
doc = new Document(PageSize.A4);
writer = PdfWriter.GetInstance(doc, new FileStream("document.pdf", FileMode.Create));
doc.Open();
cb = writer.DirectContent;
//here is the actual pdf generation
doc.Close();
//this is the part where I set the pdf document reference from the web browser
browser.Navigate(@"path\document.pdf");
}
The actually error occurs where I set the PDFwriter instance.
I've found a page preview component in the toolbox from iTextSharp, but sadly no reference on how to use it. Using that might work easier than trying it with the web browser.