Introduction
I am a .NET programmer and now I use DocX to create a Word document that contains all of my required elements, such as hyperlinks, tables, shapes, headers and footers.
But later I needed to convert this DocX into printable formats, PDF, HTML and XPS and so on.
I searched the internet and found that the writer of DocX recommends Microsoft's Office interop libraries to finish this requirements. For my develop situation, we don't want to install Microsoft Word or office to be installed on our machine. Finally I found the free Spire.Doc to finish this task, that is a standalone .NET Word API.
Application Overview
First, I use DocX to create an empty Word document and then insert a paragraph with a hyperlink, rich text and a table. Then save the Word document. Secondly, I use Spire.Doc to load the generated DocX Word document and convert it to other popular file formats, such as PDF, HTML, Image, TXT, Epub and XPS and so on.
To show how to create a Word document in DocX, I have created a sample document and here is how it looks.
First, we need to generate a Word document using DocX, which is an open source API to work with Word 2007/2010 files.
Namespace to be used:
using Novacode
DocX makes creating and manipulating documents simple. Check out the code snippet of creating a Word document with hyperlinks and tables as below:
- Console.WriteLine("\tHyperlinksImagesTables()");
-
-
- using (DocX document = DocX.Create(@"Sample.docx"))
- {
-
- Novacode.Hyperlink link = document.AddHyperlink("link", new Uri("http://www.google.com"));
-
- Novacode.Table table = document.AddTable(2, 2);
-
- table.Design = TableDesign.ColorfulList;
- table.Alignment = Alignment.center;
- table.Rows[0].Cells[0].Paragraphs[0].Append("1");
- table.Rows[0].Cells[1].Paragraphs[0].Append("2");
- table.Rows[1].Cells[0].Paragraphs[0].Append("3");
- table.Rows[1].Cells[1].Paragraphs[0].Append("4");
- Row newRow = table.InsertRow(table.Rows[1]);
-
- Paragraph title = document.InsertParagraph().Append("DocX word document").FontSize(20).Font(new FontFamily("Arial"));
- title.Alignment = Alignment.center;
-
- Paragraph p1 = document.InsertParagraph();
-
- p1.AppendLine("Here is a URL ").AppendHyperlink(link).Append(".");
- p1.AppendLine("This line contains a ").Append("word").Bold().Append(" in bold.");
- p1.AppendLine();
- p1.AppendLine("Here is a Table");
- p1.AppendLine();
-
- p1.InsertTableAfterSelf(table);
-
- document.Save();
- }
Next is to convert the Word document into a PDF. DocX doesn't have the conversion feature, so you need to use the other component: Free Spire.Doc, which is free and there is no need to install Microsoft on the machine.
Namespace to be used:
The convert function offered by Spire.Doc is very easy. You only need to use the LoadFromFile method to load the file and use SaveToFile to save the document to PDF file format. The detailed codes are as follows:
- Document doc = new Document();
- doc.LoadFromFile("Sample.docx");
- doc.SaveToFile("toPDF.PDF", FileFormat.PDF);
Check the generated PDF format as below
:
Besides converting a Word document into a PDF, the free Spire.Doc also supports conversion of Word documents into an image, HTML, TXT, XPS, XML and EPub and so on.
The full code is as follows:
- static void Main(string[] args)
- {
- Console.WriteLine("\tHyperlinksImagesTables()");
-
-
- using (DocX document = DocX.Create(@"Sample.docx"))
- {
-
- Novacode.Hyperlink link = document.AddHyperlink("link", new Uri("http://www.google.com"));
-
- Novacode.Table table = document.AddTable(2, 2);
-
- table.Design = TableDesign.ColorfulList;
- table.Alignment = Alignment.center;
- table.Rows[0].Cells[0].Paragraphs[0].Append("1");
- table.Rows[0].Cells[1].Paragraphs[0].Append("2");
- table.Rows[1].Cells[0].Paragraphs[0].Append("3");
- table.Rows[1].Cells[1].Paragraphs[0].Append("4");
- Row newRow = table.InsertRow(table.Rows[1]);
-
- Paragraph title = document.InsertParagraph().Append("DocX word document").FontSize(20).Font(new FontFamily("Arial"));
- title.Alignment = Alignment.center;
-
- Paragraph p1 = document.InsertParagraph();
-
- p1.AppendLine("Here is a URL ").AppendHyperlink(link).Append(".");
- p1.AppendLine("This line contains a ").Append("word").Bold().Append(" in bold.");
- p1.AppendLine();
- p1.AppendLine("Here is a Table");
- p1.AppendLine();
-
- p1.InsertTableAfterSelf(table);
-
- document.Save();
- Document doc = new Document();
- doc.LoadFromFile("Sample.docx");
- doc.SaveToFile("toPDF.PDF", FileFormat.PDF);
- Console.WriteLine("\tCreated: docs\\HyperlinksImagesTables.docx\n");
- }
- }