C# PDFLibNet PDF轉JPG

 

直接上代碼app

using System.Drawing.Imaging;
using System.IO;
using PDFLibNet;

namespace WindowsFormsApp1
{
    class PDFjpg
    { 

            public enum Definition
            {
                One = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 10
            }

            /// <summary>
            /// 將PDF文檔轉換爲圖片的方法
            /// </summary>
            /// <param name="pdfInputPath">PDF文件路徑</param>
            /// <param name="imageOutputPath">圖片輸出路徑</param>
            /// <param name="imageName">生成圖片的名字</param>
            /// <param name="startPageNum">從PDF文檔的第幾頁開始轉換</param>
            /// <param name="endPageNum">從PDF文檔的第幾頁開始中止轉換</param>
            /// <param name="imageFormat">設置所需圖片格式</param>
            /// <param name="definition">設置圖片的清晰度,數字越大越清晰</param>
            public static void ConvertPDF2Image(string pdfInputPath, string imageOutputPath,
                string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition)
            {
                PDFWrapper pdfWrapper = new PDFWrapper();

                pdfWrapper.LoadPDF(pdfInputPath);

                if (!System.IO.Directory.Exists(imageOutputPath))
                {
                    Directory.CreateDirectory(imageOutputPath);
                }

                // validate pageNum
                if (startPageNum <= 0)
                {
                    startPageNum = 1;
                }

                if (endPageNum > pdfWrapper.PageCount)
                {
                    endPageNum = pdfWrapper.PageCount;
                }

                if (startPageNum > endPageNum)
                {
                    int tempPageNum = startPageNum;
                    startPageNum = endPageNum;
                    endPageNum = startPageNum;
                }

                // start to convert each page
                for (int i = startPageNum; i <= endPageNum; i++)
                {
                    pdfWrapper.ExportJpg(imageOutputPath + imageName + i.ToString() + ".jpg", i, i, 180, 80);//這裏能夠設置輸出圖片的頁數、大小和圖片質量
                    if (pdfWrapper.IsJpgBusy) { System.Threading.Thread.Sleep(100); }
                }

                pdfWrapper.Dispose();
            }
         
             

    }
}

 

 

            PDFjpg.ConvertPDF2Image("E:\\download\\search.pdf", "E:\\download", "test", 1, 5, ImageFormat.Jpeg, PDFjpg.Definition.One);
相關文章
相關標籤/搜索