將 Word 轉(zhuǎn)換為 Tiff
TIFF(標(biāo)記圖像文件格式)是一種靈活的文件格式,用于存儲圖像,包括照片和藝術(shù)圖像。它受到圖像處理應(yīng)用程序、出版和頁面布局應(yīng)用程序以及掃描、傳真、文字處理應(yīng)用程序等的廣泛支持。它可以是一個包含壓縮(有損)JPEG 和(無損)PackBits 壓縮圖像的容器。以無損格式存儲圖像數(shù)據(jù)的能力使 TIFF 文件成為有用的圖像存檔。因此,有時開發(fā)人員需要將其他格式(如 word)的文檔轉(zhuǎn)換為 TIFF 格式。
Spire.Doc,一個強(qiáng)大的 .NET word 組件,使開發(fā)人員能夠輕松地將文件從 word 轉(zhuǎn)換為 TIFF。本文將向您介紹使用Spire.Doc將word轉(zhuǎn)換為TIFF的解決方案。
注意:請先下載最新版本的Spire.Doc for .NET(或Spire.Office)在bin文件夾中添加.dll作為Visual Studio的參考。
示例word文件:
請按照以下詳細(xì)步驟操作:
第 1 步:創(chuàng)建一個新的文檔實(shí)例并從文件中加載一個 word 文檔。
Document document = new Document(@"E:\Program Files\testing.docx");
第 2 步:使用 document.SaveToImages() 方法將 word 文檔保存為 Image 數(shù)組。
private static Image[] SaveAsImage(Document document) { Image[] images = document.SaveToImages(ImageType.Bitmap); return images; }
第 3 步:使用JoinTiffImages()方法將word pages中的圖片保存為tiff圖片類型,指定encoder和image-encoder參數(shù)。
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 轉(zhuǎn)換為大多數(shù)流行的文件格式。它可以將Word轉(zhuǎn)換為PDF、HTML、XML、RTF、Text、ePub等。