How to convert Word to PDF/HTML/XML/XPS/SVG/EMF/EPUB/TIFF format in C #
This article mainly introduces how to convert Word to PDF/HTML/XML/XPS/SVG/EMF/EPUB/TIFF format in C#, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let's take a look at it.
Using tools: Free Spire.Doc for .NET (Community Edition)
How to use it: after downloading and installing the control, add a reference Spire.Doc.dll file to the VS console application (the dll file can be obtained in Bin under the installation folder)
1.Word to PDF/HTML/XMLusing Spire.Doc;namespace Doc2PDF {class Program {static void Main (string [] args) {/ / create a Document class object and load the Word document Document document = new Document (); document.LoadFromFile (@ "C:\ Users\ Administrator\ Desktop\ Test.docx") / / call the method SaveToFile () to convert Word to PDF, HTML and XML document.SaveToFile ("Test.PDF", FileFormat.PDF); document.SaveToFile ("Test.html", FileFormat.Html); document.SaveToFile ("Test.xml", FileFormat.Xml); / / run the generated document System.Diagnostics.Process.Start ("Test.PDF") System.Diagnostics.Process.Start ("Test.html"); System.Diagnostics.Process.Start ("Test.xml");}}
2.Word to XPSusing Spire.Doc;using System;namespace WordtoXPS_Doc {class Program {static void Main (string [] args) {/ / initialize the String class with the element String file = "sample.docx" of the Word document to be converted; / / create a Document class object and load the sample file Document doc = new Document (file) / / Save the Word file as XPS and run the generated document doc.SaveToFile ("Word2XPS.xps", FileFormat.XPS); System.Diagnostics.Process.Start ("Word2XPS.xps");}
Debug and run the project to generate documentation, as shown below:
3.Word to SVGusing Spire.Doc;namespace WordtoSVG_Doc {class Program {static void Main (string [] args) {/ / instantiate the Document class and load Word sample Document doc = new Document (); doc.LoadFromFile (@ "C:\ Users\ Administrator\ Desktop\ sample.docx") / / Save to svg format doc.SaveToFile ("result.svg", FileFormat.SVG);}
4. Word to Emfusing Spire.Doc;using System.Drawing;using System.Drawing.Imaging;namespace WordtoEmf_Doc {class Program {static void Main (string [] args) {/ / instantiate a Document class and load Word sample Document doc = new Document (); doc.LoadFromFile (@ "C:\ Users\ Administrator\ Desktop\ sample.docx", FileFormat.Docx) / / call the method SaveToImages () to convert the first page of Word to image and save it in Emf format System.Drawing.Image image = doc.SaveToImages (0, Spire.Doc.Documents.ImageType.Metafile); image.Save ("WordtoEmf.emf", ImageFormat.Emf);}
5. Word to Epubusing Spire.Doc;namespace WordtoEPUB {class Epub {static void Main (string [] args) {/ / instantiate the Document class and load Word sample Document document = new Document (); document.LoadFromFile (@ "C:\ Users\ Administrator\ Desktop\ sample.docx") / / Save to Epub format and run the generated document document.SaveToFile ("ToEpub.epub", FileFormat.EPub); System.Diagnostics.Process.Start ("ToEpub.epub");}
6. Word to Word XMLusing Spire.Doc;namespace WordtoWordXML_Doc {class Program {static void Main (string [] args) {/ / create a Document class object and load Word sample Document doc = new Document (); doc.LoadFromFile ("sample.docx"); / / call the method SaveToFile () to save Word as WordXml doc.SaveToFile ("WordToWordXML.xml", FileFormat.WordXml) }}}
7. Word to Tiffusing 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) {/ / instantiate a Document class and load Word sample Document document = new Document (@ "C:\ Users\ Administrator\ Desktop\ sample.docx") / / call the method JoinTiffImages () to save Word in tiff format and run the resulting document JoinTiffImages (SaveAsImage (document), "result.tiff", EncoderValue.CompressionLZW); System.Diagnostics.Process.Start ("result.tiff") } / / Custom method SaveAsImage () saves the Word document as an image 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; jacks +) {if (encoders [j] .MimeType = = mimeType) return encoders [j];} throw new Exception (mimeType + "mime type not found in ImageCodecInfo") } / / Custom method JoinTiffImages () saves Word to TIFF picture format (using specified encoder and image encoding parameters) 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++;}
Thank you for reading this article carefully. I hope the article "how to convert Word in C# to PDF/HTML/XML/XPS/SVG/EMF/EPUB/TIFF format" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!