C#winform中打开PDF文件并在窗体中显示
2017-09-26 14:07
627 查看
方法一:利用Procss.Start(“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);
}
方案二可能出现的异常:
其解决的办法:
出现题目的异常,多是引用第三方控件引起的,因此在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:转载请注明出处!
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:转载请注明出处!
相关文章推荐
- C# WinForm打开PDF文件并在窗体中显示
- C#显示PDF文件,winform打开PDF文件并在窗体中显示
- C#显示PDF文件(winform)
- 【C# 基础】— 解决 "winForm 引用 Adobe PDF Reader控件不显示pdf 文件" 问题
- C# WinForm打开PDF文件
- C#WinForm打开office文件,pdf文件和视频文件
- C# WinForm打开PDF文件
- C# WinForm 在窗口菜单上显示已打开窗体的标题及其窗体的排列
- 1、怎样设置C#OpenFileDialog(文件选择窗体)的指定路径、文件格式等属性(设置打开默认路径、文件格式、窗体显示文本)
- winform打开本地的txt文件并显示在窗体中
- C# winForm 窗体:TextBox中显示文件的绝对路径,按钮背景颜色,richTextBox读取文本文件...
- C#读写文件、遍历文件、打开保存文件,窗体程序
- C# winform中不规则窗体制作的解决方案(已经解决24位色以上不能正常显示问题)
- 关于C#Winform线程调用窗体的使用方法以及窗体的单一显示
- C# 输出pdf文件流在页面上显示
- C#中调用abobe reader打开PDF文件的方法
- winform 显示PDF文件
- C# 父窗体菜单项显示已打开的所有子窗体
- c# winForm 将窗体状态栏StatusStrip 分成左中右三部分 右边显示当前时间
- C# 输出pdf文件流在页面上显示