2013-03-04 98 views
1

我正在使用webbrowsercontrol来显示本地存储的.pdf。在按钮按下时,我希望浏览器显示空白页面/无,并且我想将.pdf移动到不同的文件夹。首先我尝试在移动之前导航到“”,但我的.pdf被另一个进程使用。谷歌告诉我,我可能需要清除浏览器的缓存才能移动它。我是这样使用在这里找到的代码:http://www.gutgames.com/post/Clearing-the-Cache-of-a-WebBrowser-Control.aspx,我甚至尝试了在注释nr 2中找到的替代代码行,但没有一个让我移动我的.pdf,它仍然被另一个进程使用。webbrowsercontrol问题,清除缓存不工作?

我能做些什么来移动文件?我忘了什么吗?

在第二File.Move是我得到的错误:

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"); 
} 
+0

如果你只显示PDF,您可以使用Adobe的预览控件,而不是webbrowsercontrol的。 – Vedran 2013-03-04 08:17:29

+0

你能否指点我正确的方向?我在哪里可以找到这样的阅读和代码示例?或者,我应该谷歌? – 2013-03-04 08:39:56

+0

@Vedran明白了,谢谢你的提示! – 2013-03-04 09:10:24

回答

0

这是如何显示PDF而不使用三夏,使用Adobe阅读器来代替:

从下载Acrobat的SDK http://www.adobe.com/devnet/acrobat/downloads.html

在你的项目的引用从SDK添加到两个DLL - AxInterop.AcroPDFLib.dll和Interop.AcroPDFLib.dll

在您的窗体的构造函数中添加的Adobe预览控制:

// 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()); 
    } 
} 

最后PDF文件加载到控制:

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(); 
} 
+0

我只是引用了Adobe的某些类型为lib的东西,将其添加到工具栏中,并将其添加到我的表单中并使用:axAcroPDF1.LoadFile(file);用于将我的文件加载到查看器中。我似乎无法“卸载”该文件。怎么做? – 2013-03-04 09:52:39

+0

您可以随时执行axAcroPDF1.Dispose(),然后在要加载新PDF时重新初始化它。 – Vedran 2013-03-04 10:37:02

+0

我不喜欢它的外观。使控制消失,并出现这样的情况。我通过简单地重新加载pdf文件在File.Move之前的路径来解决它。这生成了一个空的PDF查看器窗口。 – 2013-03-04 11:00:17