pdf 转图片
说明:本方法 用acrobat 的官方接口 需要安装 acrobat professional 8.0或更高版本
然后再vs中添加com组件 abode acrobat 版本号 type library 只有安装了上面软件 才回有
在web页 头部 添加 AspCompat="true" 控制台程序 要在main函数上加 [STAThread] 保证单线程模型下 才能访问 剪贴板
using System.IO;
using System.Reflection;
using DataHelp;
using System.Text;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
///
/// Pdf导图片
///
///
///
///
///
public string PDFToPic(string pdfInputPathDirectory, string pngOutputPath, ImageFormat format)
{
StringBuilder b = new StringBuilder();
ImageFormat formate = format;
// Acrobat objects
Acrobat.CAcroPDDoc pdfDoc = null;
Acrobat.CAcroPDPage pdfPage = null;
Acrobat.CAcroRect pdfRect = null;
Acrobat.CAcroPoint pdfPoint = null;
try
{
string[] files = new string[] { pdfInputPathDirectory };
string dic = Path.GetDirectoryName(pdfInputPathDirectory);
//string[] files = Directory.GetFiles(dic, "*.pdf");
for (int n = 0; n
{
string inputFile = files[n].ToString();
string fileName = files[n].Substring(files[n].LastIndexOf(@"\") + 1).Replace(".pdf", "");
// Will always be available as .NET framework ships with all
pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");
bool ret = pdfDoc.Open(inputFile);
if (!ret)
{
throw new FileNotFoundException();
}
else
{
b = b.AppendLine("pdfDoc成功打开文件!!!");
}
// Get the number of pages (to be used later if you wanted to store that information)
int pageCount = pdfDoc.GetNumPages();
b = b.AppendLine("
- ");
for (int i = 0; i
{
// Get the first page
pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i);
pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();
b = b.AppendLine("
pdfPoint.x=" + pdfPoint.x + ";pdfPoint.y=" + pdfPoint.y + " ");pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", "");
pdfRect.Left = 0;
pdfRect.right = pdfPoint.x;
pdfRect.Top = 0;
pdfRect.bottom = pdfPoint.y;
pdfPage.CopyToClipboard(pdfRect, 0, 0, 100);
System.Windows.Forms.IDataObject clipboardData = Clipboard.GetDataObject();
bool c = false;
if (clipboardData!=null )
{
c = true;
}
b = b.AppendLine("
Clipboard.GetDataObject()=" + c + "剪贴板中是否有数据; ");b = b.AppendLine("
clipboardData.GetDataPresent(DataFormats.Bitmap)=" + clipboardData.GetDataPresent(DataFormats.Bitmap) + "; ");if (clipboardData.GetDataPresent(DataFormats.Bitmap))
{
Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);
b = b.AppendLine("
生成图片的大小: pdfBitmap.Size.Height=" + pdfBitmap.Size.Height + "; pdfBitmap.Size.Width=" + pdfBitmap.Size.Width + " ");string dPath = Path.Combine(pngOutputPath, i.ToString("0000") + "." + formate.ToString());
b = b.AppendLine("
生成图片的路径:" + dPath + "; ");pdfBitmap.Save(dPath, formate);
pdfBitmap.Dispose();
}
b = b.AppendLine("
第" + (i + 1) + "页");}
pdfDoc.Close();
Marshal.ReleaseComObject(pdfPage);
Marshal.ReleaseComObject(pdfRect);
Marshal.ReleaseComObject(pdfDoc);
}
b = b.AppendLine("
");}
catch (System.Exception ex)
{
Response.Write(ex.ToString());
}
return b.ToString();
}