您好,登錄后才能下訂單哦!
這篇文章主要介紹了C#中Word如何轉PDF/HTML/XML/XPS/SVG/EMF/EPUB/TIFF格式,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
使用工具:Free Spire.Doc for .NET(社區版)
使用方法:下載安裝該控件后,在VS控制臺應用程序中添加引用Spire.Doc.dll文件(dll文件可在該安裝文件夾下Bin中獲取)
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"); } } }
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"); } } }
調試運行該項目生成文檔,如下圖:
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); //調用方法 SaveToImages()將Word第一頁轉為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) { //創建一個Document類對象并加載Word sample Document doc = new Document(); doc.LoadFromFile("sample.docx"); //調用方法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"); //調用方法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++; } } } }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“C#中Word如何轉PDF/HTML/XML/XPS/SVG/EMF/EPUB/TIFF格式”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。