C#显示PDF文件,winform打开PDF文件并在窗体中显示

1.在工具箱中添加Adobe提供的ActiveX控件,如图所示:

选择com组件,我用的是7.0版本
打勾点确定,在控件工具栏就有了,见下图:

拖一个Adobe PDF Reader控件到窗体上,双击窗体,在窗体加载时,弹出对话框,加载PDF文件:
string fileName = MyOpenFileDialog();
axAcroPDF1.LoadFile(fileName);

MyOpenFileDialog()函数为:
        string MyOpenFileDialog()
         {
            OpenFileDialog ofd = new OpenFileDialog();
             ofd.Filter = "PDF文档(*.pdf)|*.pdf";

            if (ofd.ShowDialog() == DialogResult.OK)
             {
                return ofd.FileName;
             }
            else
             {
                return null;
             }
         }

也可以用代码创建Adobe PDF Reader组件:
string fileName = MyOpenFileDialog();
AxAcroPDFLib.AxAcroPDF axAcroPDF = new AxAcroPDFLib.AxAcroPDF();
axAcroPDF.Location = new System.Drawing.Point(0, 24);
axAcroPDF.Size = new System.Drawing.Size(292, 242);
axAcroPDF.Dock = DockStyle.Fill;
Controls.Add(axAcroPDF);
axAcroPDF.LoadFile(fileName);

不过要注意,在我们把Adobe PDF Reader组件拖到窗体上的时候,它会自动引用2个dll:AcroPDFLib和AcroPDFLib,如图:

在编译的时候,VS会Adobe PDF Reader ActiveX组件转换为2个.net组件:AxInterop.AcroPDFLib.dll和Interop.AcroPDFLib.dll,如图:

所以在写代码创建Adobe PDF Reader 组件的时候,需要手动把Adobe PDF Reader   ActiveX组件转换为.net组件并引用!最好的办法是,托一个Adobe PDF Reader 组件到窗体上,然后删除,这样就不需要手动了!

posted on 2010-08-13 15:43  努力实现目标  阅读(35772)  评论(6编辑  收藏  举报