C#將PDF轉(zhuǎn)為多種圖像文件格式的方法(Png/Bmp/Emf/Tiff)
PDF是一種在我們?nèi)粘9ぷ鲗W(xué)習(xí)中最常用到的文檔格式之一,但常常也會(huì)因?yàn)槲臋n的不易編輯的特點(diǎn),在遇到需要編輯PDF文檔內(nèi)容或者轉(zhuǎn)換文件格式的情況時(shí)讓人苦惱。通常對(duì)于開(kāi)發(fā)者而言,可選擇通過(guò)使用組件的方式來(lái)實(shí)現(xiàn)PDF文檔的編輯或者格式轉(zhuǎn)換,因此本文將介紹如何通過(guò)使用免費(fèi)版的組件Free Spire.PDF for .NET來(lái)轉(zhuǎn)換PDF文檔。這里介紹將PDF轉(zhuǎn)換多種不同格式的圖像文件格式,如PNG,BMP,EMF,TIFF等,同時(shí),轉(zhuǎn)換文檔也分為轉(zhuǎn)換全部文檔和轉(zhuǎn)換部分文檔為圖片兩種情況,本文也將作進(jìn)一步介紹。下面是實(shí)現(xiàn)轉(zhuǎn)換功能的詳述,供參考。
提示:在下載安裝該組件后,在項(xiàng)目中注意添加引用Spire.PDF.dll文件,如下圖:
一、轉(zhuǎn)換整個(gè)PDF文檔為圖片
(一)PDF轉(zhuǎn)Png
using Spire.Pdf; using System.Drawing; namespace PDFtoImage1 { class Program { static void Main(string[] args) { //初始化一個(gè)PdfDocument類實(shí)例,并加載PDF文檔 PdfDocument doc = new PdfDocument(); doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf"); //遍歷PDF每一頁(yè) for (int i = 0; i < doc.Pages.Count; i++) { //將PDF頁(yè)轉(zhuǎn)換成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); } } } }
調(diào)試運(yùn)行程序,生成文檔。
運(yùn)行結(jié)果:
Spire.PDF支持將PDF文檔轉(zhuǎn)換為多種圖像格式的文件,可根據(jù)需要選擇相應(yīng)的文件格式,這里以Png為例。
(二) PDF轉(zhuǎn)TIFF
using System; using System.Drawing; using System.Drawing.Imaging; using Spire.Pdf; namespace SavePdfAsTiff { class Program { static void Main(string[] args) { //創(chuàng)建一個(gè)PdfDocument類對(duì)象,并加載PDF文檔 PdfDocument document = new PdfDocument(); document.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf"); //調(diào)用方法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()方法,使用指定編碼器和圖像編碼器參數(shù)將圖像從pdf頁(yè)面保存到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++; } } } }
運(yùn)行結(jié)果:
二、 轉(zhuǎn)換PDF指定頁(yè)為圖片( PDF轉(zhuǎn)Png、Bmp、Emf)
using Spire.Pdf; using System.Drawing; using System.Drawing.Imaging; namespace PDFtoImage { class Program { static void Main(string[] args) { //實(shí)例化一個(gè)PdfDocument類對(duì)象,并加載PDF文檔 PdfDocument doc = new PdfDocument(); doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf"); //調(diào)用方法SaveAsImage()將PDF第二頁(yè)保存為Bmp格式 Image bmp = doc.SaveAsImage(1); //調(diào)用另一個(gè)SaveAsImage()方法,并將指定頁(yè)面保存保存為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); } //命名保存的文件并打開(kāi) 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"); } } }
運(yùn)行結(jié)果:
總結(jié)
以上所述是小編給大家介紹的C#將PDF轉(zhuǎn)為多種圖像文件格式的方法(Png/Bmp/Emf/Tiff),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
C#數(shù)據(jù)結(jié)構(gòu)與算法揭秘四 雙向鏈表
上節(jié)說(shuō)過(guò)這節(jié)會(huì)講雙向鏈表,環(huán)形鏈表和應(yīng)用舉例,我們開(kāi)始吧?。。?!2012-11-11C#?wpf定義ViewModelBase進(jìn)行簡(jiǎn)化屬性綁定
綁定機(jī)制是wpf的核心,也是界面獨(dú)立的根本,尤其是使用了mvvm模式,本文主要介紹了wpf如何定義ViewModelBase進(jìn)行簡(jiǎn)化屬性綁定,需要的可以參考下2024-04-04C#開(kāi)發(fā)Windows UWP系列之3D變換
這篇文章介紹了C#開(kāi)發(fā)Windows UWP系列之3D變換,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06C# Chart折線圖使用鼠標(biāo)滾輪放大、縮小和平移曲線方式
這篇文章主要介紹了C# Chart折線圖使用鼠標(biāo)滾輪放大、縮小和平移曲線方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06Unity實(shí)現(xiàn)物體跟隨鼠標(biāo)移動(dòng)
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)物體跟隨鼠標(biāo)移動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01C#使用HtmlAgilityPack實(shí)現(xiàn)解析提取HTML內(nèi)容
HtmlAgilityPack是一個(gè)HTML解析類庫(kù),這篇文章主要為大家詳細(xì)介紹了C#如何使用HtmlAgilityPack實(shí)現(xiàn)解析提取HTML內(nèi)容,感興趣的小伙伴可以參考一下2023-12-12