Create a Word Document and Convert it to PDF using Free Library


As a developer, I had a requirement that a Word document should be created for my last project. This article presents how to create a rich content Word file and convert it to PDF using Free Spire.Doc in C#.

Introduction:

I had a requirement that a Word document should be created for my last project. The biggest challenge was that Microsoft Word might not be installed on the machine. Basically, there were a few Word components to perform a wide range of document processing tasks without Microsoft Word installed. I chose Free Spire.Doc as it is free and easy to use. And considering the expertise I had with this component, I decided to go with it. Below you can see that how I created and manipulated a Word document. I also used some data from Adventure Works Sample Database which is produced by Microsoft to fill a table.

Purpose:

1.Create a Word document.
2.Insert header/footer.
3.Insert text, image and hyperlink.
4.Insert table and apply built-in style.
5.Save Word document.
6.Save as PDF document.

Code Snippet:

Firstly, I created a Document with Section.


//Create word document
Document doc = new Document();
//Create a new section
Section section = doc.AddSection();

Then I inserted header/footer with some texts inside (image also supported).

//Insert header
HeaderFooter header = section.HeadersFooters.Header;
//Insert text to header
Paragraph headerParagraph = header.AddParagraph();
TextRange headerText = headerParagraph.AppendText("Demo for Free Spire.Doc");
headerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right;
//Border
headerParagraph.Format.Borders.Bottom.BorderType = BorderStyle.Single;
//Insert footer
HeaderFooter footer = section.HeadersFooters.Footer;
//Insert page number to footer
Paragraph footerParagraph = footer.AddParagraph();
footerParagraph.AppendField("page number", FieldType.FieldPage);
footerParagraph.AppendText(" of ");
footerParagraph.AppendField("number of pages", FieldType.FieldNumPages);
footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right;
//border
footerParagraph.Format.Borders.Top.BorderType = BorderStyle.Single;

Then the body was added which had many paragraphs and images. I also set bullet style in some paragraphs.

//Title
Paragraph p = section.AddParagraph();
p.AppendText(contents[0]);
p.ApplyStyle(BuiltinStyle.Title);
//Add image
p = section.AddParagraph();
DocPicture picture = p.AppendPicture(Image.FromFile("Database.jpg"));
p.Format.HorizontalAlignment = HorizontalAlignment.Center;
picture.Width = 100;
picture.Height = 100;
//Subtitle 1
p = section.AddParagraph();
p.AppendText(contents[1]);
p.ApplyStyle(BuiltinStyle.ToaHeading);
//Some contents with bullet
for (int i = 2; i < 6; i++)
{
p = section.AddParagraph();
p.AppendText(contents[i]);
//Set bullet style
p.ListFormat.ApplyBulletStyle();
p.ListFormat.CurrentListLevel.NumberPosition = -10;
}
//Web hyperlink
p = section.AddParagraph();
p.AppendHyperlink(hyperlink, contents[7], HyperlinkType.WebLink);

Free Spire.Doc also supports add a table to document. So I created a table and inserted some data from AdventureWord Database to display, the result show is excellent.

//Add table with border
Table table = section.AddTable(true);
//Set row number and col number
table.ResetCells(13, 5);
//Header row
TableRow headerRow = table.Rows[0];
headerRow.IsHeader = true;
for (int i = 0; i < header.Length; i++)
{
headerRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
Paragraph p = headerRow.Cells[i].AddParagraph();
p.Format.HorizontalAlignment = HorizontalAlignment.Center;
p.AppendText(header[i]);
}
//Fill data row
TableRow dataRow = table.Rows[index++];
for (int i = 0; i < dr.FieldCount; i++)
{
dataRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
dataRow.Cells[i].AddParagraph().AppendText(dr.GetString(i));
}
//Apply table style
table.ApplyStyle(DefaultTableStyle.ColorfulList);
//Save to Word document
doc.SaveToFile("Sample.docx", FileFormat.Docx);

Output:

page 1

You can also save the document as a PDF document via little code changed.

//Save to PDF document
doc.SaveToFile("Sample.pdf", FileFormat.PDF);

Screenshot of PDF:

page 2


Comments

Author: Prasad kulkarni03 Nov 2014 Member Level: Gold   Points : 0

Really nice article Tony sir, it will boost up word automation.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: