這篇文章主要介紹了C#中Word如何轉(zhuǎn)PDF/HTML/XML/XPS/SVG/EMF/EPUB/TIFF格式,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
10年積累的成都網(wǎng)站設(shè)計、網(wǎng)站制作、外貿(mào)營銷網(wǎng)站建設(shè)經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站設(shè)計后付款的網(wǎng)站建設(shè)流程,更有樂東黎族免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
使用工具:Free Spire.Doc for .NET(社區(qū)版)
使用方法:下載安裝該控件后,在VS控制臺應(yīng)用程序中添加引用Spire.Doc.dll文件(dll文件可在該安裝文件夾下Bin中獲取)
using Spire.Doc; namespace Doc2PDF { class Program { static void Main(string[] args) { //創(chuàng)建一個Document類對象,并加載Word文檔 Document document = new Document(); document.LoadFromFile(@"C:\Users\Administrator\Desktop\Test.docx"); //調(diào)用方法SaveToFile()將Word轉(zhuǎn)為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"); } } }
using Spire.Doc; using System; namespace WordtoXPS_Doc { class Program { static void Main(string[] args) { //初始化String類,元素為需要轉(zhuǎn)換的Word文檔 String file = "sample.docx"; //創(chuàng)建一個Document類對象,加載sample文件 Document doc = new Document(file); //將Word文件保存為XPS,并運行生成的文檔 doc.SaveToFile("Word2XPS.xps", FileFormat.XPS); System.Diagnostics.Process.Start("Word2XPS.xps"); } } }
調(diào)試運行該項目生成文檔,如下圖:
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); } } }
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); //調(diào)用方法 SaveToImages()將Word第一頁轉(zhuǎn)為image并保存為Emf格式 System.Drawing.Image image = doc.SaveToImages(0, Spire.Doc.Documents.ImageType.Metafile); image.Save("WordtoEmf.emf", ImageFormat.Emf); } } }
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"); } } }
using Spire.Doc; namespace WordtoWordXML_Doc { class Program { static void Main(string[] args) { //創(chuàng)建一個Document類對象并加載Word sample Document doc = new Document(); doc.LoadFromFile("sample.docx"); //調(diào)用方法SaveToFile()保存Word為Word Xml doc.SaveToFile("WordToWordXML.xml", FileFormat.WordXml); } } }
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"); //調(diào)用方法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圖片格式(使用指定編碼器和圖像編碼參數(shù)) 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++; } } } }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“C#中Word如何轉(zhuǎn)PDF/HTML/XML/XPS/SVG/EMF/EPUB/TIFF格式”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!