2019c#將PDF轉圖片

兩種方法:.net

第一種是用O2S.Components.PDFRender4NET 你們能夠去網上查找無水印版本 可是有的時候帶顏色的字就變空白了 不知道爲何orm

第二種是用PdfiumViewer 這種方法帶顏色的字也能夠轉換 推薦使用blog

下面貼代碼圖片

public class PDFHelp
    {
        /// <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, int definition)
        {
            PDFFile pdfFile = PDFFile.Open(pdfInputPath);
            if (!Directory.Exists(imageOutputPath))
            {
                Directory.CreateDirectory(imageOutputPath);
            }
            // validate pageNum
            if (startPageNum <= 0)
            {
                startPageNum = 1;
            }
            if (endPageNum > pdfFile.PageCount)
            {
                endPageNum = pdfFile.PageCount;
            }
            if (startPageNum > endPageNum)
            {
                int tempPageNum = startPageNum;
                startPageNum = endPageNum;
                endPageNum = startPageNum;
            }
            // start to convert each page
            for (int i = startPageNum; i <= endPageNum; i++)
            {
                Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition);
                pageImage.Save(imageOutputPath + imageName + i.ToString() + "." + imageFormat.ToString(), imageFormat);
                pageImage.Dispose();
            }
            pdfFile.Dispose();
        }
        /// <summary>
        /// 將PDF轉換爲圖片方法二
        /// </summary>
        /// <param name="pdfPath">pdf文件位置</param>
        /// <param name="pageNumber">pdf文件張數</param>
        /// <param name="size">pdf文件尺寸</param>
        /// <param name="outputPath">輸出圖片位置與名稱</param>
        public static void RenderPage(string pdfPath, int pageNumber, System.Drawing.Size size, string outputPath, int dpi = 300)
        {
            #region 調用方法
            //轉一頁的話直接調用RenderPage
            //轉整個文檔調用
            //var pdf = PdfDocument.Load(@"d://20190514174209.pdf");
            //var pdfpage = pdf.PageCount;
            //var pagesizes = pdf.PageSizes;

            //for (int i = 1; i <= pdfpage; i++)
            //{
            //    Size size = new Size();
            //    size.Height = (int)pagesizes[(i - 1)].Height + 265;
            //    size.Width = (int)pagesizes[(i - 1)].Width + 405;
            //    PDFHelp.RenderPage(@"d://20190514174209.pdf", i, size, @"d://" + i + @".jpg");
            //}
            #endregion
            using (var document = PdfiumViewer.PdfDocument.Load(pdfPath))
            using (var stream = new FileStream(outputPath, FileMode.Create))
            using (var image = GetPageImage(pageNumber, size, document, dpi))
            {
                image.Save(stream, ImageFormat.Jpeg);
            }
        }
        private static Image GetPageImage(int pageNumber, Size size, PdfiumViewer.PdfDocument document, int dpi)
        {
            return document.Render(pageNumber - 1, size.Width, size.Height, dpi, dpi, PdfRenderFlags.Annotations);
        }

    }

  第二種方法 nuget安裝dll文檔

Install-Package PdfiumViewer -Version 2.13.0get

Install-Package ImageResizer.Plugins.PdfiumRenderer.Pdfium.Dll -Version 1.0.1string

參考鏈接https://blog.csdn.net/touhousonic/article/details/86711506it

相關文章
相關標籤/搜索