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#将PDF文件转换成JPG文件)