Conversion

Conversion (31)

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.

Convert Word to PDF/A in C#

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 Word to PDF/A in C#

HTML is the standard format for web pages and online content. However, there are many scenarios where you may need to convert HTML documents into other file formats, such as PDF, XPS, and XML. Whether you're looking to generate a printable version of a web page, share HTML content in a more universally accepted format, or extract data from HTML for further processing, being able to reliably convert HTML documents to these alternate formats is an important skill to have. In this article, we will demonstrate how to convert HTML to PDF, XPS, and XML in C# 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 HTML to PDF in C#

Converting HTML to PDF offers several advantages, including enhanced portability, consistent formatting, and easy sharing. PDF files retain the original layout, styling, and visual elements of the HTML content, ensuring that the document appears the same across different devices and platforms.

You can use the Document.SaveToFile(string filename, FileFormat.PDF) method to convert an HTML file to PDF format. The detailed steps are as follows.

  • Create an instance of the Document object.
  • Load an HTML file using the Document.LoadFromFile() method.
  • Save the HTML file to PDF format using the Document.SaveToFile(string filename, FileFormat.PDF) method.
  • C#
using Spire.Doc;
using Spire.Doc.Documents;

namespace ConvertHtmlToPdf
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Create an instance of the Document class
            Document doc = new Document();
            // Load an HTML file
            doc.LoadFromFile("Sample.html", FileFormat.Html, XHTMLValidationType.None);

            //Convert the HTML file to PDF format
            doc.SaveToFile("HtmlToPDF.pdf", FileFormat.PDF);
            doc.Close();
        }
    }
}

C#: Convert HTML to PDF, XPS and XML

Convert HTML String to PDF in C#

In addition to converting HTML files to PDF, you are also able to convert HTML strings to PDF. Spire.Doc for .NET provides the Paragraph.AppendHTML() method to add an HTML string to a Word document. Once the HTML string has been added, you can convert the result document to PDF using the Document.SaveToFile(string filename, FileFormat.PDF) method. The detailed steps are as follows.

  • Create an instance of the Document object.
  • Add a paragraph to the document using the Document.AddSection().AddParagraph() method.
  • Append an HTML string to the paragraph using the Paragraph.AppendHTML() method.
  • Save the document to PDF format using the Document.SaveToFile(string filename, FileFormat.PDF) method.
  • C#
using Spire.Doc;
using Spire.Doc.Documents;

namespace ConvertHtmlStringToPdf
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Create an instance of the Document class
            Document doc = new Document();
            // Add a paragraph to the document
            Paragraph para = doc.AddSection().AddParagraph();
            // Specify the HTML string
            string htmlString = @"<h1>This is a Heading</h1>
                                  <p>This is a paragraph.</p>
                                  <ul>
                                    <li>Item 1</li>
                                    <li>Item 2</li>
                                    <li>Item 3</li>
                                  </ul>";

            // Append the HTML string to the paragraph
            para.AppendHTML(htmlString);

            // Convert the document to PDF format
            doc.SaveToFile("HtmlStringToPDF.pdf", FileFormat.PDF);
            doc.Close();
        }
    }
}

C#: Convert HTML to PDF, XPS and XML

Convert HTML to XPS in C#

XPS, or XML Paper Specification, is an alternative format to PDF that provides similar functionality and advantages. Converting HTML to XPS ensures the preservation of document layout, fonts, and images while maintaining high fidelity. XPS files are optimized for printing and can be viewed using XPS viewers or Windows' built-in XPS Viewer.

By using the Document.SaveToFile(string filename, FileFormat.XPS) method, you can convert HTML files to XPS format with ease. The detailed steps are as follows.

  • Create an instance of the Document object.
  • Load an HTML file using the Document.LoadFromFile() method.
  • Save the HTML file to XPS format using the Document.SaveToFile(string filename, FileFormat.XPS) method.
  • C#
using Spire.Doc;
using Spire.Doc.Documents;

namespace ConvertHtmlToXps
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Create an instance of the Document class
            Document doc = new Document();
            // Load an HTML file
            doc.LoadFromFile("Sample.html", FileFormat.Html, XHTMLValidationType.None);

            //Convert the HTML file to XPS format
            doc.SaveToFile("HtmlToXPS.xps", FileFormat.XPS);
            doc.Close();
        }
    }
}

C#: Convert HTML to PDF, XPS and XML

Convert HTML to XML in C#

Converting HTML to XML unlocks the potential for data extraction, manipulation, and integration with other systems. XML is a flexible and extensible markup language that allows for structured representation of data. By converting HTML to XML, you can extract specific elements, organize data hierarchically, and perform data analysis or integration tasks using XML processing tools and techniques.

