Before I created the OpenSource library “DocX”, there were two methods by which a .docx document could be generated programmatically. This blog post demonstrates each method in turn by generating a simple HelloWorld document.
After experiencing both methods for document creation I hope you will agree that DocX is a interesting alternative. DocX was designed with simplicity in mind. It dose not require the user to have any knowledge of how .docx documents are stored or organised internally.
Method 1: HelloWorld using Office Interop
Make sure that you have added a reference to
1) Microsoft.Office.Interop.Word (version 12.0.0.0)
- using System;
- using Microsoft.Office.Interop.Word;
- using System.Reflection;
- using System.IO;
- namespace OfficeInterop
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Get the path of the executing assembly.
- string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
- // The location to save the file.
- Object oSaveAsFile = (Object)(path + @"\Test.docx");
- // Object of Missing "Null Value".
- Object oMissing = System.Reflection.Missing.Value;
- // Object of false.
- Object oFalse = false;
- // Create object of Word and Document.
- Application oWord = new Application();
- Document oWordDoc = new Document();
- // Don't display Word.
- oWord.Visible = false;
- // Add a new document.
- oWordDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
- // Enter some text with the font Arial Black.
- oWord.Selection.Font.Name = "Arial Black";
- oWord.Selection.TypeText("Hello World");
- // Save the document.
- oWordDoc.SaveAs
- (
- ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,
- ref oMissing, ref oMissing,ref oMissing, ref oMissing, ref oMissing,
- ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
- ref oMissing, ref oMissing
- );
- // Close the file.
- oWordDoc.Close(ref oFalse, ref oMissing, ref oMissing);
- // Quit Word.exe
- oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
- }
- }
- }
Method 2: HelloWorld using the OOXML SDK
Make sure that you have added a reference to both
1) DocumentFormat.OpenXml (version 2.0.5022.0)
2) WindowsBase (version 3.0.0.0)
- using DocumentFormat.OpenXml.Packaging;
- using DocumentFormat.OpenXml.Wordprocessing;
- using DocumentFormat.OpenXml;
- namespace OOXML
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Create a package.
- WordprocessingDocument doc =
- WordprocessingDocument.Create
- (
- @"Test.docx",
- WordprocessingDocumentType.Document
- );
- // Create a main document part and add it to the package.
- MainDocumentPart mainPart = doc.AddMainDocumentPart();
- mainPart.Document = new Document();
- // Create some content.
- Text textFirstLine = new Text("Hello World");
- Run run = new Run(textFirstLine);
- Paragraph para = new Paragraph(run);
- // Apply a font.
- RunProperties runProp = new RunProperties();
- RunFonts runFont = new RunFonts();
- runFont.Ascii = "Arial Black";
- runProp.Append(runFont);
- run.PrependChild<RunProperties>(runProp);
- // Add the content into the document.
- Body body = new Body(para);
- mainPart.Document.Append(body);
- /* Save and close */
- mainPart.Document.Save();
- doc.Close();
- }
- }
- }
HelloWorld using DocX
Make sure that you have added a reference to
1) DocX (version 1.0.0.9)
2) System.Drawing (version 2.0.0.0)
- using System;
- using Novacode;
- using System.Drawing;
- namespace DocXHelloWorld
- {
- class Program
- {
- static void Main(string[] args)
- {
- using (DocX document = DocX.Create("Test.docx"))
- {
- // Add a new Paragraph to the document.
- Paragraph p = document.InsertParagraph();
- // Append some text.
- p.Append("Hello World").Font(new FontFamily("Arial Black"));
- // Save the document.
- document.Save();
- }
- }
- }
- }

I need help here!
ReplyDeleteI need to open a word file stored on the server using asp.net but using VB and not C#.
I tried to use the code for editing the file but it gives and error saying
"Novacode.docX.Private Sub New(document as Novacode.DocX, xml As System.Xml.Linq.XElement)' is not accessible in this context because ot os 'Private'.
Please help...
Mandy, you may want to try converting the c# code above to VB using an online convertor, such as http://www.developerfusion.com/tools/convert/csharp-to-vb/
ReplyDeletehow can i save word as pdf using docx?
ReplyDeleteCathal Coffey , Thank you for your blog , it is helpful
ReplyDeleteis there a way to integrate an audio file into an existing document using DocX?
ReplyDeleteI love it,Excellent article.I am decide to put this into use one of these days.Thank you for sharing this.To Your Success!
ReplyDelete_____________________________________________________________________________
Rc Helicopter Parts|Rc Helicopter|Mini Rc Helicopter
hi, I can edit in the browser a file created with docX.dll, summing up ... I'm trying to edit the file in an asp.net page. thanks ... carlitolima@hotmail.com, i am from Brasil..
ReplyDelete