C# 如何将PDF转为多种图像文件格式(Png/Bmp/Emf/Tiff)

发布日期:2018-02-08    浏览次数:835

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++;
            }
        }
    }
}

 

运行结果:

二、 转换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转换功能的介绍可参见以下博客内容:

本文网址:https://www.wyxxw.cn/blog-detail-2-6-72.html

返回列表

非特殊说明,本文版权归原作者所有,转载请注明出处

提示:本站所有资源仅供学习与参考,请勿用于商业用途。图片来自互联网~如侵犯您的权益,请联系QQ:1067507709.

提示:转载请注明来自:https://www.cnblogs.com/Yesi/p/8423008.html 。 转载人:momo