PDF文档转为图片的方法
2012-12-10 16:09
232 查看
PDT文档转换为图片的方法:
需要PDFBox-0.7.3.dll,Interop.Acrobat.dll,Microsoft.Office.Interop.Word.dll
///将PDF文档转换为图片的方法
///因为大多数的参数都有默认值startPageNum默认值为1,endPageNum默认值为总页数
/// imageFormat默认值为aImageFormat.Jpeg,resolution默认值为1
///</summary>
///<param name="pdfInputPath">PDF文件路径</param>
///<param name="imageOutputPath">图片输出路径</param>
///<param name="imageName">图片的名字,不需要带扩展名</param>
///<param name="startPageNum">从PDF文档的第几页开始转换默认值为1</param>
///<param name="endPageNum">从PDF文档的第几页开始停止转换,默认值为PDF总页数</param>
///<param name="imageFormat">设置所需图片格式</param>
///<param name="resolution">设置图片的分辨率,数字越大越清晰,默认值为1</param>
public
static void ConvertPDF2Image(stringpdfInputPath,string imageOutputPath,
string imageName,
intstartPageNum, int endPageNum,
ImageFormat imageFormat, doubleresolution)
{
Acrobat.CAcroPDDoc pdfDoc =null;
Acrobat.CAcroPDPage pdfPage =null;
Acrobat.CAcroRect pdfRect =null;
Acrobat.CAcroPoint pdfPoint =null;
// Create the document (Can only create the AcroExch.PDDocobject using late-binding)
// Note using VisualBasic helper functions, have to addreference to DLL
pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc","");
// validateparameter
if (!pdfDoc.Open(pdfInputPath)) {throw
new FileNotFoundException(); }
if (!Directory.Exists(imageOutputPath)){Directory.CreateDirectory(imageOutputPath);}
if (startPageNum <= 0) { startPageNum = 1; }
if (endPageNum > pdfDoc.GetNumPages() ||endPageNum <= 0) { endPageNum = pdfDoc.GetNumPages(); }
if (startPageNum > endPageNum) {int tempPageNum = startPageNum; startPageNum =endPageNum; endPageNum = startPageNum; }
if (imageFormat ==
null){ imageFormat = ImageFormat.Bmp; }
if (resolution <= 0) { resolution =1; }
// start to convert each page
for (int i =startPageNum; i <= endPageNum; i++)
{
pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i- 1);
pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();
pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect","");
int imgWidth = (int)((double)pdfPoint.x * resolution);
int imgHeight = (int)((double)pdfPoint.y * resolution);
pdfRect.Left = 0;
pdfRect.right = (short)imgWidth;
pdfRect.Top = 0;
pdfRect.bottom = (short)imgHeight;
// Render to clipboard, scaled by 100 percent(ie. original size)
// Even though we want a smaller image, betterfor us to scale in .NET
// than Acrobat as it would greek out smalltext
pdfPage.CopyToClipboard(pdfRect, 0, 0, (short)(100* resolution));
IDataObject clipboardData =Clipboard.GetDataObject();
if (clipboardData.GetDataPresent(DataFormats.Bitmap))
{
Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);
pdfBitmap.Save(Path.Combine(imageOutputPath,imageName) + i.ToString() +"." +imageFormat.ToString(), imageFormat);
pdfBitmap.Dispose();
}
// Application.DoEvents();
}
pdfDoc.Close();
Marshal.ReleaseComObject(pdfPage);
Marshal.ReleaseComObject(pdfRect);
Marshal.ReleaseComObject(pdfDoc);
Marshal.ReleaseComObject(pdfPoint);
}
需要PDFBox-0.7.3.dll,Interop.Acrobat.dll,Microsoft.Office.Interop.Word.dll
///将PDF文档转换为图片的方法
///因为大多数的参数都有默认值startPageNum默认值为1,endPageNum默认值为总页数
/// imageFormat默认值为aImageFormat.Jpeg,resolution默认值为1
///</summary>
///<param name="pdfInputPath">PDF文件路径</param>
///<param name="imageOutputPath">图片输出路径</param>
///<param name="imageName">图片的名字,不需要带扩展名</param>
///<param name="startPageNum">从PDF文档的第几页开始转换默认值为1</param>
///<param name="endPageNum">从PDF文档的第几页开始停止转换,默认值为PDF总页数</param>
///<param name="imageFormat">设置所需图片格式</param>
///<param name="resolution">设置图片的分辨率,数字越大越清晰,默认值为1</param>
public
static void ConvertPDF2Image(stringpdfInputPath,string imageOutputPath,
string imageName,
intstartPageNum, int endPageNum,
ImageFormat imageFormat, doubleresolution)
{
Acrobat.CAcroPDDoc pdfDoc =null;
Acrobat.CAcroPDPage pdfPage =null;
Acrobat.CAcroRect pdfRect =null;
Acrobat.CAcroPoint pdfPoint =null;
// Create the document (Can only create the AcroExch.PDDocobject using late-binding)
// Note using VisualBasic helper functions, have to addreference to DLL
pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc","");
// validateparameter
if (!pdfDoc.Open(pdfInputPath)) {throw
new FileNotFoundException(); }
if (!Directory.Exists(imageOutputPath)){Directory.CreateDirectory(imageOutputPath);}
if (startPageNum <= 0) { startPageNum = 1; }
if (endPageNum > pdfDoc.GetNumPages() ||endPageNum <= 0) { endPageNum = pdfDoc.GetNumPages(); }
if (startPageNum > endPageNum) {int tempPageNum = startPageNum; startPageNum =endPageNum; endPageNum = startPageNum; }
if (imageFormat ==
null){ imageFormat = ImageFormat.Bmp; }
if (resolution <= 0) { resolution =1; }
// start to convert each page
for (int i =startPageNum; i <= endPageNum; i++)
{
pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i- 1);
pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();
pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect","");
int imgWidth = (int)((double)pdfPoint.x * resolution);
int imgHeight = (int)((double)pdfPoint.y * resolution);
pdfRect.Left = 0;
pdfRect.right = (short)imgWidth;
pdfRect.Top = 0;
pdfRect.bottom = (short)imgHeight;
// Render to clipboard, scaled by 100 percent(ie. original size)
// Even though we want a smaller image, betterfor us to scale in .NET
// than Acrobat as it would greek out smalltext
pdfPage.CopyToClipboard(pdfRect, 0, 0, (short)(100* resolution));
IDataObject clipboardData =Clipboard.GetDataObject();
if (clipboardData.GetDataPresent(DataFormats.Bitmap))
{
Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);
pdfBitmap.Save(Path.Combine(imageOutputPath,imageName) + i.ToString() +"." +imageFormat.ToString(), imageFormat);
pdfBitmap.Dispose();
}
// Application.DoEvents();
}
pdfDoc.Close();
Marshal.ReleaseComObject(pdfPage);
Marshal.ReleaseComObject(pdfRect);
Marshal.ReleaseComObject(pdfDoc);
Marshal.ReleaseComObject(pdfPoint);
}
相关文章推荐
- 将图片转为PDF文档的方法
- 怎样将文档转为图片-pdf转jpg的方法
- 各种电子图书馆文章(PDF、PDG、CAJ)转为Word文档的方法
- ppt转为pdf的两种文档转换方法
- 将PDF和图片文件如何转为Word或Excel分享几种转换器软件方法
- C# PDF转为图片的方法
- ppt转为pdf文档格式转换的方法讲解
- 如何将pdf文档内容转为图片
- 实现pdf图片格式转换成txt文档的方法
- OpenOffice 将word文档转为pdf再转为图片
- 将整个pdf文档转换成jpg图片的最快方法
- 将pdf文档内容转换成jpg图片的方法
- Nodejs中使用phantom将html转为pdf或图片格式的方法
- 将图片转换成PDF格式的方法讲解
- 转:C#实现office文档转换为PDF或xps的一些方法
- 利用canvas中toDataURL()将图片转为dataURL(base64)的方法详解
- 分享在线pdf转换成图片格式的方法
- PDF文档如何添加图片签名
- PDF文档如何添加图片签名
- 叫你两招编辑PDF文档的方法