C# Winform 使用Adobe Acrobat 实现 PDF 转 图片

以前也介绍过PDF打印图片的方法。但是发现了一个问题。用户使用的PDF文件未必都是标准的PDF文件。对于不标准的PDF文件。转换就会失败了。转换出来的图片,空白乱码等效果。研究了几种方法后。觉得还是使用adobe官方的插件来实现吧。所以我选择了使用Acrobat。

需要准备的:下载Adobe Acrobat Professional 9

添加引用:Microsoft.CSharp.dll,Acrobat.dll 和Microsoft.VisualBasic.dll

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/// <summary>
/// 将PDF文档转换为图片的方法,你可以像这样调用该方法:ConvertPDF2Image("F:\\A.pdf", "F:\\", "A", 0, 0, null, 0);
/// 因为大多数的参数都有默认值,startPageNum默认值为1,endPageNum默认值为总页数,
/// imageFormat默认值为ImageFormat.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(string pdfInputPath, string imageOutputPath,
string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, double resolution)
{
    Acrobat.CAcroPDDoc pdfDoc = null;
    Acrobat.CAcroPDPage pdfPage = null;
    Acrobat.CAcroRect pdfRect = null;
    Acrobat.CAcroPoint pdfPoint = null;
 
    // Create the document (Can only create the AcroExch.PDDoc object using late-binding)
    // Note using VisualBasic helper functions, have to add reference to DLL
    pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");
 
    // validate parameter
    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.Jpeg; }
    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, better for us to scale in .NET
        // than Acrobat as it would greek out small text
        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) + ".jpg", imageFormat);
            pdfBitmap.Dispose();
        }
    }
 
    pdfDoc.Close();
    Marshal.ReleaseComObject(pdfPage);
    Marshal.ReleaseComObject(pdfRect);
    Marshal.ReleaseComObject(pdfDoc);
    Marshal.ReleaseComObject(pdfPoint);
}

 

posted @   仰望 星空  阅读(6666)  评论(1编辑  收藏  举报
编辑推荐:
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
阅读排行:
· 用 DeepSeek 给对象做个网站,她一定感动坏了
· DeepSeek+PageAssist实现本地大模型联网
· 手把手教你更优雅的享受 DeepSeek
· 腾讯元宝接入 DeepSeek R1 模型,支持深度思考 + 联网搜索,好用不卡机!
· 从 14 秒到 1 秒:MySQL DDL 性能优化实战
点击右上角即可分享
微信分享提示