赞
踩
Spire.Doc 是一款专门对 Word 文档进行操作的 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。
E-iceblue 功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式
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 图像,如果您有其他问题也可以继续浏览本系列文章,获取相关教程~
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。