• [C#]用acrobat将pdf转成图片时候到剪贴板Bitmap始终是null


    参考代码

    1. public static void ConvertPdf2Image(string pdfFilePath, string imageDirectoryPath, int beginPageNum, int endPageNum, ImageFormat format, double zoom = 1)
    2. {
    3. Acrobat.CAcroPDDoc pdfDoc = null; Acrobat.CAcroPDPage pdfPage = null; Acrobat.CAcroRect pdfRect = null; Acrobat.CAcroPoint pdfPoint = null;
    4. //1) // 生成操作Pdf文件的Com对象
    5. pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");
    6. // 检查输入参数
    7. if (!pdfDoc.Open(pdfFilePath)) { throw new FileNotFoundException(string.Format("源文件{0}不存在!", pdfFilePath)); }
    8. if (!Directory.Exists(imageDirectoryPath)) { Directory.CreateDirectory(imageDirectoryPath); }
    9. if (beginPageNum <= 0) { beginPageNum = 1; }
    10. if (endPageNum > pdfDoc.GetNumPages() || endPageNum <= 0) { endPageNum = pdfDoc.GetNumPages(); }
    11. if (beginPageNum > endPageNum) { throw new ArgumentException("参数\"beginPageNum\"必须小于\"endPageNum\"!"); }
    12. if (format == null) { format = ImageFormat.Png; }
    13. if (zoom <= 0) { zoom = 1; }
    14. //转换
    15. for (int i = beginPageNum; i <= endPageNum; i++)
    16. {
    17. //2)
    18. // 取出当前页
    19. pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i - 1);
    20. //3)
    21. // 得到当前页的大小
    22. pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();
    23. // 生成一个页的裁剪区矩形对象
    24. pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", "");
    25. // 计算当前页经缩放后的实际宽度和高度,zoom==1时,保持原比例大小
    26. int imgWidth = (int)((double)pdfPoint.x * zoom); int imgHeight = (int)((double)pdfPoint.y * zoom);
    27. //设置裁剪矩形的大小为当前页的大小
    28. pdfRect.Left = 0; pdfRect.right = (short)imgWidth; pdfRect.Top = 0; pdfRect.bottom = (short)imgHeight;
    29. //4)
    30. // 将当前页的裁剪区的内容编成图片后复制到剪贴板中
    31. pdfPage.CopyToClipboard(pdfRect, 0, 0, (short)(100 * zoom));
    32. // 5)
    33. IDataObject clipboardData = Clipboard.GetDataObject();
    34. //检查剪贴板中的对象是否是图片,如果是图片则将其保存为指定格式的图片文件
    35. if (clipboardData.GetDataPresent(DataFormats.Bitmap))
    36. {
    37. Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);
    38. pdfBitmap.Save(Path.Combine(imageDirectoryPath, i.ToString("0000") + "." + format.ToString()), format);
    39. pdfBitmap.Dispose();
    40. }
    41. }
    42. // 关闭和释放相关COM对象
    43. pdfDoc.Close(); Marshal.ReleaseComObject(pdfRect);
    44. Marshal.ReleaseComObject(pdfPoint);
    45. Marshal.ReleaseComObject(pdfPage);
    46. Marshal.ReleaseComObject(pdfDoc);
    47. }

    但是发现始终获取不到图像,原因是acrobat DC好像不行,clipboardData.GetDataPresent(DataFormats.Bitmap)始终返回false。因此需要安装acrobat XI或者acrobat 9.0才行

  • 相关阅读:
    黄东旭:开发者的“技术无感化”时代,从 Serverless HTAP 数据库开始 | PingCAP DevCon 2022
    【货干】IP 配置出现意外。
    M1 Mac创建虚拟环境遇到的问题
    Docker 搭建 Jenkins 实现自动部署
    LVGL---进度条(lv_bar)
    【数据结构-树】哈夫曼树及其应用
    jfreechart后台生成图片采样完美解决方案以及样式美化
    Hystrix熔断器整合 - Feign实现服务容错
    oracle高级—索引
    Mac通过brew安装PostgreSQL保姆级实践步骤
  • 原文地址:https://blog.csdn.net/FL1623863129/article/details/133886791