To convert HTML files to XML format, you can use the Document.SaveToFile(string filename, FileFormat.Xml) method. The detailed steps are as follows.

  • Create an instance of the Document object.
  • Load an HTML file using the Document.LoadFromFile() method.
  • Save the HTML file to XML format using the Document.SaveToFile(string filename, FileFormat.Xml) method.
  • C#
using Spire.Doc;
using Spire.Doc.Documents;

namespace ConvertHtmlToXml
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Create an instance of the Document class
            Document doc = new Document();
            // Load an HTML file
            doc.LoadFromFile("Sample.html", FileFormat.Html, XHTMLValidationType.None);

            //Convert the HTML file to XML format
            doc.SaveToFile("HtmlToXML.xml", FileFormat.Xml);
            doc.Close();
        }
    }
}

C#: Convert HTML to PDF, XPS and XML

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.

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.

[C#]
Document document = new Document();

Step 2: Set the value of imageEmbedded attribute.

[C#]
doc.HtmlExportOptions.ImageEmbedded=true;

Step 3: Save word document to HTML.

[C#]
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.

[C#]
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#

2013-11-07 07:22:24 Written by Administrator

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.

[C#]
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:

word to emf c#

C#/VB.NET: Convert Word to XPS

2022-08-05 06:45:00 Written by daisy zhang

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);
        }
    }
}

C#/VB.NET: Convert Word to 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

2022-07-22 03:32:00 Written by support iceblue

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);
        }
    }
}

C#/VB.NET: Convert Word to 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);
        }
    }
}

C#/VB.NET: Convert Word to EPUB

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.

A common use of HTML is to display data and information on websites, web applications, and a variety of platforms. It is sometimes necessary to convert HTML to images like JPG, PNG, TIFF, BMP etc. since images are difficult to modify and can be accessed by virtually anyone. This article will show you how to perform the HTML to images conversion programmatically 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 HTML to Images

Spire.Doc for .NET offers the Document.SaveToImages() method to convert HTML to Images. Here are detailed steps.

  • Create a Document instance.
  • Load an HTML sample document using Document.LoadFromFile() method.
  • Save the document as an image using Document.SaveToImages() method.
  • C#
  • VB.NET
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing.Imaging;

namespace HTMLToImage
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document mydoc = new Document();
           
            //Load an HTML sample document
            mydoc.LoadFromFile(@"sample.html", FileFormat.Html, XHTMLValidationType.None);
           
            //Save to image. You can convert HTML to BMP, JPEG, PNG, GIF, Tiff etc
            Image image = mydoc.SaveToImages(0, ImageType.Bitmap);
            image.Save("HTMLToImage.png", ImageFormat.Png);
        }
    }
}

C#/VB.NET: Convert HTML to Images

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#: Convert Word to TIFF

2024-06-26 02:30:00 Written by support iceblue

Converting Word to TIFF can be useful in various scenarios. TIFF files have high quality and wide support, making them versatile for sharing documents. The conversion also "flattens" the Word document, preserving the layout so it appears exactly as the original. This can be helpful when the document needs to be incorporated into another application or workflow that requires image-based files.

In this article, you will learn how to convert Word to TIFF using C# and the Spire.Doc for .NET library.

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 TIFF in C#

Spire.Doc for .NET provides the Document.SaveToImages() method, which enables developers to convert an entire document into an array of images. Subsequently, these individual images can be combined into a single TIFF image using the built-in .NET library.

The steps to convert Word to TIFF using C# are as follows.

  • Create a Document object.
  • Load a Word document using Document.LoadFile() method.
  • Convert the document into an array of images using Document.SaveToImages() method.
  • Combine these images into a single TIFF file using the custom method ConvertImagesToTiff().
  • C#
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
using System.Drawing.Imaging;

namespace WordToTiff
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a Document object
            Document doc = new Document();

            // Load a Word document
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx");

            // Convert the whole document to images
            Image[] images = doc.SaveToImages(ImageType.Bitmap);

            // Convert multiple images into a TIFF file
            ConvertImagesToTiff(images, "ToTiff.tiff", EncoderValue.CompressionLZW);

            // Dispose resource
            doc.Dispose();
        }

        private static ImageCodecInfo GetEncoderInfo(string mimeType)
        {
            // Get the image encoders
            ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
            for (int j = 0; j < encoders.Length; j++)
            {
                // Find the encoder that matches the specified MIME type
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
        }

        public static void ConvertImagesToTiff(Image[] images, string outFile, EncoderValue compressEncoder)
        {
            // Set the encoder parameters 
            Encoder enc = Encoder.SaveFlag;
            EncoderParameters ep = new EncoderParameters(2);
            ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
            ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)compressEncoder);

            // Get the first image
            Image pages = images[0];

            // Create a variable
            int frame = 0;

            // Get an ImageCodecInfo object for processing TIFF image codec information
            ImageCodecInfo info = GetEncoderInfo("image/tiff");

            // Iterate through each Image
            foreach (Image img in images)
            {
                // If it's the first frame, save it to the output file with specified encoder parameters
                if (frame == 0)
                {
                    pages = img;
                    pages.Save(outFile, info, ep);
                }
                else
                {
                    // Save the intermediate frames
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);

                    pages.SaveAdd(img, ep);
                }

                // If it's the last frame, flush the encoder parameters and close the file
                if (frame == images.Length - 1)
                {
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
                    pages.SaveAdd(ep);
                }
                frame++;
            }
        }
    }
}

