[Good Text Translation] Step by Step teach you to use Spire. Doc to convert the Word Document Format

Source: Internet
Author: User

Background:

This article attempts to prove and review the format conversion capability of Spire. Doc. For a long time, developers had to install the Office software on the server to operate the document. First, this is a bad design and practice. Second, Microsoft never intended to use Office as a server component. It is also used to interpret and operate documents on the server. As a result, a class library like Spire. Doc is generated. When we discuss this issue, it is worth mentioning that Office Open Xml. office Open XML (also known as OOXML or OpenXML) is a compressed XML-based file format developed by Microsoft to present workbooks and display charts, demonstration and text processing. In November 2005, Microsoft announced that, as a major partner of ECMA International, it standardized its XML-based file format, known as "Office Open XML ". The introduction of Open XML makes the structure of office documents more standardized, and developers can directly perform many simple operations using Open xml sdk, but there are still many gaps, such as converting Word documents into other formats, for example, PDF, image, or HTML. This is why Spire. Doc is used to save developers.

Document conversion:

I will introduce the various scenarios that Spire. Doc can be used in the rest of the article. All the examples shown in this article can be found in the Spire. Doc DEMO. You can easily download and use them. My example is a simple console program. Of course, it also supports other platforms, such as web projects or Silverlight projects.

In their own words, Spire. Doc claims :"Spire. Doc for. NET can convert word files into the most common and popular formats."

To start using Spire. Doc, you first need to add Spire. Doc, Spire. License and Spire. Pdf to your project. These two components are packaged in Spire. Doc.

 

You need a valid Spire. Doc authorization file to use this class library, otherwise it will display the "evaluation version" warning in the document. It is very easy to set authorization. As long as you set the location of the authorization, the class library will automatically complete verification and other work. There are other methods to load the authorization file, such as dynamically specifying or adding it to the resource file. For more information, see here ..

FileInfo licenseFile = new FileInfo (@ "C: \ ManasBhardwaj \ license. lic ");

Spire. License. LicenseProvider. SetLicenseFile (licenseFile );

To verify basic functions, I used a Word document containing simple text, an image, and a table. As shown in, you can also find it in the Demo.

 

The core of the class library is the Document class. Therefore, we start from creating a Document object and then load the Document information. Simply put, you only need three lines of code to convert a Word document that is quite complex and contains different elements into a completely different document, such as an HTML document.

// Create word document

Document document = new Document ();

Document. LoadFromFile (@ "This is a Test Document.docx ");

Insert Image

// Insert image

DocPicture picturew.document.sections%02.16.paragraphs%02.16.appendpicture(image.fromfile(image.png );

Insert table

 

AddTable (document. Sections [0]);

Privatevoid addTable (Section section)

{

// Create a table with border

Spire. Doc. Table table = section. AddTable (true );

String [] header = {"Column A", "Column B", "Column C "};

String [] [] data = {

NewString [] {"Value 1", "V 1.1", "V 1.2 "},

Newstring [] {"Value 2", "V 2.1", "V 2.2 "}

};

Table. ResetCells (data. Length + 1, header. Length );

TableRow Frow = table. Rows [0];

Frow. IsHeader = true;

Frow. Height = 15;// Unit: point, 1 point = 0.3528mm 

For (int I = 0; I

{

Frow. Cells [I]. CellFormat. BackColor = Color. FromArgb (142, 91,155,213 );

Frow. Cells [I]. CellFormat. verticalignment = verticalignment. Middle;

Paragraph p = Frow. Cells [I]. AddParagraph ();

P. Format. HorizontalAlignment = Spire. Doc. Documents. HorizontalAlignment. Left;

TextRange txtRange = p. AppendText (header [I]);

TxtRange. CharacterFormat. Bold = true;

}

For (int r = 0; r <data. Length; r ++)

{

TableRow dataRow = table. Rows [r + 1];

DataRow. Height = 15;

DataRow. HeightType = TableRowHeightType. Exactly;

DataRow. RowFormat. BackColor = Color. Empty;

For (int c = 0; c <data [r]. Length; c ++)

{

If (r % 2 = 0)

DataRow. Cells1.CellFormat. BackColor = Color. FromArgb (137,222,230,246 );

DataRow. Cells1.CellFormat. verticalignment = verticalignment. Middle;

DataRow. Cells1.AddParagraph (). AppendText (data [r] 1 );

}

}

}

Convert to HTML document

// Convert the file into an HTML document

Document. SaveToFile ("Test.html", FileFormat. Html );

So now we have a document for conversion. Let's see what happened behind this. You can see that a new HTML document is generated, and there are additional files and folders. These files and folders are used to retain additional information in the original Word document. In this example, the folder contains the test image we added in the original document, and the style sheet contains the style definition of the table. Therefore, this conversion not only converts data, but also tries its best to ensure the integrity of additional information such as styles.

 

The style sheet looks like this:

 

The difference between a simple parameter allows you to convert a document to another format, such as the following PDF. What I'm most satisfied with is that all these are based on a framework. We can convert documents into different formats without additional style and configuration definitions. Note that all these operations are completed in the memory, so you do not need additional system permissions. I remember that when we wanted to convert a project, we had to transfer data from one component to another to export a PDF file, and we could not maintain the same layout in different document formats.

ConvertPDF

// Convert the file to PDF

Document. SaveToFile ("Test. Pdf", FileFormat. PDF );

With very little code, you can get the following PDF document. I am using a trial version, so I have an authorization warning. When you use the official version, there is no warning.

Convert to XML

The FileFormat class displays that it supports up to 24 file formats. I like XML most. It has good scalability, And you can process a lot of data in the document. For example, you can use a Word document to create an XML document from the original file.

// Convert the file to Xml

Document. SaveToFile ("Test. Xml", FileFormat. Xml );

 Convert to image

So what about converting to an image? Spire. Doc supports direct conversion of documents into images, and all. net Framework supports the image format.

// Save as image

Image image = document. SaveToImages (0, ImageType. Metafile );

Image. Save ("Test. tif", System. Drawing. Imaging. ImageFormat. Tiff );

Summary

Spire. Doc is a powerful and easy-to-use product for converting Word documents to any other format file. If you also use the report function, it would be better. Like other third-party vendors, there are usually many ways to do the same thing, but you need to weigh the cost of purchasing or reusing the product and the convenience it brings.

In terms of license and pricing, it is not very expensive compared with other similar products on the market. Therefore, in my opinion, this is a very cost-effective expense.

Related Article

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.