将PDF文档保存为tiff图像
Spire.PDF for .NET 是一款专门对 Word 文档进行操作的 .NET 类库。致力于在于帮助开发人员轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档,而无需安装 Microsoft Word。
行号用于在每行文本旁边显示 Word 自动计算的行数。当我们需要参考合同或法律文件等文档中的特定行时,它非常有用。word中的行号功能允许我们设置起始值、编号间隔、与文本的距离以及行号的编号方式。使用 Spire.Doc,我们可以实现上述所有功能。本文将介绍如何将 XPS 转为PDF 格式。
欢迎加入spire技术交流群:767755948
Tiff图像作为一种图形容器,既可以存储光栅图像,也可以存储矢量图像;可以包含高质量的图形,支持从1位到24位的色深;支持有损和无损压缩;还支持多层和多页。若想将文档转换为高质量的图形,并且在压缩过程中保存时不会丢失图像文件信息,tiff图像是您的最佳选择。
本文介绍了通过document.SaveAsImage()和JoinTiffImages()方法将PDF文档保存为tiff图像的详细方法。Spire.PDF for .NET是一个PDF组件,它包含了在.NET、Silverlight和WPF平台上创建、阅读、编辑和处理PDF文档的丰富功能。下面的截图展示了将PDF文档保存为tiff图像后的结果:
该方法的主要步骤如下
步骤1:创建一个新的pdf文档并加载它。
1 PdfDocument document = new PdfDocument(); 2 document.LoadFromFile(@"sample.pdf");
步骤2:使用document.SaveAsImage()方法将pdf文档保存为图像数组。
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图像类型,并指定编码器和图像编码器参数。
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