將PDF文檔保存為tiff圖像
Spire.PDF for .NET 是一款專門對 Word 文檔進行操作的 .NET 類庫。致力于在于幫助開發(fā)人員輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔,而無需安裝 Microsoft Word。
行號用于在每行文本旁邊顯示 Word 自動計算的行數(shù)。當我們需要參考合同或法律文件等文檔中的特定行時,它非常有用。word中的行號功能允許我們設置起始值、編號間隔、與文本的距離以及行號的編號方式。使用 Spire.Doc,我們可以實現(xiàn)上述所有功能。本文將介紹如何將 XPS 轉(zhuǎn)為PDF 格式。
歡迎加入spire技術交流群:767755948
Tiff圖像作為一種圖形容器,既可以存儲光柵圖像,也可以存儲矢量圖像;可以包含高質(zhì)量的圖形,支持從1位到24位的色深;支持有損和無損壓縮;還支持多層和多頁。若想將文檔轉(zhuǎn)換為高質(zhì)量的圖形,并且在壓縮過程中保存時不會丟失圖像文件信息,tiff圖像是您的最佳選擇。
本文介紹了通過document.SaveAsImage()和JoinTiffImages()方法將PDF文檔保存為tiff圖像的詳細方法。Spire.PDF for .NET是一個PDF組件,它包含了在.NET、Silverlight和WPF平臺上創(chuàng)建、閱讀、編輯和處理PDF文檔的豐富功能。下面的截圖展示了將PDF文檔保存為tiff圖像后的結果:
該方法的主要步驟如下
步驟1:創(chuàng)建一個新的pdf文檔并加載它。
1 PdfDocument document = new PdfDocument(); 2 document.LoadFromFile(@"sample.pdf");
步驟2:使用document.SaveAsImage()方法將pdf文檔保存為圖像數(shù)組。
01 private static Image[] SaveAsImage(PdfDocument document) 02 { 03 Image[] images = new Image[document.Pages.Count]; 04 for (int i = 0; i < document.Pages.Count; i++) 05 { 06 //use the document.SaveAsImage() method save the pdf as image 07 images[i] = document.SaveAsImage(i); 08 } 09 return images; 10 }
步驟3:使用JoinTiffImages()方法將pdf頁面中的圖像保存為tiff圖像類型,并指定編碼器和圖像編碼器參數(shù)。
01 public static void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder) 02 { 03 //use the save encoder 04 Encoder enc = Encoder.SaveFlag; 05 EncoderParameters ep = new EncoderParameters(2); 06 ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame); 07 ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)compressEncoder); 08 Image pages = images[0]; 09 int frame = 0; 10 ImageCodecInfo info = GetEncoderInfo("image/tiff"); 11 foreach (Image img in images) 12 { 13 if (frame == 0) 14 { 15 pages = img; 16 //save the first frame 17 pages.Save(outFile, info, ep); 18 } 19 else 20 { 21 //save the intermediate frames 22 ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage); 23 pages.SaveAdd(img, ep); 24 } 25 if (frame == images.Length - 1) 26 { 27 //flush and close. 28 ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush); 29 pages.SaveAdd(ep); 30 } 31 frame++; 32 } 33 }
下載并安裝Spire.Pdf for .NET,使用下面的代碼體驗將pdf文檔保存為tiff圖片的方法。
完整代碼:
[C#]
01 using System; 02 using System.Drawing; 03 using System.Drawing.Imaging; 04 using Spire.Pdf; 05 namespace SavePdfAsTiff 06 { 07 class Program 08 { 09 static void Main(string[] args) 10 { 11 PdfDocument document = new PdfDocument(); 12 document.LoadFromFile(@"01.pdf"); 13 JoinTiffImages(SaveAsImage(document),"result.tiff",EncoderValue.CompressionLZW); 14 System.Diagnostics.Process.Start("result.tiff"); 15 } 16 private static Image[] SaveAsImage(PdfDocument document) 17 { 18 Image[] images = new Image[document.Pages.Count]; 19 for (int i = 0; i < document.Pages.Count; i++) 20 { 21 images[i] = document.SaveAsImage(i); 22 } 23 return images; 24 } 25 26 private static ImageCodecInfo GetEncoderInfo(string mimeType) 27 { 28 ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders(); 29 for (int j = 0; j < encoders.Length; j++) 30 { 31 if (encoders[j].MimeType == mimeType) 32 return encoders[j]; 33 } 34 throw new Exception(mimeType + " mime type not found in ImageCodecInfo"); 35 } 36 37 public static void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder) 38 { 39 //use the save encoder 40 Encoder enc = Encoder.SaveFlag; 41 EncoderParameters ep = new EncoderParameters(2); 42 ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame); 43 ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)compressEncoder); 44 Image pages = images[0]; 45 int frame = 0; 46 ImageCodecInfo info = GetEncoderInfo("image/tiff"); 47 foreach (Image img in images) 48 { 49 if (frame == 0) 50 { 51 pages = img; 52 //save the first frame 53 pages.Save(outFile, info, ep); 54 } 55 56 else 57 { 58 //save the intermediate frames 59 ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage); 60 61 pages.SaveAdd(img, ep); 62 } 63 if (frame == images.Length - 1) 64 { 65 //flush and close. 66 ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush); 67 pages.SaveAdd(ep); 68 } 69 frame++; 70 } 71 } 72 } 73 }
[VB.NET]
01 Imports System 02 Imports System.Drawing 03 Imports System.Drawing.Imaging 04 Imports Spire.Pdf 05 Namespace SavePdfAsTiff 06 Class Program 07 Sub Main() 08 Dim document As PdfDocument = New PdfDocument() 09 document.LoadFromFile("01.pdf") 10 JoinTiffImages(SaveAsImage(document), "result.tiff", EncoderValue.CompressionLZW) 11 System.Diagnostics.Process.Start("result.tiff") 12 End Sub 13 Private Shared Function SaveAsImage(ByVal document As PdfDocument) 14 Dim images() As Image = New Image(document.Pages.Count-1) {} 15 Dim i As Integer 16 For i = 0 To document.Pages.Count - 1 Step i + 1 17 images(i) = document.SaveAsImage(i) 18 Next 19 Return images 20 End Function 21 Private Shared Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo 22 Dim encoders() As ImageCodecInfo = ImageCodecInfo.GetImageEncoders() 23 Dim j As Integer 24 For j = 0 To encoders.Length - 1 Step j + 1 25 If encoders(j).MimeType = mimeType Then 26 Return encoders(j) 27 End If 28 Next 29 Throw New Exception(mimeType + " mime type not found in ImageCodecInfo") 30 End Function 31 Public Shared Sub JoinTiffImages(ByVal images() As Image, ByVal outFile As String, ByVal compressEncoder As EncoderValue) 32 use the save encoder 33 Dim enc As Encoder = Encoder.SaveFlag 34 Dim ep As EncoderParameters = New EncoderParameters(2) 35 ep.Param(0) = New EncoderParameter(enc, CType(EncoderValue.MultiFrame, Long)) 36 ep.Param(1) = New EncoderParameter(Encoder.Compression, CType(compressEncoder, Long)) 37 Dim pages As Image = images(0) 38 Dim frame As Integer = 0 39 Dim info As ImageCodecInfo = GetEncoderInfo("image/tiff") 40 Dim img As Image 41 For Each img In images 42 If frame = 0 Then 43 pages = img 44 'save the first frame 45 pages.Save(outFile, info, ep) 46 Else 47 'save the intermediate frames 48 ep.Param(0) = New EncoderParameter(enc, CType(EncoderValue.FrameDimensionPage, Long)) 49 pages.SaveAdd(img, ep) 50 End If 51 52 If frame = images.Length - 1 Then 53 'flush and close. 54 ep.Param(0) = New EncoderParameter(enc, CType(EncoderValue.Flush, Long)) 55 pages.SaveAdd(ep) 56 End If 57 frame = frame + 1 58 Next 59 End Sub 60 End Class 61 End Namespace