Skip to content

Instantly share code, notes, and snippets.

@conholdate-gists
Last active December 1, 2021 12:11
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 conholdate-gists/bf147783599fa06ba512c0d6a461df0c to your computer and use it in GitHub Desktop.
Save conholdate-gists/bf147783599fa06ba512c0d6a461df0c to your computer and use it in GitHub Desktop.
Automate Word to Create, Edit, or Convert Word Documents using Java
// Load document
Document doc = new Document("C:\\Files\\Words\\document.docx");
// Provide PDFSaveOption compliance to PDF17
PdfSaveOptions options = new PdfSaveOptions();
options.setCompliance(PdfCompliance.PDF_17);
// Convert Word to PDF
doc.save("C:\\Files\\Words\\output.pdf", options);
// Open the document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Set font for next elements
Font font = builder.getFont();
font.setSize(25);
font.setBold(true);
font.setColor(Color.BLACK);
font.setName("Arial");
// Insert text
builder.writeln("Welcome!");
// Set font for next elements
font.setSize(12);
font.setBold(false);
// Insert paragraph
builder.writeln("Aspose.Words for Java is a class library that enables your applications to perform a great range of document processing tasks.\r\n"
+ "\r\n"
+ "Aspose.Words supports most of the popular document formats such as DOC, DOCX, RTF, HTML, Markdown, PDF, XPS, EPUB, and others.\r\n"
+ "\r\n"
+ "With Aspose.Words for Java, you can generate, modify, convert, render, and print documents without third-party applications or Office Automation.");
builder.writeln();
font.setBold(true);
builder.writeln("This is a sample table");
font.setBold(false);
// Insert a table
Table table = builder.startTable();
builder.insertCell();
table.autoFit(AutoFitBehavior.AUTO_FIT_TO_CONTENTS);
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
builder.write("This is row 1 cell 1");
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 an image
builder.insertImage("C:\\Files\\Words\\words_java.jpg");
// Insert page break
builder.insertBreak(BreakType.PAGE_BREAK);
// all the elements after page break will be inserted to next page.
// Save the document
doc.save("C:\\Files\\Words\\document.docx");
// Load document
Document doc = new Document("C:\\Files\\Words\\document.docx");
// Access the paragraph
Run paragraph = doc.getSections().get(0).getBody().getFirstParagraph().getRuns().get(0);
paragraph.setText("This is updated text");
// Save the document
doc.save("C:\\Files\\Words\\Document_updated.docx");
// Load document
Document doc = new Document("C:\\Files\\Words\\document.docx");
// Update using find and replace
// Specify the search string and replace string using the Replace method.
doc.getRange().replace("Aspose.Words", "Hello", new FindReplaceOptions());
// Save the document
doc.save("C:\\Files\\Words\\Document_updated.docx");
// Load the document from disk.
Document doc = new Document("C:\\Files\\Words\\document.docx");
// Save as plain text
doc.save("C:\\Files\\Words\\output.txt");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment