资源简介
采用Adobe Acrobat9.0的COM组件,将Pdf文件的每一页转换成对应的图片文件
代码片段和文件信息
using System;
using System.Linq;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Diagnostics;
namespace Pdf2Image {
public class Program {
[STAThread]
public static void Main(string[] args) {
//args.ToList().ForEach(p => Console.WriteLine(p));
//return;
if (args.Length != 1) {
Console.WriteLine(“命令行格式:Pdf2Image “);
return;
}
string pdfFilePath = args[0];
if(!System.IO.File.Exists(pdfFilePath))
{
Console.WriteLine(“文件\“{0}\“不存在“ pdfFilePath);
return;
}
FileInfo pdfFi = new FileInfo(pdfFilePath);
pdfFilePath = pdfFi.FullName;
string imageDirectoryPath = System.IO.Path.Combine(
pdfFi.DirectoryName
pdfFi.Name.Replace(pdfFi.Extension““));
try {
ConvertPdf2Image(pdfFilePath imageDirectoryPath 0 0 null 1);
}
catch (Exception ex) {
Console.WriteLine(ex.ToString());
}
Console.Read();
}
public static void ConvertPdf2Image(string pdfFilePath string imageDirectoryPath
int beginPageNum int endPageNum ImageFormat format double zoom = 1) {
Acrobat.CAcroPDDoc pdfDoc = null;
Acrobat.CAcroPDPage pdfPage = null;
Acrobat.CAcroRect pdfRect = null;
Acrobat.CAcroPoint pdfPoint = null;
//生成操作Pdf文件的Com对象
pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.Createobject(“AcroExch.PDDoc“ ““);
//检查输入参数
if (!pdfDoc.Open(pdfFilePath)) {
throw new FileNotFoundException(string.Format(“源文件{0}不存在!“ pdfFilePath));
}
if (!Directory.Exists(imageDirectoryPath)) {
Directory.CreateDirectory(imageDirectoryPath);
}
if (beginPageNum <= 0) {
beginPageNum = 1;
}
if (endPageNum > pdfDoc.GetNumPages() || endPageNum <= 0) {
endPageNum = pdfDoc.GetNumPages();
}
if (beginPageNum > endPageNum) {
throw new ArgumentException(“参数\“beginPageNum\“必须小于\“endPageNum\“!“);
}
if (format == null) {
format = ImageFormat.Png;
}
if (zoom <= 0) {
zoom = 1;
}
//转换
for (int i = beginPageNum; i <= endPageNum; i++) {
//取出当前页
pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i - 1);
//得到当前页的大小
pdfPoint = (Acrobat.CAcroPoint)pdfPa
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 907 2012-04-08 23:21 Pdf2ImageWithAcrobat\Acrobat.pdf2image.sln
..A..H. 20480 2012-04-08 23:49 Pdf2ImageWithAcrobat\Acrobat.pdf2image.suo
文件 144 2012-04-08 19:09 Pdf2ImageWithAcrobat\app.config
文件 4815 2012-04-08 23:48 Pdf2ImageWithAcrobat\Pdf2Image.csproj
文件 599 2012-04-08 23:33 Pdf2ImageWithAcrobat\Pdf2Image.csproj.user
文件 4853 2012-04-08 23:43 Pdf2ImageWithAcrobat\Program.cs
文件 1349 2012-04-08 23:21 Pdf2ImageWithAcrobat\Properties\AssemblyInfo.cs
目录 0 2012-04-08 23:50 Pdf2ImageWithAcrobat\Properties
目录 0 2012-04-08 23:50 Pdf2ImageWithAcrobat
----------- --------- ---------- ----- ----
33147 9
评论
共有 条评论