一款有着强大的文档转换功能的工具,无论何时何地都会是现代办公环境极为需要的。在本篇文章中,将介绍关于Word文档的转换功能(Word转XPS/SVG/EMF/EPUB/TIFF)。希望方法中的代码能为各位开发者们提供一定的参考价值。

  使用工具:Free Spire.Doc for .NET(社区版)

  使用方法:下载安装该控件后,在VS控制台应用程序中添加引用Spire.Doc.dll文件(dll文件可在该安装文件夹下Bin中获取)

  1.Word转PDF/HTML/XML

  using Spire.Doc;

  namespace Doc2PDF

  {

  class Program

  {

  static void Main(string[] args)

  {

  //创建一个Document类对象,并加载Word文档

  Document document = new Document();

  document.LoadFromFile(@"C:\Users\Administrator\Desktop\Test.docx");

  //调用方法SaveToFile()将Word转为PDF、HTML和XML

  document.SaveToFile("Test.PDF", FileFormat.PDF);

  document.SaveToFile("Test.html", FileFormat.Html);

  document.SaveToFile("Test.xml", FileFormat.Xml);

  //运行生成的文档

  System.Diagnostics.Process.Start("Test.PDF");

  System.Diagnostics.Process.Start("Test.html");

  System.Diagnostics.Process.Start("Test.xml");

  }

  }

  }

  复制代码

  2.Word转XPS

  using Spire.Doc;

  using System;

  namespace WordtoXPS_Doc

  {

  class Program

  {

  static void Main(string[] args)

  {

  //初始化String类,元素为需要转换的Word文档

  String file = "sample.docx";

  //创建一个Document类对象,加载sample文件

  Document doc = new Document(file);

  //将Word文件保存为XPS,并运行生成的文档

  doc.SaveToFile("Word2XPS.xps", FileFormat.XPS);

  System.Diagnostics.Process.Start("Word2XPS.xps");

  }

  }

  }

  复制代码

  调试运行该项目生成文档,如下图:

  3.Word转SVG

  using Spire.Doc;

  namespace WordtoSVG_Doc

  {

  class Program

  {

  static void Main(string[] args)

  {

  //实例化Document类,并加载Word sample

  Document doc = new Document();

  doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx");

  //保存为svg格式

  doc.SaveToFile("result.svg", FileFormat.SVG);

  }

  }

  }

  复制代码

  4. Word转Emf

  using Spire.Doc;

  using System.Drawing;

  using System.Drawing.Imaging;

  namespace WordtoEmf_Doc

  {

  class Program

  {

  static void Main(string[] args)

  {

  //实例化一个Document类,并加载Word sample

  Document doc = new Document();

  doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx", FileFormat.Docx);

  //调用方法 SaveToImages()将Word第一页转为image并保存为Emf格式

  System.Drawing.Image image = doc.SaveToImages(0, Spire.Doc.Documents.ImageType.Metafile);

  image.Save("WordtoEmf.emf", ImageFormat.Emf);

  }

  }

  }

  复制代码

  5. Word转Epub

  using Spire.Doc;

  namespace WordtoEPUB

  {

  class Epub

  {

  static void Main(string[] args)

  {

  //实例化Document类,并加载Word sample

  Document document = new Document();

  document.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx");

  //保存为Epub格式,并运行生成的文档

  document.SaveToFile("ToEpub.epub", FileFormat.EPub);

  System.Diagnostics.Process.Start("ToEpub.epub");

  }

  }

  }

  复制代码

  6. Word转Word XML

  using Spire.Doc;

  namespace WordtoWordXML_Doc

  {

  class Program

  {

  static void Main(string[] args)

  {

  //创建一个Document类对象并加载Word sample

  Document doc = new Document();

  doc.LoadFromFile("sample.docx");

  //调用方法SaveToFile()保存Word为Word Xml

  doc.SaveToFile("WordToWordXML.xml", FileFormat.WordXml);

  }

  }

  }

  复制代码

  7. 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++;

  }

  }

  }

  }

  复制代码(编辑:雷林鹏 来源:网络)

最新文章

  1. 浅析linux内核中的idr机制
  2. ES6 基础版迭代器
  3. 自定义Notification
  4. 【CITE】 C#中实现拖动无边框Form窗体
  5. codeforces 260 div2 C题
  6. 第二节:Maven的运行机制
  7. MIT6.828 虚拟地址转化为物理地址——二级分页
  8. PHP学习之-Mongodb在Windows下安装及配置
  9. isapi_rewrite运行在.net framework 4.0+iis 6.0环境下404错误解决方案
  10. jQuery多文件
  11. Charles抓取https请求详解
  12. cookie的存取
  13. 201521123029《Java程序设计》第六周学习总结
  14. PHP连接SQL Server数据库
  15. 模块(相当于Java里的包)
  16. C# groupby 应用小技巧
  17. Java对象在Hibernate持久化层的状态
  18. Unity3d KeyCode 键盘各种键值详情
  19. Linux Free命令每个数字的含义 和 cache 、buffer的区别
  20. 微信公众号开发笔记1-获取Access Token

热门文章

  1. poj3449 Geometric Shapes【计算几何】
  2. C# WebBrowser的8个方法、13个属性和事件
  3. oracle Bug 4287115(ora-12083)
  4. 自旋锁原理及java自旋锁
  5. 如何打造高性能Web应用
  6. 省市县三级联动的SQL
  7. mysql修改端口经验
  8. SLAM for Dummies SLAM初学者教程 中文翻译 1到4章
  9. Spark之Task原理分析
  10. 20165324_mypwd