Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created December 8, 2023 06:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/3934e9e91f4a42bebf2b946ff3a13baf to your computer and use it in GitHub Desktop.
Save aspose-com-gists/3934e9e91f4a42bebf2b946ff3a13baf to your computer and use it in GitHub Desktop.
Create Word Documents Programmatically
// Initialize a Document
System::SharedPtr<Document> doc = System::MakeObject<Document>();
// Use a document builder to add content to the document
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
// Add text
builder->Writeln(u"Hello World!");
// Save the document to disk
doc->Save(u"document.docx");
// Create a new document
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Specify font formatting
Font font = builder.Font;
font.Size = 32;
font.Bold = true;
font.Color = System.Drawing.Color.Black;
font.Name = "Arial";
font.Underline = Underline.Single;
// Insert text
builder.Writeln("This is the first page.");
builder.Writeln();
// Change formatting for next elements.
font.Underline = Underline.None;
font.Size = 10;
font.Color = System.Drawing.Color.Blue;
builder.Writeln("This following is a table");
// Insert a table
Table table = builder.StartTable();
// Insert a cell
builder.InsertCell();
// Use fixed column widths.
table.AutoFit(AutoFitBehavior.AutoFitToContents);
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
builder.Write("This is row 1 cell 1");
// Insert a cell
builder.InsertCell();
builder.Write("This is row 1 cell 2");
builder.EndRow();
builder.InsertCell();
builder.Write("This is row 2 cell 1");
builder.InsertCell();
builder.Write("This is row 2 cell 2");
builder.EndRow();
builder.EndTable();
builder.Writeln();
// Insert image
builder.InsertImage("image.png");
// Insert page break
builder.InsertBreak(BreakType.PageBreak);
// all the elements after page break will be inserted to next page.
// Save the document
doc.Save("Document.docx");
// Create a Document object
Document doc = new Document();
// Create a DocumentBuilder object
DocumentBuilder builder = new DocumentBuilder(doc);
// Specify font formatting
Font font = builder.getFont();
font.setSize(18);
font.setBold(true);
font.setColor(Color.BLACK);
font.setName("Arial");
builder.write("How to Create a Rich Word Document?");
builder.insertBreak(BreakType.LINE_BREAK);
// Start the paragraph
font.setSize(12);
font.setBold(false);
ParagraphFormat paragraphFormat = builder.getParagraphFormat();
paragraphFormat.setFirstLineIndent(12);
paragraphFormat.setKeepTogether(true);
builder.write("This article shows how to create a Word document containing text, images and lists.");
// Save the document
doc.save("Rich Word Document.docx");
import aspose.words as aw
# create document object
doc = aw.Document()
# create a document builder object
builder = aw.DocumentBuilder(doc)
# add text to the document
builder.write("Hello world!")
# save document
doc.save("out.docx")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment