How to convert Word to Word XML in C#, VB.NET
Simple introduction about Word XML
Word XML is a special XML format, which makes Word be able to manipulate the Word documents stored in XML format. It can be divided into two types: WordML(supported by Word 2003) and WordXML(supported by Word 2007). If external applications support Word XML and the generated data follow the Word XML structure, then the data can be processed by Word. In this way, Word XML has become the bridge between Word and other external applications, any XML- formatted document based on Word XML structure can be opened, edited and saved in Word.
Using C#/VB.NET to convert Word to Word XML via Spire.Doc
Spire.Doc enables users to convert word document to Word XML format easily by using the doc.SaveToFile() method. Now, please follow the detail steps below:
Note: Before start, please download Spire.Doc and install it correctly, then add Spire.Doc.dll file from Bin folder as the reference of your project.
This is the screenshot of the original word document:
Step 1: Create a new document instance.
Document doc = new Document();
Step 2: Load the sample word document from file.
doc.LoadFromFile("Spire.Doc for .NET.docx");
Step 3: Save the word document as Word XML format.
For word 2003: doc.SaveToFile("DocxToWordML.xml", FileFormat.WordML); For word 2007: doc.SaveToFile("DocxToWordXML.xml", FileFormat.WordXml);
Effective screenshot:
Full codes:
using Spire.Doc; namespace Convert_Word_to_Word_XML { class Program { static void Main(string[] args) { Document doc = new Document(); doc.LoadFromFile("Spire.Doc for .NET.docx"); doc.SaveToFile("DocxToWordML.xml", FileFormat.WordML); //doc.SaveToFile("DocxToWordXML.xml", FileFormat.WordXml); } } }
Imports Spire.Doc Namespace Convert_Word_to_Word_XML Class Program Private Shared Sub Main(args As String()) Dim doc As New Document() doc.LoadFromFile("Spire.Doc for .NET.docx") doc.SaveToFile("DocxToWordML.xml", FileFormat.WordML) 'doc.SaveToFile("DocxToWordXML.xml", FileFormat.WordXml); End Sub End Class End Namespace
C#/VB.NET: Convert RTF to PDF
RTF (Rich Text Format) is a cross-platform document developed by Microsoft in the 1980s. RTF can be opened by most word processors, and it is also convenient for editing. But when it comes to sharing and printing documents in daily work, it’s more recommended to convert the RTF to PDF for further processing. In this article, you will learn how to convert RTF to PDF programmatically using Spire.Doc for .NET.
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Convert RTF to PDF in C# and VB.NET
Spire.Doc for .NET enables you to directly load a file with .rtf extension and then convert it to PDF with only three lines of code. The detailed steps are as follows.
- Create a Document instance.
- Load a sample RTF document using Document.LoadFromFile() method.
- Save the document as a PDF file using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc; namespace RTFtoPDF { class Program { static void Main(string[] args) { //Create a Document instance Document doc = new Document(); //Load a sample RTF document doc.LoadFromFile("sample.rtf", FileFormat.Rtf); //Save it to PDF doc.SaveToFile("RTFtoPDF.pdf", FileFormat.PDF); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
How to convert RTF to Image and reset image resolution
Spire.Doc has a powerful ability to operate RTF file formats in C# and VB.NET. By using Spire.Doc, developers can convert RTF to PDF, HTML and word documents in .doc, .docx. This article will show you how to convert RTF into image and then reset the image resolution.
Download and install Spire.Doc for .NET and then add Spire.Doc.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll". Here comes to the details of how to convert RTF into PNG and reset image resolution in C#.
Step 1: Create a new document and load from file.
Document doc = new Document(); doc.LoadFromFile("sample.rtf", FileFormat.Rtf);
Step 2: Save the RTF to image
Image[] images = doc.SaveToImages(Spire.Doc.Documents.ImageType.Metafile);
Step 3: Traverse the elements in the list of images and save them into .Png format.
for (int i = 0; i < images.Length; i++) { Metafile mf = images[i] as Metafile; Image newimage = ResetResolution(mf, 200); string outputfile = String.Format("image-{0}.png", i); newimage.Save(outputfile, System.Drawing.Imaging.ImageFormat.Png); }
Step 4: Set the image resolution call the method: ResetResolution.
public static Image ResetResolution(Metafile mf, float resolution) { int width = (int)(mf.Width * resolution / mf.HorizontalResolution); int height = (int)(mf.Height * resolution / mf.VerticalResolution); Bitmap bmp = new Bitmap(width, height); bmp.SetResolution(resolution, resolution); using (Graphics g = Graphics.FromImage(bmp)) { g.DrawImage(mf, Point.Empty); } return bmp; }
Effective screenshot of the image before reset the image resolution:
The image after reset the image resolution:
Full codes:
using Spire.Doc; using System.Drawing; using System.Drawing.Imaging; namespace RTFtoImage { class Program { static void Main(string[] args) { //Create a new document and load from file. Document doc = new Document(); doc.LoadFromFile("sample.rtf", FileFormat.Rtf); // save the RTF to image Image[] images = doc.SaveToImages(Spire.Doc.Documents.ImageType.Metafile); for (int i = 0; i < images.Length; i++) { Metafile mf = images[i] as Metafile; Image newimage = ResetResolution(mf, 200); string outputfile = String.Format("image-{0}.png", i); newimage.Save(outputfile, System.Drawing.Imaging.ImageFormat.Png); } } //set the image resolution by the ResetResolution() method public static Image ResetResolution(Metafile mf, float resolution) { int width = (int)(mf.Width * resolution / mf.HorizontalResolution); int height = (int)(mf.Height * resolution / mf.VerticalResolution); Bitmap bmp = new Bitmap(width, height); bmp.SetResolution(resolution, resolution); using (Graphics g = Graphics.FromImage(bmp)) { g.DrawImage(mf, Point.Empty); } return bmp; } } }
How to Convert Word to PDF/A in C# ?
PDF/A is an ISO-standardized version of the Portable Document Format (PDF) specialized for the digital preservation of electronic documents. It is widely used for long term archiving for PDF format. This article mainly shows how to convert word document (doc and docx) to PDF/A in C# by using Spire.Doc.
Make sure Spire.Doc for .NET Version 5.0.26 (or above) has been installed correctly and then add Spire.Doc.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll".
First, check the original word document that will be converted to PDF/A.
Here comes to the details of how developers convert word document to PDF/A directly by using Spire.Doc:
Step 1: Load a word document from the file.
Document document = new Document(); document.LoadFromFile(@"D:\test.docx",FileFormat.Docx);
Step 2: Sets the Pdf document's Conformance-level to PDF_A1B.
ToPdfParameterList toPdf = new ToPdfParameterList(); toPdf.PdfConformanceLevel = Spire.Pdf.PdfConformanceLevel.Pdf_A1B;
Step 3: Save word document to PDF
document.SaveToFile("result.Pdf",toPdf);
Please check the effective screenshot of the result PDF in PDF/A format.
Convert HTML file to PDF and XPS in C#
HTML file is widely used to collect data and for the security reason, we need to convert it to printable documents in PDF and XPS formats. You can easily convert HTML page in URL to PDF in high quality by using Spire.PDF. This article will focus on demonstrate how to convert HTML files into the printable document, both in PDF and XPS by the help of Spire.Doc.
First, check the html file that will be converted to PDF and XPS.
Secondly, download Spire.Doc and install on your system. The Spire.Doc installation is clean, professional and wrapped up in a MSI installer.
Then adds Spire.Doc.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll".
Now it comes to the steps of how to convert HTML into PDF and XPS.
Step 1: Load a HTML file from the file.
Document document = new Document(); document.LoadFromFile("Good.htm", FileFormat.Html, XHTMLValidationType.None) ;
Step 2: Save HTML to the file format in PDF and XPS.
//Save html to PDF. document.SaveToFile("Sample.pdf", FileFormat.PDF); //Save html to XPS. document.SaveToFile("Sample.xps", FileFormat.XPS);
After debugging, please check the following PDF and XPS file as the result.
Full codes:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace HTML2PDFXPS { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Document document = new Document(); document.LoadFromFile("Good.htm", FileFormat.Html, XHTMLValidationType.None); //Save html to PDF. document.SaveToFile("Sample.pdf", FileFormat.PDF); } private void button2_Click(object sender, EventArgs e) { Document document = new Document(); document.LoadFromFile("Good.htm", FileFormat.Html, XHTMLValidationType.None); //Save html to PDF. document.SaveToFile("Sample.xps", FileFormat.XPS); } } }
How to Convert HTML to XML in C#, VB.NET
Office OpenXML becomes the technology of choice for delivering structured data on the Web, working hand-in-hand with HTML and fully complementing HTML. Consequently, we need to convert HTML to Office OpenXML at some point at work. This article mainly talks about the conversion process through a professional Word .NET library Spire.Doc.
First we need to complete the preparatory work before the procedure:
- Download the Spire.Doc and install it on your machine.
- Add the Spire.Doc.dll files as reference.
- Open bin folder and select the three dll files under .NET 4.0.
- Right click property and select properties in its menu.
- Set the target framework as .NET 4.
- Add Spire.Doc as namespace.
The following steps will show you how to do this with ease:
Step 1: Create a Word document.
Document doc = new Document();
Step 2: Load the HTML file.
doc.LoadFromFile("Sample.html");
Step 3: Save the HTML as the XML file.
doc.SaveToFile("test.xml", FileFormat.Xml);
Here comes to the full C# and VB.NET code
using Spire.Doc; namespace HTMLXML { class Program { static void Main(string[] args) { Document doc = new Document(); doc.LoadFromFile("Sample.html"); doc.SaveToFile("test.xml", FileFormat.Xml); } } }
Imports Spire.Doc Namespace HTMLXML Class Program Private Shared Sub Main(args As String()) Dim doc As New Document() doc.LoadFromFile("Sample.html") doc.SaveToFile("test.xml", FileFormat.Xml) End Sub End Class End Namespace
Preview of original HTML file.
Preview of generated Office OpenXML file.
Convert Word from/to HTML with Embedded Image
Convert Word document to HTML is popular and widely used by programmers and developers. With the help of Spire.Doc for .NET, a professional word component, without installing MS Word, developers can convert word to html with only two lines of key code in C#. At the same time, Spire.Doc supports convert HTML to word document easily and quickly.
This article still focuses on convert word from/to HTML, while it mainly about the supports of embed image in the word document and HTML. With the improvements of Spire.Doc (starts from Spire.Doc V. 4.9.32), now it supports the new function of ImageEmbedded.
Please download Spire.Doc (version 4.9.32 or above) with .NET framework together and follow the simple steps as below:
Convert Word to HTML in C#:
Step 1: Create the word document.
Document document = new Document();
Step 2: Set the value of imageEmbedded attribute.
doc.HtmlExportOptions.ImageEmbedded=true;
Step 3: Save word document to HTML.
doc.SaveToFile("result.html",FileFormat.Html);
Spire.Doc also supports load the result HTML page and convert it into word document in only three lines of codes as below.
doc.SaveToFile("htmltoword.docx",FileFormat.Docx);
Besides conversion of word from/to HTML, Spire.Doc also supports Convert Word to PDF, Convert Word to Image and Convert Word to XPS in C#.
How to Convert Word to Emf in C#
The article will introduce an easy way to convert Word to Emf by a powerful and independent Word .NET component called Spire.Doc, without Microsoft Word installed on the machine. It also offers support for converting Word and HTML to frequently-used image formats like Jpeg, Png, Gif, Bmp and Tiff, etc. Just click here to have a try.
Emf is a file extension for Enhanced MetaFile, used as a graphics language for printer drivers by the Windows operating system. In 1993, a newer version with additional commands 32-bit version of Win32/GDI introduced the Enhanced Metafile (Emf). Microsoft also recommends enhanced-format (Emf) functions to be used instead of rarely being used Windows-format (WMF) functions.
Spire.Doc presents almost the easiest solution to convert Word to Emf through the following 5 lines simple code.
using Spire.Doc; using System.Drawing.Imaging; namespace DOCEMF { class Program { static void Main(string[] args) { // create an instance of Spire.Doc.Document Document doc = new Document(); // load the file base on a specified file name doc.LoadFromFile(@"../../Original Word.docx", FileFormat.Docx); //convert the first page of document to image System.Drawing.Image image = doc.SaveToImages(0, Spire.Doc.Documents.ImageType.Metafile); // save the document object to Emf file image.Save(@"../../Convert Word to Image.emf", ImageFormat.Emf); //close the document doc.Close(); } } }
Check the effect screenshot below:
C#/VB.NET: Convert Word to XPS
XPS (XML Paper Specification) is a fixed-layout document format designed to preserve document fidelity and provide device-independent document appearance. It is similar to PDF, but is based on XML rather than PostScript. If you want to save a Word document to a fixed-layout file format, XPS would be an option. This article will demonstrate how to convert Word documents to XPS in C# and VB.NET using Spire.Doc for .NET.
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Convert Word to XPS in C# and VB.NET
The following are the detailed steps to convert a Word document to XPS using Spire.Doc for .NET:
- Initialize an instance of Document class.
- Load a Word document using Document.LoadFromFile() method.
- Save the Word document to XPS using Document.SaveToFile(string filePath, FileFormat fileFormat) method.
- C#
- VB.NET
using Spire.Doc; namespace ConvertWordToXps { class Program { static void Main(string[] args) { //Create a Document instance Document doc = new Document(); //Load a Word document doc.LoadFromFile("Sample.docx"); //convert the document to XPS doc.SaveToFile("ToXPS.xps", FileFormat.XPS); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
C#/VB.NET: Convert Word to EPUB
EPUB is a standard file format for publishing eBooks or other electronic documents. The content in an EPUB file is reflowable, which means that the content automatically adjusts itself to fit the screen it is being displayed on. People who want to publish their eBooks may need to convert their works stored in Word documents to EPUB files. In this article, you will learn how to programmatically achieve this task using Spire.Doc for .NET.
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Convert Word to EPUB
The detailed steps are as follows:
- Create a Document instance.
- Load a sample Word document using Document.LoadFromFile() method.
- Save the document to EPUB using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc; namespace WordtoEPUB { class Epub { static void Main(string[] args) { //Create a Document instance Document document = new Document(); //Load a sample Word document document.LoadFromFile("demo.docx"); //Convert the Word document to EPUB document.SaveToFile("ToEpub.epub", FileFormat.EPub); } } }
Convert Word to EPUB with a Cover Image
The detailed steps are as follows.
- Create a Document instance.
- Load a sample Word document using Document.LoadFromFile() method.
- Create a DocPicture instance, and then load an image using DocPicture.LoadImage() method.
- Save the Word document to EPUB with cover image using Document.SaveToEpub(String, DocPicture) method.
- C#
- VB.NET
using Spire.Doc; using Spire.Doc.Fields; using System.Drawing; namespace ConvertWordToEpubWithCoverImage { class Program { static void Main(string[] args) { //Create a Document instance Document doc = new Document(); //Load a sample Word document doc.LoadFromFile("demo.docx"); //Create a DocPicture instance DocPicture picture = new DocPicture(doc); //Load an image picture.LoadImage(Image.FromFile("CoverImage.png")); //Save the Word document to EPUB with cover image doc.SaveToEpub("ToEpubWithCoverImage.epub", picture); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.