1

I'm using a webbrowsercontrol to show .pdf's stored locally. At button press I want the webbrowser to show an empty page/nothing and I want to move the .pdf to a different folder. First I tried navigating to "" before moving but my .pdf was used by another process. Google told me that I probably needed to clear the browser's cache to be able to move it. I did so using the code found here: http://www.gutgames.com/post/Clearing-the-Cache-of-a-WebBrowser-Control.aspx and I even tried the alternative code line found in comment nr 2, but none of these let me move my .pdf, it's still used by another process.

What can/should I do to be able to move the file? Have I forgotten something?

At the second File.Move is where I get the error:

webBrowser1.Navigate("");
WebBrowserHelper.ClearCache();
if (calConv != "")
{
    File.Move(forsDir + calConv + ".cal", forsDir + calConv.Replace("ToDo\\", "") + ".cg4");
    File.Move(forsDir + calConv + ".pdf", forsDir + calConv.Replace("ToDo\\", "") + ".pdf");
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397

1 Answers1

0

This is how to show the PDF without using the webcontrol, using Adobe Reader instead:

Download the Acrobat SDK from http://www.adobe.com/devnet/acrobat/downloads.html

In your project add a reference to two dlls from the SDK - AxInterop.AcroPDFLib.dll and Interop.AcroPDFLib.dll

In your form's constructor add the Adobe previewer control:

// Check if the user has Adobe Reader installed, if not you could show a link to Adobe Reader installer
if (Type.GetTypeFromProgID("AcroPDF.PDF") == null)
{
    pnlGetAdobe.Visible = pnlGetAdobe.Enabled = true;
}
else
{
    try
    {
        // Initialize the Adobe control
        axAcroPDF1 = new AxAcroPDF();
        axAcroPDF1.Dock = DockStyle.Fill;
        axAcroPDF1.Enabled = true;
        axAcroPDF1.Location = new Point(0, 25);
        axAcroPDF1.Name = "axAcroPDF1";
        axAcroPDF1.OcxState = (AxHost.State)new ComponentResourceManager(typeof(JasperPdfReport)).GetObject("axAcroPDF1.OcxState");
        axAcroPDF1.Size = new Size(634, 393);
        axAcroPDF1.TabIndex = 1;
        pnlCenter.Controls.Add(axAcroPDF1); // Add it to a container or instead directly to your form with this.Controls.Add(axAcroPDF1)
        axAcroPDF1.BringToFront();
    }
    catch (COMException cex)
    {
        axAcroPDF1.Dispose();
        axAcroPDF1 = null;
        MessageBox.Show(cex.ToString());
    }
    catch (Exception ex)
    {
        axAcroPDF1.Dispose();
        axAcroPDF1 = null;
        MessageBox.Show(ex.ToString());
    }
}

And finally load your PDF file into the control:

if (axAcroPDF1 != null && File.Exists(pdfFilename))
{
    axAcroPDF1.setShowToolbar(false);
    axAcroPDF1.setView("FitH");
    axAcroPDF1.setLayoutMode("SinglePage");
    // Load the PDF into the control
    axAcroPDF1.LoadFile(pdfFilename);
    axAcroPDF1.src = pdfFilename;

    // Show it
    axAcroPDF1.Show();
    axAcroPDF1.Refresh();
}
Vedran
  • 10,369
  • 5
  • 50
  • 57
  • I just referenced the Adobe something something type lib, added it to the toolbar, added it to my form and used: axAcroPDF1.LoadFile(file); for loading my file into the viewer. I can't seem to "unload" the file though. How to do that? – ChristopherSirén Mar 04 '13 at 09:52
  • You could always do axAcroPDF1.Dispose() then reinitialize it when you want to load a new PDF. – Vedran Mar 04 '13 at 10:37
  • I didn't like the look of it. Having the control disappering and appearing like that. I solved it by simply reloading the path where the pdf file was _before_ the File.Move. This generated an empty pdf viewer window. – ChristopherSirén Mar 04 '13 at 11:00
  • Beware what happens when the Adobe control is not installed, your application might crash - which is why I'm referencing the SDK dll-s and manually loading the control. If that's not the issue for you - you're done :) – Vedran Mar 04 '13 at 11:15