翻譯|行業資訊|編輯:胡濤|2023-07-31 14:25:20.357|閱讀 125 次
概述:本文介紹了使用Spire.PDF for .NET 通過 document.SaveAsImage() 和 JoinTiffImages() 方法將 PDF 文檔保存為 tiff 圖像的詳細方法,歡迎查閱~
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Spire.Doc 是一款專門對 Word 文檔進行操作的 類庫。在于幫助開發人員無需安裝 Microsoft Word情況下,輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔。擁有近10年專業開發經驗Spire系列辦公文檔開發工具,專注于創建、編輯、轉換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團隊研發,不依賴第三方軟件,不受其他國家的技術或法律法規限制,同時適配國產操作系統如中科方德、中標麒麟等,兼容國產文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
Spire.PDF for.net下載 Spire.PDF for java下載
Tiff圖像作為圖形容器,可以存儲光柵圖像和矢量圖像;可能包含支持 1 至 24 位顏色深度的高質量圖形;支持有損和無損壓縮;還支持多層和頁面。如果您希望文檔能夠轉換為高質量的圖形,并且在壓縮過程中保存時不丟失圖像文件信息,tiff 圖像是您的最佳選擇。
本文介紹了使用Spire.PDF for .NET 通過 document.SaveAsImage() 和 JoinTiffImages() 方法將 PDF 文檔保存為 tiff 圖像的詳細方法,Spire.PDF for .NET是一個 PDF 組件,包含令人難以置信的豐富的創建、閱讀、編輯功能并在.NET、Silverlight 和 WPF 平臺上操作 PDF 文檔。下面的屏幕截圖顯示了將 PDF 文檔保存為 tiff 圖像后的結果:
該方法的主要步驟是:
第 1 步:創建一個新的 pdf 文檔并加載它。
PdfDocument document = new PdfDocument(); document.LoadFromFile(@"sample.pdf");
步驟2: 使用document.SaveAsImage()方法將pdf文檔保存為圖像數組。
private static Image[] SaveAsImage(PdfDocument document) { Image[] images = new Image[document.Pages.Count]; for (int i = 0; i < document.Pages.Count; i++) { //use the document.SaveAsImage() method save the pdf as image images[i] = document.SaveAsImage(i); } return images; }
步驟 3:使用 JoinTiffImages() 方法將 pdf 頁面中的圖像保存為 tiff 圖像類型,并指定編碼器和圖像編碼器參數。
public static void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder) { //use the save encoder 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; //save the first frame pages.Save(outFile, info, ep); } else { //save the intermediate frames ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage); pages.SaveAdd(img, ep); } if (frame == images.Length - 1) { //flush and close. ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush); pages.SaveAdd(ep); } frame++; } }
完整代碼:
[C#]
using System; using System.Drawing; using System.Drawing.Imaging; using Spire.Pdf; namespace SavePdfAsTiff { class Program { static void Main(string[] args) { PdfDocument document = new PdfDocument(); document.LoadFromFile(@"01.pdf"); JoinTiffImages(SaveAsImage(document),"result.tiff",EncoderValue.CompressionLZW); System.Diagnostics.Process.Start("result.tiff"); } 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"); } public static void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder) { //use the save encoder 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; //save the first frame pages.Save(outFile, info, ep); } else { //save the intermediate frames ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage); pages.SaveAdd(img, ep); } if (frame == images.Length - 1) { //flush and close. ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush); pages.SaveAdd(ep); } frame++; } } } }
[VB.NET]
Imports System Imports System.Drawing Imports System.Drawing.Imaging Imports Spire.Pdf Namespace SavePdfAsTiff Class Program Sub Main() Dim document As PdfDocument = New PdfDocument() document.LoadFromFile("01.pdf") JoinTiffImages(SaveAsImage(document), "result.tiff", EncoderValue.CompressionLZW) System.Diagnostics.Process.Start("result.tiff") End Sub Private Shared Function SaveAsImage(ByVal document As PdfDocument) Dim images() As Image = New Image(document.Pages.Count-1) {} Dim i As Integer For i = 0 To document.Pages.Count - 1 Step i + 1 images(i) = document.SaveAsImage(i) Next Return images End Function Private Shared Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo Dim encoders() As ImageCodecInfo = ImageCodecInfo.GetImageEncoders() Dim j As Integer For j = 0 To encoders.Length - 1 Step j + 1 If encoders(j).MimeType = mimeType Then Return encoders(j) End If Next Throw New Exception(mimeType + " mime type not found in ImageCodecInfo") End Function Public Shared Sub JoinTiffImages(ByVal images() As Image, ByVal outFile As String, ByVal compressEncoder As EncoderValue) 'use the save encoder Dim enc As Encoder = Encoder.SaveFlag Dim ep As EncoderParameters = New EncoderParameters(2) ep.Param(0) = New EncoderParameter(enc, CType(EncoderValue.MultiFrame, Long)) ep.Param(1) = New EncoderParameter(Encoder.Compression, CType(compressEncoder, Long)) Dim pages As Image = images(0) Dim frame As Integer = 0 Dim info As ImageCodecInfo = GetEncoderInfo("image/tiff") Dim img As Image For Each img In images If frame = 0 Then pages = img 'save the first frame pages.Save(outFile, info, ep) Else 'save the intermediate frames ep.Param(0) = New EncoderParameter(enc, CType(EncoderValue.FrameDimensionPage, Long)) pages.SaveAdd(img, ep) End If If frame = images.Length - 1 Then 'flush and close. ep.Param(0) = New EncoderParameter(enc, CType(EncoderValue.Flush, Long)) pages.SaveAdd(ep) End If frame = frame + 1 Next End Sub End Class End Namespace
以上便是如何PDF 文檔另存為 tiff 圖像,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
獲取更多信息請咨詢 ;技術交流Q群(767755948)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn