C # Word turns into multiple format files (Word to Xps/svg/emf/epub/tiff)

Source: Internet
Author: User

A powerful tool for document conversion, whenever and wherever the modern office environment is most needed. In this article, you will continue to describe the conversion functionality for Word documents (Word to Xps/svg/emf/epub/tiff) the code in the Hope method will provide some reference value to the developers.

PS: More word conversion features can be found in these two articles

    • Word Turn Html/xml/pdf/image
    • Turn Word to TXT

using the tool : Freespire.doc for. NET

usage : After downloading the control, add a reference Spire.Doc.dll file (DLL file can be obtained in the bin of the installation folder) in the VS console application

1. turn Word to XPS

usingSpire.doc;usingSystem;namespacewordtoxps_doc{classProgram {Static voidMain (string[] args) {            //initializes the string class, which is the Word document that needs to be convertedString file ="Sample.docx"; //Create a document class object to load the sample fileDocument doc =NewDocument (file); //Save the Word file as XPS and run the resulting documentDoc. SaveToFile ("Word2xps.xps", Fileformat.xps); System.Diagnostics.Process.Start ("Word2xps.xps"); }    }}

Debug run the project build document, such as:

2. Word to SVG

usingSpire.doc;namespacewordtosvg_doc{classProgram {Static voidMain (string[] args) {            //instantiate the document class and load Word sampleDocument doc =NewDocument (); Doc. LoadFromFile (@"C:\Users\Administrator\Desktop\sample.docx"); //Save as SVG formatDoc. SaveToFile ("Result.svg", Fileformat.svg); }    }}

3. Word turns Emf

usingSpire.doc;usingSystem.Drawing;usingSystem.Drawing.Imaging;namespacewordtoemf_doc{classProgram {Static voidMain (string[] args) {            //instantiate a document class, and load Word sampleDocument doc =NewDocument (); Doc. LoadFromFile (@"C:\Users\Administrator\Desktop\sample.docx", Fileformat.docx); //Call Method Savetoimages () Convert Word first page to image and save as EMF formatSystem.Drawing.Image Image = doc. Savetoimages (0, Spire.Doc.Documents.ImageType.Metafile); Image. Save ("wordtoemf.emf", IMAGEFORMAT.EMF); }    }}

4. turn Word to Epub

usingSpire.doc;namespacewordtoepub{classEpub {Static voidMain (string[] args) {            //instantiate the document class and load Word sampleDocument document =NewDocument (); Document. LoadFromFile (@"C:\Users\Administrator\Desktop\sample.docx"); //Save as EPUB format and run the generated documentDocument. SaveToFile ("toepub.epub", fileformat.epub); System.Diagnostics.Process.Start ("toepub.epub"); }    }}

5. word to word XML

usingSpire.doc;namespacewordtowordxml_doc{classProgram {Static voidMain (string[] args) {            //Create a document class object and load Word sampleDocument doc =NewDocument (); Doc. LoadFromFile ("Sample.docx"); //Call Method SaveToFile () to save Word as word XmlDoc. SaveToFile ("Wordtowordxml.xml", Fileformat.wordxml); }    }}

6. Word to Tiff

usingSpire.doc;usingSpire.Doc.Documents;usingSystem;usingSystem.Drawing;usingSystem.Drawing.Imaging;namespaceconvert_word_to_tiff{classProgram {Static voidMain (string[] args) {            //instantiate a document class, load Word sampleDocument document =NewDocument (@"C:\Users\Administrator\Desktop\sample.docx"); //Call Method Jointiffimages () to save Word as a TIFF format and run the resulting documentJointiffimages (Saveasimage (document),"Result.tiff", ENCODERVALUE.COMPRESSIONLZW); System.Diagnostics.Process.Start ("Result.tiff"); }        //Custom Method Saveasimage () to save a Word document as an image        Private Staticimage[] Saveasimage (document document) {image[] images=document.            Savetoimages (IMAGETYPE.BITMAP); returnimages; }        Private StaticImageCodecInfo Getencoderinfo (stringMimeType) {imagecodecinfo[] encoders=imagecodecinfo.getimageencoders ();  for(intj =0; J < Encoders. Length; J + +)            {                if(Encoders[j]. MimeType = =MimeType)returnEncoders[j]; }            Throw NewException (MimeType +"MIME type not found in ImageCodecInfo"); }        //Custom Method Jointiffimages () saves word as a TIFF picture format (using the specified encoder and image encoding parameters)         Public Static voidJointiffimages (image[] images,stringOutFile, Encodervalue compressencoder) {System.Drawing.Imaging.Encoder enc=System.Drawing.Imaging.Encoder.SaveFlag; EncoderParameters EP=NewEncoderParameters (2); Ep. param[0] =NewEncoderparameter (ENC, (Long) encodervalue.multiframe); Ep. param[1] =NewEncoderparameter (System.Drawing.Imaging.Encoder.Compression, (Long) compressencoder); Image pages= images[0]; intframe =0; ImageCodecInfo Info= Getencoderinfo ("Image/tiff"); foreach(Image imginchimages) {                if(frame = =0) {pages=img; Pages.                Save (OutFile, info, EP); }                Else{EP. param[0] =NewEncoderparameter (ENC, (Long) encodervalue.framedimensionpage); Pages.                SaveAdd (IMG, EP); }                if(frame = = images. Length-1) {EP. param[0] =NewEncoderparameter (ENC, (Long) Encodervalue.flush); Pages.                SaveAdd (EP); } Frame++; }        }    }}

The above is a specific description of the word into other format files, the code in the method for reference. Welcome Reprint (Reproduced please specify the source)

C # Word turns into multiple format files (Word to Xps/svg/emf/epub/tiff)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.