Categories: C#

C#.Net PDF convert to Image

開始之前,需要安裝 Adobe Reader X Pro 以上的版本,安裝完成後去資料夾底下搜尋『Acrobat.dll

開啟Visual Studio專案,並將『Acrobat.dll』加入參考

接著加入『Microsoft.VisualBasic

將以下程式碼貼上去就可以使用了,註解都幫各位寫好了,想知道原理看參考文獻或是註解文字即可

使用程式下載或是Github必須要有『Acrobat.dll』,否則會出現找不到dll錯誤

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace PDFConvertImage
{
    public partial class PDFtoImage : Form
    {
        public PDFtoImage()
        {
            InitializeComponent();
        }

        private void button_LoadFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.InitialDirectory = getDeskPath();
            dialog.Filter = "PDF|*.pdf";
            dialog.RestoreDirectory = true;

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                //倍數
                short pixelZoom = 10;

                //http://www.cnblogs.com/kongxianghai/archive/2012/04/09/2438321.html
                //頁
                Acrobat.CAcroPDPage pdfPage = null;

                //頁面大小
                Acrobat.CAcroPoint pdfPoint = null;

                //矩形
                Acrobat.CAcroRect pdfRect = null;

                //http://forums.codeguru.com/showthread.php?31751-What-do-I-need-for-CreateObject(-AcroExch.PDDoc-)
                Acrobat.CAcroPDDoc pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");
                if (pdfDoc.Open(dialog.FileName))
                {
                    //PDF頁數
                    int pageCount = pdfDoc.GetNumPages();
                    for (int i = 0; i < pageCount; i++)
                    {
                        //取得目前頁數
                        pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i);

                        //取得頁面大小
                        pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();

                        //http://forums.adobe.com/thread/305835
                        pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", "");

                        //左邊
                        pdfRect.Left = 0;

                        //頂端
                        pdfRect.Top = 0;

                        //右邊
                        pdfRect.right = (short)(pdfPoint.x * pixelZoom);

                        //底部
                        pdfRect.bottom = (short)(pdfPoint.y * pixelZoom);

                        //取得整個頁面
                        pdfPage.CopyToClipboard(pdfRect, (short)(pdfRect.Left * pixelZoom), (short)(pdfRect.Top * pixelZoom), (short)(100 * pixelZoom));

                        ///<summary>http://msdn.microsoft.com/en-us/library/system.windows.forms.idataobject.aspx
                        ///You retrieve stored data from an IDataObject by calling the GetData method and specifying the data format in the format parameter. Set the  autoConvert parameter to false to retrieve only data that was stored in the specified format. To convert the stored data to the specified format, set autoConvert to true, or do not use autoConvert.
                        ///</summary>
                        IDataObject loClipboardData = Clipboard.GetDataObject();

                        //Get Image
                        Bitmap pdfBitmap = (Bitmap)loClipboardData.GetData(DataFormats.Bitmap);

                        //Save Image
                        pdfBitmap.Save(getDeskPath() + (i + 1) + ".jpeg", ImageFormat.Jpeg);
                    }
                    pdfDoc.Close();

                    ///<summary>http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.releasecomobject.aspx
                    ///This method is used to explicitly control the lifetime of a COM object used from managed code. You should use this method to free the underlying COM object that holds references to resources in a timely manner or when objects must be freed in a specific order.
                    ///</summary>
                    Marshal.ReleaseComObject(pdfPage);
                    Marshal.ReleaseComObject(pdfRect);
                    Marshal.ReleaseComObject(pdfDoc);
                    MessageBox.Show("轉換成功");
                }
                else
                {
                    MessageBox.Show("檔案可能損毀了");
                }
            }
        }

        //取得桌面路徑
        private static string getDeskPath()
        {
            return System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\";
        }

        private void PDFtoImage_Load(object sender, EventArgs e)
        {
            this.MaximizeBox = false;
            this.MinimizeBox = false;
        }
    }
}

參考資料:

s47548081

Share
Published by
s47548081

Recent Posts

[教學] 打造你的 NFT 智能合約 – ERC721A

GM!前些日子在幣圈亂玩,一路...

2 年 ago

JavaScript – Singleton 設計模式

前言 在設計程式時,我們有時會...

4 年 ago

PlaidML 讓你的 Mac 也能加速 Tensorflow 機器學習!

相信很多使用 Mac 或者手上...

4 年 ago

RESTful API 測試很煩,只好動手寫屬於自己的測試了

寫在最前面 嗨,大家好久不見!...

4 年 ago

Node.js 與 Socket.io – 即時聊天室實作:資料庫

經過前兩篇(一、二)文章,我們...

7 年 ago