关于在c#中打开pdf文件 [英] about opening a pdf file in c#
本文介绍了关于在c#中打开pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用c#打开一个pdf文件。我写了下面的代码并得到一个错误,使用未分配的局部变量a
I want to open a pdf file using c# . i have written the following code and got an error as "Use of unassigned local variable a"
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AxAcroPDFLib;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
AxAcroPDF a;
a.LoadFile("d:/novels/master_of_the_game.pdf");
a.Show();
}
private void axAcroPDF1_OnError(object sender, EventArgs e)
{
MessageBox.Show("unable to open file");
}
}
}
推荐答案
喜欢这个?
AxAcroPDF a =新AxAcroPDF();
a.loadFile(@C:\ a.pdf);
:
http://stackoverflow.com/questions/5562691/axacropdf-doesn-not-display-pdf-on-windows-xp [ ^ ]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Title = "Open";
open.Filter = "All Files|*.*";
try
{
if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
axAcroPDF1.LoadFile(open.FileName);
}
}
catch (ArgumentException ex)
{
MessageBox.Show(ex.Message.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
string path =C:\\Report\\filename.pdf;
FileStream fs;
fs = File.Open(path,FileMode.Open);
byte [] bytBytes = new byte [fs.Length];
fs.Read(bytBytes,0,Convert.ToInt32(fs.Length));
fs.Close();
Response.ContentType =application / pdf;
Response.BinaryWrite(bytBytes);
FileInfo fi1 = new FileInfo(path);
fi1.Delete();
Response.Flush();
Response.End();
string path = "C:\\Report\\filename.pdf";
FileStream fs;
fs = File.Open(path, FileMode.Open);
byte[] bytBytes = new byte[fs.Length];
fs.Read(bytBytes, 0, Convert.ToInt32(fs.Length));
fs.Close();
Response.ContentType = "application/pdf";
Response.BinaryWrite(bytBytes);
FileInfo fi1 = new FileInfo(path);
fi1.Delete();
Response.Flush();
Response.End();
这篇关于关于在c#中打开pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文