PDF文档转为图片的方法

本文介绍了一种使用PDFBox、Interop.Acrobat和Microsoft.Office.Interop.Word库将PDF文档转换为图片的方法。该方法提供了自定义参数,如起始页数、结束页数、图片格式和分辨率,确保了灵活的转换过程。通过步骤演示,读者可以轻松掌握从PDF到图片的高效转换技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PDT文档转换为图片的方法:

需要PDFBox-0.7.3.dll,Interop.Acrobat.dll,Microsoft.Office.Interop.Word.dll

 

///PDF文档转换为图片的方法

        ///因为大多数的参数都有默认值startPageNum默认值为1endPageNum默认值为总页数

        /// imageFormat默认值为aImageFormat.Jpegresolution默认值为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);

        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值