C# 尝试使用 AxAcroPDFLib 打开 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29952970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:23:55  来源:igfitidea点击:

Trying to open a PDF with AxAcroPDFLib

c#pdfadobe

提问by Tamas Kinsztler

I have installed the latest Adobe Reader on my PC (Adobe Acrobat Reader DC). Now I would like to use AxAcroPDFLib in C# to open and show a PDF file in my Windows Forms application.

我已经在我的 PC 上安装了最新的 Adob​​e Reader (Adobe Acrobat Reader DC)。现在我想在 C# 中使用 AxAcroPDFLib 在我的 Windows 窗体应用程序中打开和显示 PDF 文件。

The problem is, if I am trying to use the LoadFile()method, then it says that this method is not exist.

问题是,如果我尝试使用该LoadFile()方法,则它说该方法不存在。

I loaded the Adobe Acrobat 7.0 Browser Control Type Library 1.0COM reference into my project, and I added the Adobe PDF ReaderCOM Component to my toolbox (Tools / Choose Toolbox Items... / COM Components).

我将Adobe Acrobat 7.0 Browser Control Type Library 1.0COM 引用加载到我的项目中,并将Adobe PDF ReaderCOM 组件添加到我的工具箱(工具/选择工具箱项目.../COM 组件)。

enter image description here

在此处输入图片说明

What is wrong? How should I open a PDF file with this library? I found a lot of tutorials on the internet, and everybody tells that I have to use the LoadFile method... Please help, thanks!

怎么了?我应该如何使用此库打开 PDF 文件?网上找了很多教程,大家都说必须要用LoadFile的方法...请大家帮忙,谢谢!

回答by BrankoH

This is no longer supported in Adobe Reader DC. Install Adobe Reader v11 and it will work.

Adobe Reader DC 不再支持此功能。安装 Adob​​e Reader v11 即可使用。

回答by Scott

This is still possible. You just need to invoke the method differently.

这仍然是可能的。您只需要以不同的方式调用该方法。

public void LoadFile(string path)
{
    this.GetOcx().GetType().InvokeMember("LoadFile", BindingFlags.InvokeMethod | 
      BindingFlags.OptionalParamBinding, null, this.GetOcx(), new object[1] { path });
}

See this post for details.

有关详细信息,请参阅此帖子。

回答by knr

Cast object representing your control (of type AxAcroPDFLib.AxAcroPDF) to AcroPDFLib.IAcroAXDocShiminterface:

将代表您的控件(类型AxAcroPDFLib.AxAcroPDF)的对象转换为AcroPDFLib.IAcroAXDocShim接口:

var acro = (AcroPDFLib.IAcroAXDocShim)axAcroPDFControl.GetOcx();
acro.LoadFile(fileName);

It appears that all useful methods are now available under this interface. Works if Adobe Reader DC is installed.

似乎所有有用的方法现在都可以在此界面下使用。如果安装了 Adob​​e Reader DC,则有效。

A little extension can be defined:

可以定义一个小扩展:

public static class AcroExtensions
{
    public static AcroPDFLib.IAcroAXDocShim AsAcroPDF(this AxAcroPDFLib.AxAcroPDF source)
    {
        return (AcroPDFLib.IAcroAXDocShim)source.GetOcx();
    }
}

Then you can write:

然后你可以写:

axAcroPDFControl.AsAcroPDF().LoadFile(fileName)

回答by Alexander Kozlov

Just in case anyone still needs a solution. I'm using Adobe Acrobat DC and actually have AxAcroPDF.LoadFile()method. However, it doesn't work i.e. nothing happenns :/

以防万一有人仍然需要解决方案。我正在使用 Adob​​e Acrobat DC 并且实际上有AxAcroPDF.LoadFile()方法。但是,它不起作用,即什么也没有发生:/

So, I used AxAcroPDF.srcproperty with url for local file

所以,我使用AxAcroPDF.src带有 url 的属性作为本地文件

axAcroPdf1.src = "file:///c:/my.pdf"

Hope it helps

希望能帮助到你