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

参考代码:

复制代码
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;
            //1)       //     生成操作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++)
            {
                //2)               
                //  取出当前页
                pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i - 1);
                //3)                
                //    得到当前页的大小               
                pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();
                // 生成一个页的裁剪区矩形对象            
                pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", "");
                // 计算当前页经缩放后的实际宽度和高度,zoom==1时,保持原比例大小   
                int imgWidth = (int)((double)pdfPoint.x * zoom); int imgHeight = (int)((double)pdfPoint.y * zoom);
                //设置裁剪矩形的大小为当前页的大小             
                pdfRect.Left = 0; pdfRect.right = (short)imgWidth; pdfRect.Top = 0; pdfRect.bottom = (short)imgHeight;
                //4)                
                //    将当前页的裁剪区的内容编成图片后复制到剪贴板中            
                pdfPage.CopyToClipboard(pdfRect, 0, 0, (short)(100 * zoom));
                // 5)             
                IDataObject clipboardData = Clipboard.GetDataObject();
                //检查剪贴板中的对象是否是图片,如果是图片则将其保存为指定格式的图片文件   
                if (clipboardData.GetDataPresent(DataFormats.Bitmap))
                {
                    Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);
                    pdfBitmap.Save(Path.Combine(imageDirectoryPath, i.ToString("0000") + "." + format.ToString()), format);
                    pdfBitmap.Dispose();
                }

            }
            // 关闭和释放相关COM对象 
            pdfDoc.Close(); Marshal.ReleaseComObject(pdfRect);
            Marshal.ReleaseComObject(pdfPoint);
            Marshal.ReleaseComObject(pdfPage);
            Marshal.ReleaseComObject(pdfDoc);
        }

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

相关推荐
拓端研究室2 小时前
专题:2025AI产业全景洞察报告:企业应用、技术突破与市场机遇|附920+份报告PDF、数据、可视化模板汇总下载
大数据·人工智能·pdf
南风微微吹2 小时前
2026年新大纲普通话考试真题题库50套PDF电子版
pdf·普通话
矶鹬笛手5 小时前
(2.1) 信息技术及其发展
sql·计算机网络·c#
u***27616 小时前
C#数据库操作系列---SqlSugar完结篇
网络·数据库·c#
笑非不退7 小时前
C# c++ 实现程序开机自启动
开发语言·c++·c#
JHC0000009 小时前
Python PDF 相关操作
开发语言·python·pdf
周杰伦fans10 小时前
[特殊字符] 代理模式超详细讲解 ——.NET
数据库·c#·代理模式
用户83562907805111 小时前
C# 高效生成 Word 表格:复杂表格创建实战指南
后端·c#
屠夫11 小时前
C# LINQ
c#
光头闪亮亮13 小时前
电子发票解析工具-c#桌面应用开发-DataGridView表格控件使用详解
c#