Con*_*oly 5 pdf wpf webbrowser-control visual-studio-2012
我正在使用Acrobat Reader 11.0.3和Windows 7,Visual Studio 2012,并且已经在Manage Addon中启用了Acrobat Reader
并按照WPF webbrowser在Adobe阅读器窗口中打开PDF文件
尝试两种方法后仍然有错误
即使使用管理员帐户打开WPF程序,也会取消对网页的导航
private void CheckAndFixWebBrowserRenderingEngine()
{
RegistryKey baseRegistryKey = Registry.LocalMachine;
string renderingEngineSubKeyString = @"SOFTWARE";
// 64bit operationg systems have another registry path
if (Environment.Is64BitOperatingSystem)
{
renderingEngineSubKeyString += @"\Wow6432Node";
}
renderingEngineSubKeyString += @"\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION";
//var assemblyValueKey = System.IO.Path.GetFileName(App.ResourceAssembly.Location);
var assemblyValueKey = System.IO.Path.GetFileName(System.Windows.Application.ResourceAssembly.Location);
var renderingEngingeValue = 9999; // check other values below
try
{
RegistryKey sk1 = baseRegistryKey.CreateSubKey(renderingEngineSubKeyString);
var value = sk1.GetValue(assemblyValueKey);
if (value == null || value.ToString() != renderingEngingeValue.ToString())
{
sk1.SetValue(assemblyValueKey, renderingEngingeValue);
//LogHandler.Instance.Add(string.Format("Did update webbrowser rendering engine from {0} to 9000.", value == null ? "[missing]" : value));
}
}
catch (Exception ex)
{
//LogHandler.Instance.Add("Could not check webbrowser rendering engine in registry.");
//LogHandler.Instance.Add(ex.ToString(), Logging.LoggingPriorities.Exception);
}
/*
9999 (0x270F)
Internet Explorer 9. Webpages are displayed in IE9 Standards mode, regardless of the !DOCTYPE directive.
9000 (0x2328)
Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode.
8888 (0x22B8)
Webpages are displayed in IE8 Standards mode, regardless of the !DOCTYPE directive.
8000 (0x1F40)
Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode.
7000 (0x1B58)
Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode.
*/
}
public ReportForm()
{
this.formID = "Test Report";
//base.Initialization();
InitializeComponent();
CheckAndFixWebBrowserRenderingEngine();
String PDFPath = @"C:\Users\LAB-User2\Downloads\Elimination_Methods_in_Polynomial_Computer_Alge(Bookos.org).pdf";
WebBro1.Navigate("file:///" + PDFPath);
Run Code Online (Sandbox Code Playgroud)
方法2
我还尝试了另一种方法,添加参考windowformintegration并使用winform的UserControl,它只在一个独立的WPF小项目中工作,当我在我的大项目中集成它时,它得到了上面的错误
String PDFPath = @"C:\Users\LAB-User2\Downloads\Elimination_Methods_in_Polynomial_Computer_Alge(Bookos.org).pdf";
//WebBro1.Navigate("file:///" + PDFPath);
webBrowser1.Navigate("file:///" + PDFPath);
private System.Windows.Forms.WebBrowser webBrowser1;
<WindowsFormsHost Margin="1" x:Name="windowsFormsHost1" Height="600" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="1000"/>
var uc = new UserControl1();
this.windowsFormsHost1.Child = uc;
application.UI.CreateNewForm("Test Report", new Client.Reporting.Pages2.ClientStatement.ReportForm());
public partial class ReportForm : BaseForm
public void CreateNewForm(string title, BaseForm form)
mw.CreateNewForm(title, form);
public class BaseMainWindow : MetroWindow
{
public virtual void CreateNewForm(string title, BaseForm form)
{
}
Run Code Online (Sandbox Code Playgroud)
更新:在集成到大项目之前,它要求在html文件中允许块浏览器中的块内容和灰色屏幕
<html>
<body>
<object
classid="clsid:ca8a9780-280d-11cf-a24d-444553540000" id="pdf1"
type="application/pdf"
data="C:\Users\LAB-User2\Downloads\Elimination_Methods_in_Polynomial_Computer_Alge(Bookos.org).pdf"
style="width: 100%; height: 100%">
<param name="src" "value="C:\Users\LAB-User2\Downloads\Elimination_Methods_in_Polynomial_Computer_Alge(Bookos.org).pdf"></param>
</object>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

更新:在大型WPF项目中运行代码时,它会显示一个红叉

更新:更新后的html工作在独立的html文件中,但是集成在WPF大项目中,它无法显示并且只显示红十字
String temphtml = "<!-- saved from url=(0016)http://localhost --><body style=\"border: 0; margin: 0; padding: 0\"><object classid=\"clsid:ca8a9780-280d-11cf-a24d-444553540000\" id=\"pdf1\" type=\"application/pdf\" data=\"02_LAB_LeaveApplication.pdf\" style=\"width: 100%; height: 100%; border: 0; margin: 0; padding: 0\"></object><script>window.onload = function(){ window.focus();pdf1.setActive(); }</script></body>";
System.IO.File.WriteAllText(@"C:\Users\LAB-User2\Desktop\vpfs-ivpfs-client\iVPFS_Client\Application\bin\Debug\a.html", temphtml);
this.WebBro1.Navigate(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "a.html"));
Run Code Online (Sandbox Code Playgroud)
更新虽然运行从codeproject下载的项目,它显示一个错误,然后可以显示pdf并打开时有几次空白,然后我将代码和库复制到一个小的WPF项目并运行,它也有错误出来,只有显示白色空白https://docs.google.com/file/d/0Bxs_ao6uuBDUeV8yREVBOExSbW8/edit?usp=sharing
您可以生成一个临时 HTML 文件来嵌入所需的 PDF:
<body>
<object
classid="clsid:ca8a9780-280d-11cf-a24d-444553540000" id="pdf1"
type="application/pdf"
data="test.pdf"
style="width: 100%; height: 100%">
<param name="src" "value"="test.pdf"></param>
</object>
</body>
Run Code Online (Sandbox Code Playgroud)
有趣的是,为了使其与本地 (file://) PDF 文档一起使用,我必须指定data属性和src参数。当它通过 http 从 localhost 提供时,仅数据就足够了。
更新:我不知道为什么这对 OP 不起作用,所以我编写了一个非常基本的C# WebBrowser 项目来演示它。它在我的环境(Win8、IE10、Acrobat Reader 11.0.3)中运行得非常可靠。
更新:我想我明白为什么会发生这种情况。您的项目使用 WPF,而我的示例使用 Winforms。抱歉,我一开始没有注意到这一点。WPF WebBrowser具有不同的安全模型,它默认在IE保护模式下运行。
使用 WPF WebBrowser,我已经设法摆脱了提示(注意<!-- saved from url=(0016)http://localhost -->),但我确实确认 Acrobat Reader 控件只是时不时地挂起加载文件:
<!-- saved from url=(0016)http://localhost -->
<body style="border: 0; margin: 0; padding: 0">
<object
classid="clsid:ca8a9780-280d-11cf-a24d-444553540000" id="pdf1"
style="width: 100%; height: 100%; border: 0; margin: 0; padding: 0">
<param name="src" "value"="test.pdf"></param>
</object>
<script>
window.onload = function()
{
window.focus();
pdf1.setActive();
}
</script>
</body>
Run Code Online (Sandbox Code Playgroud)
已知Acrobat Reader 控件在保护模式下存在问题。与其尝试寻找解决方法,直接在 WPF 项目中托管 Acrobat Reader ActiveX 控件可能更容易。具体方法如下:
http://www.codeproject.com/Articles/380019/Using-Adobe-Reader-in-a-WPF-app http://hugeonion.com/2009/04/06/displaying-a-pdf-file-within -a-wpf-应用程序/