标签:
一款有着强大的文档转换成果的工具,无论何时何地城市是现代庖公环境极为需要的。在本篇文章中,将介绍关于Word文档的转换成果(Word转XPS/SVG/EMF/EPUB/TIFF)。但愿要领中的代码能为列位开发者们供给必然的参考价值。使用工具:Free Spire.Doc for .NET(社区版)
使用要领:下载安置该控件后,在VS控制台应用措施中添加引用Spire.Doc.dll文件(dll文件可在该安置文件夹下Bin中获取)
调试运行该项目生成文档,如下图:
6. Word转Tiff 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类,加载Word sample Document document = new Document(@"C:\Users\Administrator\Desktop\sample.docx"); //挪用要领JoinTiffImages()将Word生存为tiff格局,并运行生成的文档 JoinTiffImages(SaveAsImage(document), "result.tiff", EncoderValue.CompressionLZW); System.Diagnostics.Process.Start("result.tiff"); } //自界说要领SaveAsImage()将Word文档生存为图像 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"); } //自界说要领JoinTiffImages()将Word生存为TIFF图片格局(使用指定编码器和图像编码参数) public static void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder) { 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; pages.Save(outFile, info, ep); } else { ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage); pages.SaveAdd(img, ep); } if (frame == images.Length - 1) { ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush); pages.SaveAdd(ep); } frame++; } } } }