C#: Convert Word to TIFF

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#: Convert RTF to HTML, Image

2024-09-10 02:49:00 Written by support iceblue

RTF, or Rich Text Format, is a file format designed for storing text and formatting information. While processing RTF files, sometimes you might need to convert them into a more web-friendly format such as HTML, or convert to images for better sharing and archiving purposes. In this article, you will learn how to convert RTF to HTML or images in C# 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 HTML in C#

Converting RTF to HTML ensures that the document can be easily viewed and edited in any modern web browser without requiring any additional software.

With Spire.Doc for .NET, you can achieve RTF to HTML conversion through the Document.SaveToFile(string fileName, FileFormat.Html) method. The following are the detailed steps.

  • Create a Document instance.
  • Load an RTF document using Document.LoadFromFile() method.
  • Save the RTF document in HTML format using Document.SaveToFile(string fileName, FileFormat.Html) method.
  • C#
using Spire.Doc;

namespace ConvertRtfToHtml
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();

            //Load an RTF document 
            document.LoadFromFile("input.rtf");

            //Save as HTML format
            document.SaveToFile("RtfToHtml.html", FileFormat.Html);
        }
    }
}

C#: Convert RTF to HTML, Image

Convert RTF to Image in C#

To convert RTF to images, we can use the Document.SaveToImages() method to convert an RTF file into individual Bitmap or Metafile images. Then, the Bitmap or Metafile images can be saved as a BMP, EMF, JPEG, PNG, GIF, or WMF format files. The following are the detailed steps.

  • Create a Document object.
  • Load an RTF document using Document.LoadFromFile() method.
  • Convert the document to images using Document.SaveToImages() method.
  • Iterate through the converted image, and then save each as a PNG file using Image[].Save(string fileName, ImageFormat format) method.
  • C#
using Spire.Doc;
using System.Drawing.Imaging;
using System.Drawing;
using Spire.Doc.Documents;

namespace ConvertRtfToImage

{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();

            //Load an RTF document 
            document.LoadFromFile("input.rtf");

            //Convert the RTF document to images
            Image[] images = document.SaveToImages(ImageType.Bitmap);

            // Iterate through the image collection
            for (int i = 0; i < images.Length; i++)
            {
                //Save the image as png format
                string outputfile = string.Format("image-{0}.png", i);
                images[i].Save(outputfile, ImageFormat.Png);
            }
        }
    }
}

C#: Convert RTF to HTML, Image

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.

Rich Text Format (RTF) is a proprietary text file format which can serve as an exchange format between word processing programs from different manufacturers on different operating systems. Rich text documents include page formatting options, such as custom page margins, line spacing, and tab widths. With rich text, it's easy to create columns, add tables, and format your documents in a way that makes them easier to read. This article will demonstrate how to convert Word Doc/Docx to RTF files and convert RTF to Word Doc/Docx with the help of 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 DLLs files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Convert Word Doc/Docx to RTF

The following steps show you how to convert Word to RTF using Spire.Doc for .NET.

  • Create a Document instance.
  • Load a Word sample document using Document.LoadFromFile() method.
  • Save the document as an RTF file using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace ConvertToRtf
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Word document instance
            Document document = new Document();

            //Load a Word sample document
            document.LoadFromFile("sample.docx");

            // Save the document to RTF
            document.SaveToFile("Result.rtf", FileFormat.Rtf);
        }

    }
}

C#/VB.NET: Convert RTF to Word Doc/Docx and Vice Versa

Convert RTF to Word Doc/Docx

The steps to convert an RTF document to Word Doc/Docx is very similar with that of the above example:

  • Create a Document instance.
  • Load an RTF document using Document.LoadFromFile() method.
  • Save the RTF document to Doc/Docx format using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using System;

public class RtfToDocDocx
{

    public static void Main(String[] args)
    {
        // Create a Document instance
        Document document = new Document();
        // Load an RTF document
        document.LoadFromFile("input.rtf", FileFormat.Rtf);
        // Save the document to Doc
        document.SaveToFile("toDoc.doc", FileFormat.Doc);
        // Save the document to Docx
        document.SaveToFile("toDocx.docx", FileFormat.Docx2013);
    }
}

C#/VB.NET: Convert RTF to Word Doc/Docx and Vice Versa

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.

Page 2 of 3
page 2