문제

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");
}
도움이 되었습니까?

해결책

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();
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top