zoukankan      html  css  js  c++  java
  • C#显示PDF文件,winform打开PDF文件并在窗体中显示

    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 组件到窗体上,然后删除,这样就不需要手动了!

  • 相关阅读:
    Java自学-JDBC execute与executeUpdate的区别
    POI 操作Word, 向Word中写入文本,图片.
    C++ 设计模式 4:行为型模式
    C++ 设计模式 3:结构型模式
    C++ 设计模式 2:创建型模式
    C++ 设计模式 1:概述
    C++ 数据结构 4:排序
    C++ 数据结构 3:树和二叉树
    C++ 数据结构 2:栈和队列
    C++ 数据结构 1:线性表
  • 原文地址:https://www.cnblogs.com/hfzsjz/p/1799094.html
Copyright © 2011-2022 走看看