C# PDF转图片(Png/Bmp/Emf/Tiff)

2025-10-25 21:42:12

1、下载并安装该类库,在项目程序中添加Spire.Pdf.dll文件

C# PDF转图片(Png/Bmp/Emf/Tiff)

2、(一)PDF转Png

【C#】

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为例。)

C# PDF转图片(Png/Bmp/Emf/Tiff)

C# PDF转图片(Png/Bmp/Emf/Tiff)

3、(二)PDF转Tiff

【C#】

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)

4、(三)转换PDF指定页为图片(PDF转Png、Bmp、Emf)

【C#】

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)

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