The sample code below is an example of creating a simple .docx file using the library attached at the end of the page. The library can do simple stuff like
Add header
Add footer
Set page size
Insert pargraphs
Insert table
Insert images
import myDOCX.*;
public class TestDOCXWriter
{
public static void main(String[] args)
{
try
{
SimpleDOCXFile docxFile = new SimpleDOCXFile("Testing.docx");
Formatting formatting = new Formatting("Times New Roman",12);//colour set to auto, no bold, no underline, no italics
Body body = docxFile.getBody();
//set paper size as A4 protrait
body.setPaperSizeAsA4Protrait();
//set paper as US Letter
//body.setPaperSize(8.5*2.54,11*2.54);//in cm
//set the margins
/*
body.setLeftMargin(1);
body.setRightMargin(1);
body.setTopMargin(1);
body.setBottomMargin(1);
body.setHeaderMargin(0.5);
body.setFooterMargin(0.5);
*/
//add something to the header
Header header = body.getHeader();
{
Paragraph paragraph = new Paragraph();
paragraph.insertText("This is in header.",formatting);
header.addParagraph(paragraph);
}
//add a footer to the body
Footer footer = body.getFooter();
{
Paragraph paragraph = new Paragraph();
paragraph.insertText("This is in footer.",formatting);
footer.addParagraph(paragraph);
}
{
Paragraph paragraph = new Paragraph();
paragraph.insertText("This is left alignment",formatting);
paragraph.setLeftAlignment();
body.addParagraph(paragraph);
}
{
Paragraph paragraph = new Paragraph();
paragraph.insertText("This is center alignment",formatting);
paragraph.setCenterAlignment();
body.addParagraph(paragraph);
}
{
Paragraph paragraph = new Paragraph();
paragraph.insertText("This is right alignment",formatting);
paragraph.setRightAlignment();
body.addParagraph(paragraph);
}
{
Paragraph paragraph = new Paragraph();
paragraph.insertText("This is justify alignment",formatting);
paragraph.setJustify();
body.addParagraph(paragraph);
}
{
Paragraph paragraph = new Paragraph();
Formatting redFormatting = formatting.clone();
redFormatting.setColourStr("FF0000");
paragraph.insertText("This is in red",redFormatting);
body.addParagraph(paragraph);
}
{
Paragraph paragraph = new Paragraph();
paragraph.setLeftAlignment();
Formatting boldFormatting = formatting.clone();
boldFormatting.setBold(true);
Formatting italicsFormatting = formatting.clone();
italicsFormatting.setItalics(true);
Formatting singleUnderlineFormatting = formatting.clone();
singleUnderlineFormatting.setSingleUnderline();
Formatting doubleUnderlineFormatting = formatting.clone();
doubleUnderlineFormatting.setDoubleUnderline();
Formatting superscriptFormatting = formatting.clone();
superscriptFormatting.setSuperscript();
Formatting subscriptFormatting = formatting.clone();
subscriptFormatting.setSubscript();
Formatting singleStrikeFormatting = formatting.clone();
singleStrikeFormatting.setSingleStrikeThrough();
Formatting doubleStrikeFormatting = formatting.clone();
doubleStrikeFormatting.setDoubleStrikeThrough();
paragraph.insertText("bolded ",boldFormatting);
paragraph.insertText("italics ",italicsFormatting);
paragraph.insertText("single underline ",singleUnderlineFormatting);
paragraph.insertText("double ",doubleUnderlineFormatting);
paragraph.insertText("superscript ",superscriptFormatting);
paragraph.insertText("subscript ",subscriptFormatting);
paragraph.insertText("single strike ",singleStrikeFormatting);
paragraph.insertText("double strike ",doubleStrikeFormatting);
body.addParagraph(paragraph);
}
{
Paragraph paragraph = new Paragraph();
paragraph.setLeftTab(1);
paragraph.setCenterTab(8);
paragraph.setRightTab(16);
paragraph.insertTab();
paragraph.insertText("left",formatting);
paragraph.insertTab();
paragraph.insertText("center",formatting);
paragraph.insertTab();
paragraph.insertText("right",formatting);
body.addParagraph(paragraph);
}
{
Paragraph paragraph = new Paragraph();
paragraph.setLineSpacing(2);
paragraph.insertText("this is line 1 of a paragraph.",formatting);
paragraph.insertLineBreak();
paragraph.insertText("this is line 2 of a paragraph.",formatting);
paragraph.insertPageBreak();
body.addParagraph(paragraph);
}
{
Paragraph paragraph = new Paragraph();
paragraph.insertText("this is in page 2.",formatting);
body.addParagraph(paragraph);
}
{
Table table = new Table(2,3);
//set the border width
table.setBorderWidth(4);
//set the column width, starts fom 0
table.setColumnWidth(0,3);
table.setColumnWidth(1,3);
table.setColumnWidth(2,3);
table.setCenterAlignment();
for(int r=0;r<2;r++)
{
for(int c=0;c<3;c++)
{
Paragraph paragraph = new Paragraph();
paragraph.insertText("("+r+","+c+")",formatting);
paragraph.setCenterAlignment();
table.setCell(r,c,paragraph);
}
}
body.addTable(table);
}
{
docxFile.insertAnImage("a jpg image.jpg");//space will be replace with _
docxFile.insertAnImage("a png image.png");//space will be replace with _
Paragraph paragraph = new Paragraph();
paragraph.insertImage(new Image(0,5,5));
paragraph.insertImage(new Image(1,10,10));
body.addParagraph(paragraph);
}
docxFile.save();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}