C# 如何將PDF轉爲多種圖像文件格式(Png/Bmp/Emf/Tiff)

PDF是一種在我們日常工作學習中最常用到的文檔格式之一,但常常也會因爲文檔的不易編輯的特點,在遇到需要編輯PDF文檔內容或者轉換文件格式的情況時讓人苦惱。通常對於開發者而言,可選擇通過使用組件的方式來實現PDF文檔的編輯或者格式轉換,因此本文將介紹如何通過使用免費版的組件Free Spire.PDF for .NET來轉換PDF文檔。這裏介紹將PDF轉換多種不同格式的圖像文件格式,如PNG,BMP,EMF,TIFF等,同時,轉換文檔也分爲轉換全部文檔和轉換部分文檔爲圖片兩種情況,本文也將作進一步介紹。下面是實現轉換功能的詳述,供參考。
提示:在下載安裝該組件後,在項目中注意添加引用Spire.PDF.dll文件,如下圖:
C# 如何將PDF轉爲多種圖像文件格式(Png/Bmp/Emf/Tiff)

一、轉換整個PDF文檔爲圖片

(一)PDF轉Png

using Spire.Pdf;
using System.Drawing;

namespace PDFtoImage1
{
    class Program
    {
        static void Main(string[] args)
        {
            //初始化一個PdfDocument類實例,並加載PDF文檔
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

            //遍歷PDF每一頁
            for (int i = 0; i < doc.Pages.Count; i++)
            {
                //將PDF頁轉換成Bitmap圖形
                System.Drawing.Image bmp = doc.SaveAsImage(i);

                //將Bitmap圖形保存爲Png格式的圖片
                string fileName = string.Format("Page-{0}.png", i + 1);
                bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
            }
        }
    }
}

調試運行程序,生成文檔。
C# 如何將PDF轉爲多種圖像文件格式(Png/Bmp/Emf/Tiff)
Spire.PDF支持將PDF文檔轉換爲多種圖像格式的文件,可根據需要選擇相應的文件格式,這裏以Png爲例。
C# 如何將PDF轉爲多種圖像文件格式(Png/Bmp/Emf/Tiff)

(二)PDF轉Tiff

using System;
using System.Drawing;
using System.Drawing.Imaging;
using Spire.Pdf;

namespace SavePdfAsTiff
{
    class Program
    {
        static void Main(string[] args)
        {
            //創建一個PdfDocument類對象,並加載PDF文檔
            PdfDocument document = new PdfDocument();
            document.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

            //調用方法SaveAsImage()將PDF文檔保存爲tiff格式
            JoinTiffImages(SaveAsImage(document), "result.tiff", EncoderValue.CompressionLZW);
            System.Diagnostics.Process.Start("result.tiff");
        }
        //自定義方法SaveAsImage()將PDF文檔保存圖像文件
        private static Image[] SaveAsImage(PdfDocument document)
        {
            Image[] images = new Image[document.Pages.Count];
            for (int i = 0; i < document.Pages.Count; i++)
            {
                images[i] = document.SaveAsImage(i);
            }
            return images;
        }

        private static ImageCodecInfo GetEncoderInfo(string mimeType)
        {
            ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
            for (int j = 0; j < encoders.Length; j++)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
        }
        //自定義JoinTiffImages()方法,使用指定編碼器和圖像編碼器參數將圖像從pdf頁面保存到tiff圖像類型,。
        public static void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder)
        {

            Encoder enc = Encoder.SaveFlag;
            EncoderParameters ep = new EncoderParameters(2);
            ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
            ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)compressEncoder);
            Image pages = images[0];
            int frame = 0;
            ImageCodecInfo info = GetEncoderInfo("image/tiff");
            foreach (Image img in images)
            {
                if (frame == 0)
                {
                    pages = img;

                    pages.Save(outFile, info, ep);
                }

                else
                {

                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);

                    pages.SaveAdd(img, ep);
                }
                if (frame == images.Length - 1)
                {

                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
                    pages.SaveAdd(ep);
                }
                frame++;
            }
        }
    }
}

運行結果:
C# 如何將PDF轉爲多種圖像文件格式(Png/Bmp/Emf/Tiff)

二、 轉換PDF指定頁爲圖片(PDF轉Png、Bmp、Emf)

using Spire.Pdf;
using System.Drawing;
using System.Drawing.Imaging;

namespace PDFtoImage
{
    class Program
    {
        static void Main(string[] args)
        {
            //實例化一個PdfDocument類對象,並加載PDF文檔
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

            //調用方法SaveAsImage()將PDF第二頁保存爲Bmp格式
            Image bmp = doc.SaveAsImage(1);
            // //調用另一個SaveAsImage()方法,並將指定頁面保存保存爲Emf、Png            Image emf = doc.SaveAsImage(0, Spire.Pdf.Graphics.PdfImageType.Metafile);
            Image zoomImg = new Bitmap((int)(emf.Size.Width * 2), (int)(emf.Size.Height * 2));
            using (Graphics g = Graphics.FromImage(zoomImg))
            {
                g.ScaleTransform(2.0f, 2.0f);
                g.DrawImage(emf, new Rectangle(new Point(0, 0), emf.Size), new Rectangle(new Point(0, 0), emf.Size), GraphicsUnit.Pixel);
            }

            //命名保存的文件並打開
            bmp.Save("convertToBmp.bmp", ImageFormat.Bmp);
            System.Diagnostics.Process.Start("convertToBmp.bmp");
            emf.Save("convertToEmf.emf", ImageFormat.Emf);
            System.Diagnostics.Process.Start("convertToEmf.emf");
            zoomImg.Save("convertToZoom.png", ImageFormat.Png);
            System.Diagnostics.Process.Start("convertToZoom.png");
        }
    }
}

運行結果:
C# 如何將PDF轉爲多種圖像文件格式(Png/Bmp/Emf/Tiff)

以上全部內容爲本篇文章關於PDF轉爲多種圖像文件的方法介紹,如果喜歡本文歡迎轉載(轉載請註明出處)。
感謝閱讀!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章