We will create a windows application to demonstrate how to generate word doc as a report (Certificate) using Spire.net library.
Spire.Doc for .NET is a library for developers to create, read, write, convert and print Word document files from C#, VB.NET, ASP.NET with quality performance.
Step 1: Create Windows Form Application, go to File - New Project, Windows, then Windows Forms Application, give a name 'WinSpiredoc' and click 'Ok'.
This will create a Windows form application as in the following screenshot,
Step 2 : Create a layout by drag and drop Button control on form.
Step 3 : Change button text property and give a name 'Generate Certificate'.
Step 4: Add the reference dll of Spire.Doc.dll in your project, this is the dll using the class and methods we are going to generate word document.
Step 5: Include
Spire.Doc and
Spire.Doc.Document reference in your Form.cs file. For downloading and using Spire.Doc please
click here.
Step 6: Now add the following code on your button click event. Here we are creating document object, and for that load a document first, then we will append the text paragraph, and lastly we will save doc file and launch the MS Word file.
- private void button1_Click(object sender, EventArgs e)
- {
-
- Document document = new Document();
-
-
- document.LoadFromFile(@ "D:\Ankur\PROJECTS\My Blogs\certificate.txt");
-
-
- Paragraph paragraph = document.Sections[0].AddParagraph();
-
-
- paragraph.AppendText("This is to certify that Mr / Ms._____________________,has successfully completed his ______________________________ study with specialisation in _____________at XYZ University.");
-
-
-
- document.SaveToFile("Sample.doc", FileFormat.Doc);
-
-
- WordDocViewer("Sample.doc");
- }
- private void WordDocViewer(string fileName)
- {
- try
- {
- System.Diagnostics.Process.Start(fileName);
- }
- catch { }
- }
Step 7: Now run the application and click on 'Generate Certificate' Button.
Step 8: This will open a word doc file with the paragraph you have added in code.
You can create invoice, certificates, bills, etc dynamically using this without utilizing Microsoft Word.
So, using Spire.Doc you can generate, modify, convert, render and print documents without utilizing Microsoft Word®.
But you need MS Word viewer to view the resultant document.
Hope you like this article.