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


PDF是一種在我們日常工作學習中最常用到的文檔格式之一,但常常也會因為文檔的不易編輯的特點,在遇到需要編輯PDF文檔內容或者轉換文件格式的情況時讓人苦惱。通常對於開發者而言,可選擇通過使用組件的方式來實現PDF文檔的編輯或者格式轉換,因此本文將介紹如何通過使用免費版的組件Free Spire.PDF for .NET來轉換PDF文檔。這里介紹將PDF轉換多種不同格式的圖像文件格式,如PNG,BMP,EMF,TIFF等,同時,轉換文檔也分為轉換全部文檔和轉換部分文檔為圖片兩種情況,本文也將作進一步介紹。下面是實現轉換功能的詳述,供參考。

提示:下載安裝該組件后,在項目中注意添加引用Spire.PDF.dll文件,如下圖:

一、轉換整個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);
            }
        }
    }
}

調試運行程序,生成文檔。

運行結果:

Spire.PDF支持將PDF文檔轉換為多種圖像格式的文件,可根據需要選擇相應的文件格式,這里以Png為例。

(二) 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++;
            }
        }
    }
}

 

運行結果:

(三)TIFF 轉PDF

static void Main(string[] args)
        {
            //創建PdfDocument對象
            PdfDocument pdfDocument = new PdfDocument();
            //加載tiff圖片
            Image tiffImage = Image.FromFile("test.tiff");

            //調用方法拆分tiff圖片
            Image[] images = SplitTIFFImage(tiffImage);
             for (int i = 0; i < images.Length; i++)
              {
                PdfImage pdfImg = PdfImage.FromImage(images[i]);//獲取圖片
                PdfPageBase page = pdfDocument.Pages.Add();//添加PDF頁面
                //指定圖片在PDF中的高度、寬度及位置
                float width = pdfImg.Width * 0.5f;
                float height = pdfImg.Height * 0.5f;
                float x = (page.Canvas.ClientSize.Width - width) / 2;
                //將圖片繪制到PDF頁面 
                page.Canvas.DrawImage(pdfImg, x, 0, width, height);
              }
            //保存文檔
            pdfDocument.SaveToFile("result.pdf");
        }
        //自定義方法SplitTIFFImage()
        public static Image[] SplitTIFFImage(Image tiffImage)
        {
            int frameCount = tiffImage.GetFrameCount(FrameDimension.Page);
            Image[] images = new Image[frameCount];
            Guid objGuid = tiffImage.FrameDimensionsList[0];
            FrameDimension objDimension = new FrameDimension(objGuid); 
            for (int i = 0; i < frameCount; i++)
            {
                tiffImage.SelectActiveFrame(objDimension, i);
                using (MemoryStream ms = new MemoryStream())
                {
                    tiffImage.Save(ms, ImageFormat.Tiff);
                    images[i] = Image.FromStream(ms);
                }
            }
            return images;
        }

 

二、 轉換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");
        }
    }
}

運行結果:

PS:更多關於PDF轉換功能的介紹可參見以下博客內容:

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

感謝閱讀!


免責聲明!

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



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