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

本文介绍了两种在C#应用程序中打开PDF文档的方法。一种是使用Process.Start方法直接打开PDF文件,另一种是通过集成Adobe Acrobat ActiveX控件来实现PDF预览及交互功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法一:利用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:转载请注明出处!


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值