C#将PDF文件转换成JPG文件

C#和Java的现成的程序都有,用C++的有,是调用Acrobat中的文件自己编写的,但不是生成图片,只是用VC显示,网址是:
http://www.evget.com/zh-CN/Info/catalog/6594.html
有个用C#实现的网址是:
http://www.codeproject.com/KB/cs/GhostScriptUseWithCSharp.aspx
这里面有具体的步骤及如何调用别人的库,但C++使用起来不太好办。
这里有一个pdflib的操作,可以操作页,也是C#的:
http://www.codeproject.com/KB/graphics/AnotherPDFLib.aspx
下面还有用C++如何调用C#的:

http://blog.csdn.net/suoxd123/archive/2010/01/08/5157668.aspx


下面提供一个完整的事例代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Acrobat;

namespace PDFLibSharp
{
    public class PDFLibSharp
    {
        private string filename;
        int page;

        /// <summary>
        /// the construct function to create the filename and page
        /// </summary>
        /// <param name="filenamesource"></param>
        /// <param name="pagesource"></param>       
        public int ConvertPtoI(string filenamesource, int pagesource)
        {
            filename = filenamesource;
            page = pagesource;
            //assert the file is pdf or not
            if(!(filename.EndsWith(".pdf") || filename.EndsWith(".PDF")))
            {
                return 0;  //0 represent there is no file
            }               

            //begin to change
            try
            {
                Acrobat.CAcroPDDoc pdfDoc;
                pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");
                bool ret = pdfDoc.Open(filename);
                if(!ret)
                {
                    return 1;  //1 can not open to read
                }

                //the page is too big
                int pageCount = pdfDoc.GetNumPages();
                if(page >= pageCount || page <= 0)
                {
                    return 2;
                }

                //get the page and the size
                Acrobat.CAcroPDPage pdfPage;
                pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(page);
                Acrobat.CAcroPoint pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();
                Acrobat.CAcroRect 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);
                IDataObject clipboardData = Clipboard.GetDataObject();

                if (clipboardData.GetDataPresent(DataFormats.Bitmap))
                {
                    Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);

                    int thumbnailWidth = 1000;
                    int thumbnailHeight = 1000;
                 
                    Image pdfImage = pdfBitmap.GetThumbnailImage(thumbnailWidth, thumbnailHeight,null, IntPtr.Zero);
 
                    Bitmap thumbnailBitmap = new Bitmap(thumbnailWidth + 7, thumbnailHeight + 7,System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                    using (Graphics thumbnailGraphics = Graphics.FromImage(thumbnailBitmap))
                    {
                        thumbnailGraphics.DrawImage(pdfImage, 2, 2, thumbnailWidth, thumbnailHeight);
                        string outputFile = filename.Substring(0, filename.Length - 4) + '_' + page.ToString() + ".png";
                        thumbnailBitmap.Save(outputFile, System.Drawing.Imaging.ImageFormat.Png);
                        Console.WriteLine("Generated thumbnail... {0}", outputFile);
                    }
                    pdfDoc.Close();

                    Marshal.ReleaseComObject(pdfPage);
                    Marshal.ReleaseComObject(pdfRect);
                    Marshal.ReleaseComObject(pdfDoc);
                }

            }
            catch (System.Exception ex)
            {
                return 10;
            }
            return 5;
           
        }
    }
}



你可以使用C#中的一些库和工具来实现PDFJPG的功能。下面是一个简单的示例,使用iTextSharp库将PDF换为JPG: ```csharp using System; using System.Drawing; using System.Drawing.Imaging; using iTextSharp.text.pdf; class Program { static void Main(string[] args) { string pdfFilePath = "path_to_your_pdf_file.pdf"; string outputImagePath = "path_to_output_jpg_file.jpg"; // 使用iTextSharp库打开PDF文件 PdfReader reader = new PdfReader(pdfFilePath); // 获取PDF的第一页 PdfDictionary page = reader.GetPageN(1); // 获取页面的宽度和高度 int width = (int)page.GetAsNumber(PdfName.WIDTH).Value; int height = (int)page.GetAsNumber(PdfName.HEIGHT).Value; // 创建一个与页面大小相同的Bitmap对象 Bitmap bitmap = new Bitmap(width, height); // 创建一个用于绘制图像的Graphics对象 Graphics graphics = Graphics.FromImage(bitmap); // 使用iTextSharp库将PDF页面绘制到Graphics对象上 iTextSharp.text.Image pdfImage = iTextSharp.text.Image.GetInstance(page); pdfImage.ScaleToFit(width, height); graphics.DrawImage(pdfImage, 0, 0); // 保存Bitmap对象为JPG图像文件 bitmap.Save(outputImagePath, ImageFormat.Jpeg); // 清理资源 reader.Close(); graphics.Dispose(); bitmap.Dispose(); Console.WriteLine("PDF换为JPG完成!"); } } ``` 请确保你已经将iTextSharp库添加到你的项目中。这个示例将PDF的第一页换为JPG图像文件。你需要将`pdfFilePath`替换为你的PDF文件路径,`outputImagePath`替换为你希望保存换后JPG的路径。运行代码后,你将在指定的输出路径中找到换后的JPG文件。 希望这对你有帮助!如果你有任何其他问题,请随时问我。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值