1.新建项目
2.新增一个新文件夹“lib”(主要是为了存放引用的dll)
3.将“gsdll32.dll 、PDFLibNet.dll 、PDFView.dll”3个dll添加到文件夹中
4.项目添加“PDFLibNet.dll 、PDFView.dll”2个类库的引用,并将gsdll32.dll 拷贝到项目生产根目录中(bin 目录)
(注意:gsdll32.dll 是无法和其它两个dll 一样添加到项目中进行引用的,这一点我之前未能给大家作出说明,不好意思,^_^ )
5.在主界面中添加文本框“TextBox1”,浏览按钮“button1”,转换按钮“button2”和文件选择控件“OpenFileDialog1”
6.执行方式:点击浏览按钮选择一个PDF,点击“转换按钮”即可
7.源代码:Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace PDFEditer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 将PDF 相应的页转换为图片
/// </summary>
/// <param name="strPDFpath">PDF 路径</param>
/// <param name="Page">需要转换的页页码</param>
private void GetImage(string strPDFpath,int Page)
{
FileInfo file=new FileInfo (strPDFpath);
string strSavePath=file.Directory.FullName;
byte[] ImgData = GetImgData(strPDFpath, Page);
MemoryStream ms = new MemoryStream(ImgData, 0, ImgData.Length);
Bitmap returnImage = (Bitmap)Bitmap.FromStream(ms);
string strImgPath=Path.Combine(strSavePath,string.Format("PDFConvert{0}.jpg",Page));
returnImage.Save(strImgPath);
}
/// <summary>
/// 从PDF中获取首页的图片
/// </summary>
/// <param name="PDFPath">PDF 文件路径</param>
/// <param name="Page">需要获取的第几页</param>
/// <returns></returns>
private byte[] GetImgData(string PDFPath,int Page)
{
System.Drawing.Image img = PDFView.ConvertPDF.PDFConvert.GetPageFromPDF(PDFPath,Page, 300, "", true);
return GetDataByImg(img);//读取img的数据并返回
}
/// <summary>
/// 将单页的PDF转换为图片
/// </summary>
/// <param name="_image"></param>
/// <returns></returns>
private byte[] GetDataByImg(System.Drawing.Image _image)
{
System.IO.MemoryStream Ms = new MemoryStream();
_image.Save(Ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imgdata = new byte[Ms.Length];
Ms.Position = 0;
Ms.Read(imgdata, 0, Convert.ToInt32(Ms.Length));
Ms.Close();
return imgdata;
}
string Pdfpath = "";
/// <summary>
/// 选择需要转换的PDF
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
Pdfpath = openFileDialog1.FileName;
}
else
{
Pdfpath = "";
}
textBox1.Text = Pdfpath;
}
/// <summary>
/// 转换
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Pdfpath))
{
GetImage(Pdfpath,2);
}
}
}
}
源码下载:http://u.163.com/8MJPw5bT 提取码:pe8y8a35