将 Word 转换为 Tiff
TIFF(标记图像文件格式)是一种灵活的文件格式,用于存储图像,包括照片和艺术图像。它受到图像处理应用程序、出版和页面布局应用程序以及扫描、传真、文字处理应用程序等的广泛支持。它可以是一个包含压缩(有损)JPEG 和(无损)PackBits 压缩图像的容器。以无损格式存储图像数据的能力使 TIFF 文件成为有用的图像存档。因此,有时开发人员需要将其他格式(如 word)的文档转换为 TIFF 格式。
Spire.Doc,一个强大的 .NET word 组件,使开发人员能够轻松地将文件从 word 转换为 TIFF。本文将向您介绍使用Spire.Doc将word转换为TIFF的解决方案。
注意:请先下载最新版本的Spire.Doc for .NET(或Spire.Office)在bin文件夹中添加.dll作为Visual Studio的参考。
示例word文件:
请按照以下详细步骤操作:
第 1 步:创建一个新的文档实例并从文件中加载一个 word 文档。
Document document = new Document(@"E:\Program Files\testing.docx");
第 2 步:使用 document.SaveToImages() 方法将 word 文档保存为 Image 数组。
private static Image[] SaveAsImage(Document document) { Image[] images = document.SaveToImages(ImageType.Bitmap); return images; }
第 3 步:使用JoinTiffImages()方法将word pages中的图片保存为tiff图片类型,指定encoder和image-encoder参数。
public static void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder) { //use the save encoder System.Drawing.Imaging.Encoder enc = System.Drawing.Imaging.Encoder.SaveFlag; EncoderParameters ep = new EncoderParameters(2); ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame); ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.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++; } }
TIFF文件:
完整代码:
[C#]
using Spire.Doc; using Spire.Doc.Documents; using System; using System.Drawing; using System.Drawing.Imaging; namespace convert_word_to_tiff { class Program { static void Main(string[] args) { Document document = new Document(@"E:\Program Files\testing.docx"); JoinTiffImages(SaveAsImage(document),"6056result.tiff",EncoderValue.CompressionLZW); System.Diagnostics.Process.Start("6056result.tiff"); } private static Image[] SaveAsImage(Document document) { Image[] images = document.SaveToImages(ImageType.Bitmap); 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 System.Drawing.Imaging.Encoder enc = System.Drawing.Imaging.Encoder.SaveFlag; EncoderParameters ep = new EncoderParameters(2); ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame); ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.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 Spire.Doc Imports Spire.Doc.Documents Imports System.Drawing Imports System.Drawing.Imaging Namespace convert_word_to_tiff Class Program Private Shared Sub Main(args As String()) Dim document As New Document("E:\Program Files\testing.docx") JoinTiffImages(SaveAsImage(document), "6056result.tiff", EncoderValue.CompressionLZW) System.Diagnostics.Process.Start("6056result.tiff") End Sub Private Shared Function SaveAsImage(document As Document) As Image() Dim images As Image() = document.SaveToImages(ImageType.Bitmap) Return images End Function Private Shared Function GetEncoderInfo(mimeType As String) As ImageCodecInfo Dim encoders As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders() For j As Integer = 0 To encoders.Length - 1 If encoders(j).MimeType = mimeType Then Return encoders(j) End If Next Throw New Exception(mimeType & Convert.ToString(" mime type not found in ImageCodecInfo")) End Function Public Shared Sub JoinTiffImages(images As Image(), outFile As String, compressEncoder As EncoderValue) 'use the save encoder Dim enc As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.SaveFlag Dim ep As New EncoderParameters(2) ep.Param(0) = New EncoderParameter(enc, CLng(EncoderValue.MultiFrame)) ep.Param(1) = New EncoderParameter(System.Drawing.Imaging.Encoder.Compression, CLng(compressEncoder)) Dim pages As Image = images(0) Dim frame As Integer = 0 Dim info As ImageCodecInfo = GetEncoderInfo("image/tiff") For Each img As Image 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, CLng(EncoderValue.FrameDimensionPage)) pages.SaveAdd(img, ep) End If If frame = images.Length - 1 Then 'flush and close. ep.Param(0) = New EncoderParameter(enc, CLng(EncoderValue.Flush)) pages.SaveAdd(ep) End If frame += 1 Next End Sub End Class End Namespace
Spire.Doc 可以将 Word 转换为大多数流行的文件格式。它可以将Word转换为PDF、HTML、XML、RTF、Text、ePub等。