C# Winform 使用Adobe Acrobat 實現 PDF 轉 圖片


以前也介紹過PDF打印圖片的方法。但是發現了一個問題。用戶使用的PDF文件未必都是標准的PDF文件。對於不標准的PDF文件。轉換就會失敗了。轉換出來的圖片,空白亂碼等效果。研究了幾種方法后。覺得還是使用adobe官方的插件來實現吧。所以我選擇了使用Acrobat。

需要准備的:下載Adobe Acrobat Professional 9

添加引用:Microsoft.CSharp.dll,Acrobat.dll 和Microsoft.VisualBasic.dll

 

/// <summary>
/// 將PDF文檔轉換為圖片的方法,你可以像這樣調用該方法:ConvertPDF2Image("F:\\A.pdf", "F:\\", "A", 0, 0, null, 0);
/// 因為大多數的參數都有默認值,startPageNum默認值為1,endPageNum默認值為總頁數,
/// imageFormat默認值為ImageFormat.Jpeg,resolution默認值為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(string pdfInputPath, string imageOutputPath,
string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, double resolution)
{
    Acrobat.CAcroPDDoc pdfDoc = null;
    Acrobat.CAcroPDPage pdfPage = null;
    Acrobat.CAcroRect pdfRect = null;
    Acrobat.CAcroPoint pdfPoint = null;

    // Create the document (Can only create the AcroExch.PDDoc object using late-binding)
    // Note using VisualBasic helper functions, have to add reference to DLL
    pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");

    // validate parameter
    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.Jpeg; }
    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, better for us to scale in .NET
        // than Acrobat as it would greek out small text
        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) + ".jpg", imageFormat);
            pdfBitmap.Dispose();
        }
    }

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM