1

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.

yacc
  • 2,915
  • 4
  • 19
  • 33
JayKay 135
  • 33
  • 5
  • 1
    The "Visual Studio" in the title and the tag are not appropriate, as you are asking about how to implement something in your application which is independent of the tool you use for editng. – Klaus Gütter May 03 '20 at 06:09
  • 1
    what is `browser`? how is it defined? – Derviş Kayımbaşıoğlu May 03 '20 at 07:18
  • browser is a just a reference to a default WebBrowser component from the toolbox. I guess I should mention aswell, that I'm building my application as a windows forms-app. – JayKay 135 May 03 '20 at 08:01
  • You should tag your question appropriately instead of just mentioning Windows Forms. And include a [MCVE] demonstrating your issue. – mason May 04 '20 at 15:57

2 Answers2

0

If you don't mind a little bit of flickering just navigate to "about:blank" before you try to save.

If you have a probelm with that, just make a temporary copy of the file and open the copy with the browser. Probably not the best solutions, but should work

Phoenixdust
  • 433
  • 2
  • 8
0

My problem was, that the web browser navigation is asynchronous. As a workaround I used an event listener that keeps track, when the browser actually loaded the document.

For more information about that topic check this question out: https://stackoverflow.com/a/583909/12178103

Down here you can see my complete code

//gets called when the application starts
public Form1()
{
    InitializeComponent();

    //first time the web browser load operation gets called - make sure to set the event handler
    webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowserUnload);
    WebBrowserLoad();          
}

//this button regenerates the pdf
private void Button_Click(object sender, EventArgs e)
{
    WebBrowserLoad();
}

//creates the actually pdf document
private void WebBrowserLoad()
{
    browser.Hide();
    browser.Navigate("about:blank");
}

private void WebBrowserUnload(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if (e.Url.ToString() == "about:blank")
    {
        doc = new Document(PageSize.A4);

        using (fileStream = new FileStream("document\pdf", FileMode.Create))
        {
            using (writer = PdfWriter.GetInstance(doc, fileStream))
            {
                PageEventHelper pageEventHelper = new PageEventHelper();
                writer.PageEvent = pageEventHelper;
                doc.Open();

                cb = writer.DirectContent;

                //create the pdf here

                writer.Flush();

                doc.Close();
                doc.Dispose();
            }

        }

        browser.Navigate(@"path\document.pdf");
    }
    else if (e.Url.ToString() == "file:///path/document.pdf")
    {
        browser.Show();
    }
}
JayKay 135
  • 33
  • 5