Creating Word Document Using Spire Doc Library

Introduction

This article demonstrates how to create a Word document using Spire Doc library. Spire Doc provides an easy library that helps to create a Word document without puttingin much effort. This library creates a document in the client machine; even any third party Application cannot be installed in the same machine.

This demonstration has been created using .NET and Spire Doc library and this library provides more features, but in this demonstration, we created a Word document and a header, footer, paragraph and an image has been inserted into the document because nowadays; these are the requirements. This demonstrator explains every step to create header, footer, paragraph and format the text.

Note

This
Spire Doc library is available in Manage NuGet Packages; download and install the library in your project.

To create and implement the features mentioned above in the document, you need the following main classes and what Spire Doc Library provides are mentioned below:

  1. Document 
  2. Section
  3. HeaderFooter
  4. Paragraph
  5. DocPicture 

The Document class comes under Spire.doc namespace. It helps to create an object of document, the default constructor of this class does not take any parameter and one of its constructors takes the document path; if you provide the path of a Word document, then it will open that document.

The Section class helps to create a section in the document. HeaderFooter class helps to insert header and footer into the document section(Word document). The Paragraph class, which comes under Spire.documents namespace, helps to add the paragraph into the document and DocPicture class inserts an image into the document and Spire.Doc.Fields.

We discussed a little about the mains classes, which provides an idea to create a document. Lets discuss the code that helps to create the document.

Section 1

Creating, Adding page and saving Document

  1. Spire.Doc.Document doc = new Spire.Doc.Document();  
  2. Spire.Doc.Section section = doc.AddSection();  
  3. doc.SaveToFile("D://MyDoc.docx", Spire.Doc.FileFormat.Docx);  
The First line creates an object of the Word document; using document class of Spire.Doc Namespace. This class has many constructors. This one is a default constructor, which creates an object of the new document. If you pass an existing document's full name as a parameter of one of constructor of this class, then this class will create an object of the provided document.
 
The second line of the code given above addsa new page into the document object, that means adding a new page into the document. 

Section 2  

Adding Header and Formatting  Header Text

  1. Spire.Doc.HeaderFooter header = section.HeadersFooters.Header;
  2. Spire.Doc.Documents.Paragraph headerParagraph = header.AddParagraph();  
  3. Spire.Doc.Fields.TextRange headerText = headerParagraph.AppendText("Spire.Doc for .NET");  
  4. headerText.CharacterFormat.FontName = "Algerian"; headerText.CharacterFormat.FontSize = 15; headerText.CharacterFormat.TextColor = System.Drawing.Color.Navy;  
The First line of the second section creates an object of Header class, which comes under Spire.Doc namespace and adds header into the document page, as well. The second line of the code adds a paragraph into the header, which helps to display header test in a formatted way, by creating an object of Paragraph class, which comes under Spire.Doc.Documents Namespace.The third line and fourth line of the code inserts text into the page header and formats the text.
 
Section 3

Adding Paragraph into Page
  1.  Spire.Doc.Documents.Paragraph paragraph = section.AddParagraph();  
  2.  paragraph.AppendText("Hello! "  
  3.             + "I was created by Spire.Doc for WPF, it's a professional .NET Word component "  
  4.             + "which enables developers to perform a large range of tasks on Word document (such as create, open, write, edit, save and convert "  
  5.             + "Word document) without installing Microsoft Office and any other third-party tools on system.");  
  6. paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Justify;  
  7. paragraph.Format.AfterSpacing = 15;  
  8. paragraph.Format.BeforeSpacing = 20;  
  9.   
  10. Spire.Doc.Documents.Paragraph paragraph1 = section.AddParagraph();  
  11. paragraph1.AppendText("Hello! "  
  12.            + "I was created by Spire.Doc for WPF, it's a professional .NET Word component "  
  13.            + "which enables developers to perform a large range of tasks on Word document (such as create, open, write, edit, save and convert "  
  14.            + "Word document) without installing Microsoft Office and any other third-party tools on system.");  
  15. paragraph1.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Justify;  
  16. paragraph1.Format.AfterSpacing = 15;  
This section adds two paragraphs into the page, using AddParagraph method, which returns Paragraph Class.The AppendText function of Paragraph class helps to add text into the paragraph.
 
Section 4

Inserting Image into Page
  1. Spire.Doc.Documents.Paragraph paragraph2 = section.AddParagraph();  
  2.              
  3. Spire.Doc.Fields.DocPicture image = paragraph2.AppendPicture(System.Drawing.Image.FromFile(@"D:\Photos\100APPLE\IMG_0230.jpg"));  
  4. image.VerticalAlignment = Spire.Doc.ShapeVerticalAlignment.Center;  
  5. image.HorizontalAlignment = Spire.Doc.ShapeHorizontalAlignment.Center; image.Width = 500;   
  6. image.Height = 500;  
 The code shown above of the fourth section inserts an image into the page. The first line of the code adds a paragraph into the page and the 03,04,05,06 lines of the code inserts an image into the paragraph and sets an alignment, height and weight of the image.

Section 5

Adding Header and Formatting Header Text

  1. Spire.Doc.HeaderFooter footer = section.HeadersFooters.Footer;  
  2. Spire.Doc.Documents.Paragraph footerParagraph = footer.AddParagraph();  
  3. footerParagraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;  
  4. footerParagraph.Format.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.ThickThinMediumGap; footerParagraph.Format.Borders.Bottom.Space = 0.05f; footerParagraph.Format.Borders.Bottom.Color = System.Drawing.Color.Navy;  
  5. Spire.Doc.Fields.TextRange footerText = footerParagraph.AppendText("This document is created by Kailash");   
  6. footerText.CharacterFormat.FontName = "Cambria";  
  7. footerText.CharacterFormat.FontSize = 15; footerText.CharacterFormat.TextColor = System.Drawing.Color.Gray;  
The code shown above inserts footer into the page and formats the text of the footer and the paragraph. It is the same as inserting a header into the page.


Summary

In the above discussion, we have seen how to create a Word document using Spire.Doc library. I hope you now understand how to create a document using the library.