Create DOCM Documents via C#
Native and high performance Microsoft Word DOCM files creation using server side .NET APIs.
Generating MS Word DOCM file dynamically within running application is easy. In order to create DOCM documents from scratch without requiring MS Office, we’ll use
API that offers different features for document creation, manipulation and conversion using .NET platform.
How to Create DOCM via C#
It is easy for the developers to creat, load, modify and convert DOCM files within running Word Automation applications for data processing in just a few lines of code.
- Include the namespace in your class file
- Create a Document class object.
- Create DocumentBuilder class object and initialize it with the Document object.
- Update elements using the DocumentBuilder object.
- Save the file using Save(.) method.
System Requirements
Just make sure that system have Microsoft Windows or a compatible OS with .NET Framework, .NET Core, Windows Azure, Mono or Xamarin Platforms as well as development environment like Microsoft Visual Studio.
- Install from command line as
nuget install Aspose.Words
or via Package Manager Console of Visual Studio withInstall-Package Aspose.Words
. - Alternatively, get the offline MSI installer or all DLLs in a ZIP file from downloads
Following source code shows how to create a Word DOCM file using C#.
Document docm = new Document();
DocumentBuilder bldr = new DocumentBuilder(docm);
// Define relevant font formatting
Font fnt = bldr.Font;
fnt.Size = 32;
fnt.Bold = true;
fnt.Color = System.Drawing.Color.Black;
fnt.Name = "Arial";
fnt.Underline = Underline.Single;
// Insert text
bldr.Writeln("This is the first page.");
bldr.Writeln();
// Change formatting for next elements.
fnt.Underline = Underline.None;
fnt.Size = 10;
fnt.Color = System.Drawing.Color.Blue;
bldr.Writeln("This following is a table");
// Insert a table
Table table = bldr.StartTable();
// Insert a cell
bldr.InsertCell();
// Use fixed column widths.
table.AutoFit(AutoFitBehavior.AutoFitToContents);
bldr.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
bldr.Write("row one cell one");
// Insert a cell
bldr.InsertCell();
bldr.Write("row one cell two");
bldr.EndRow();
bldr.InsertCell();
bldr.Write("row two cell one");
bldr.InsertCell();
bldr.Write("row two cell two");
bldr.EndRow();
bldr.EndTable();
bldr.Writeln();
// Insert image
bldr.InsertImage("image.png");
// Insert page break
bldr.InsertBreak(BreakType.PageBreak);
// Elements after page break will be inserted to next page.
// Save it to get the created one
docm.Save("Document.docm");
DOCM What is DOCM File Format
DOCM files are Microsoft Word 2007 or higher generated documents with the ability to run macros. It is similar to DOCX file format but the ability to run macros makes it different from DOCX. Like DOCX, DOCM files can be store text, images, tables, shapes, charts and other contents.The capability to run macros make it easy to save time by executing the series of commands in the form of recorded actions for automatic completion of a task. DOCM files can be opened and edited in Microsoft Word 2007 and above.
Read MoreOther Supported Document Generation
You can also create other Microsoft Words file formats including few listed below.