您的位置:首页 > 编程语言 > C#

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

2017-09-26 14:07 627 查看
方法一:利用Procss.Start(“pdf文档路径”)

eg:    System.Diagnostics.Process.Start("F:\\1.pdf");

方法二:

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

(2)拖动Adobe Acrobat 7.0 Browser Control控件到窗体中,并创建一个button4按钮,并添加按钮事件。

按钮事件代码如下:

方案一:通过查找pdf文档

  private void btn1_Click(object sender, EventArgs e)     

   {            

    string filename = MyOpenFileDialog();

           axAcroPDF1.LoadFile(filename); 

     }

   string MyOpenFileDialog()       

   {        

          OpenFileDialog ofd = new OpenFileDialog();  

          ofd.Filter = "PDF文档(*.pdf)|*.pdf";

            if (ofd.ShowDialog() == DialogResult.OK)      

          {             

          return ofd.FileName;       

          }     

       else       

        {            

        return null;       

         }       

    }
方案二:直接打开pdf文档

   public void MyOpenFileDialog()
        {
            AxAcroPDFLib.AxAcroPDF axAcroPDF = new AxAcroPDFLib.AxAcroPDF();
           // axAcroPDF.BeginInit();
            ((System.ComponentModel.ISupportInitialize)(axAcroPDF)).BeginInit();
            axAcroPDF.Location = new System.Drawing.Point(0, 24);
           // axAcroPDF.Size = new System.Drawing.Size(292,242);       
            axAcroPDF.Dock = DockStyle.Fill;          
            this.Controls.Add(axAcroPDF);
            //axAcroPDF.EndInit();
            ((System.ComponentModel.ISupportInitialize)(axAcroPDF)).EndInit();
            string filename = MyLoadFile();    
            axAcroPDF.LoadFile( Application.StartupPath+"\\Instruction Manual-EN.pdf");
            axAcroPDF.setView("fit");          
            axAcroPDF.setShowScrollbars(true);
            axAcroPDF.setShowToolbar(true);
        }   

  private void btn1_Click(object sender, EventArgs e)     

   {            

    string filename = MyOpenFileDialog();

           axAcroPDF1.LoadFile(filename); 

     }

方案二可能出现的异常:


System.Windows.Forms.AxHost+InvalidActiveXStateException

其解决的办法:

     
 出现题目的异常,多是引用第三方控件引起的,因此在NEW时,需要初始化该对象:

   
         AxESACTIVEXLib.AxESActiveX ax = new AxESACTIVEXLib.AxESActiveX();

            ((System.ComponentModel.ISupportInitialize)(this.ax)).BeginInit();或者 axAcroPDF.BeginInit();
             this.Controls.Add(ax);
            ((System.ComponentModel.ISupportInitialize)(this.ax)).EndInit();或者axAcroPDF.EndInit();

Ps:转载请注明出处!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: