Adjust Margins of All Pages Except First

Hello.

I want to adjust the margins of all pages in a docx document except the first. I know that I can adjust the margins for all pages by doing the following:

DocumentBuilder docBuilder = new DocumentBuilder(wordDocument);
docBuilder.getPageSetup().setTopMargin(72.0);

However, I don’t want to adjust the margins for the first page. How can I do that?

@mohammed.allulu Page setup in MS Word documents is applied per Section. So you should create a section for the first page and a section for the rest of the pages. For example, wee the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Setup first section.
builder.getPageSetup().setTopMargin(72);
builder.getPageSetup().setBottomMargin(72);
builder.getPageSetup().setRightMargin(72);
builder.getPageSetup().setLeftMargin(72);

// Insert some content
builder.write("This is content of the first section.");

// Insert a section break and change page setup of the next section.
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
builder.getPageSetup().setTopMargin(100);
builder.getPageSetup().setBottomMargin(100);
builder.getPageSetup().setRightMargin(100);
builder.getPageSetup().setLeftMargin(100);

// Insert some content
builder.write("This is content of the second section.");

doc.save("C:\\Temp\\out.docx");

Please see our documentation to learn how to work with sections.