C# Word轉PDF/HTML/XML/XPS/SVG/EMF/EPUB/TIFF

  一款有着強大的文檔轉換功能的工具,不管什麼時候何地都會是現代辦公環境極爲須要的。在本篇文章中,將介紹關於Word文檔的轉換功能(Word轉XPS/SVG/EMF/EPUB/TIFF)。但願方法中的代碼能爲各位開發者們提供必定的參考價值。html

  使用工具:Free Spire.Doc for .NET(社區版)網絡

  使用方法:下載安裝該控件後,在VS控制檯應用程序中添加引用Spire.Doc.dll文件(dll文件可在該安裝文件夾下Bin中獲取)svg

  1.Word轉PDF/HTML/XML工具

  using Spire.Doc;編碼

  namespace Doc2PDFspa

  {調試

  class Programcode

  {orm

  static void Main(string[] args)xml

  {

  //建立一個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++;

  }

  }

  }

  }

  複製代碼(編輯:雷林鵬 來源:網絡)

相關文章
相關標籤/搜索