com.aspose.words
Class PageSetup

java.lang.Object
    extended by com.aspose.words.PageSetup

public class PageSetup 
extends java.lang.Object

Represents the page setup properties of a section.

PageSetup object contains all the page setup attributes of a section (left margin, bottom margin, paper size, and so on) as properties.

Example:

Shows how to insert sections using DocumentBuilder, specify page setup for a section and reset page setup to defaults.
DocumentBuilder builder = new DocumentBuilder();

// Modify the first section in the document.
builder.getPageSetup().setOrientation(Orientation.LANDSCAPE);
builder.getPageSetup().setVerticalAlignment(PageVerticalAlignment.CENTER);
builder.writeln("Section 1, landscape oriented and text vertically centered.");

// Start a new section and reset its formatting to defaults.
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
builder.getPageSetup().clearFormatting();
builder.writeln("Section 2, back to default Letter paper size, portrait orientation and top alignment.");

builder.getDocument().save(getArtifactsDir() + "PageSetup.ClearFormatting.doc");

Property Getters/Setters Summary
booleangetBidi()
voidsetBidi(boolean value)
           Specifies that this section contains bidirectional (complex scripts) text.
booleangetBorderAlwaysInFront()
voidsetBorderAlwaysInFront(boolean value)
           Specifies where the page border is positioned relative to intersecting texts and objects.
intgetBorderAppliesTo()
voidsetBorderAppliesTo(int value)
           Specifies which pages the page border is printed on. The value of the property is PageBorderAppliesTo integer constant.
intgetBorderDistanceFrom()
voidsetBorderDistanceFrom(int value)
           Gets or sets a value that indicates whether the specified page border is measured from the edge of the page or from the text it surrounds. The value of the property is PageBorderDistanceFrom integer constant.
BorderCollectiongetBorders()
           Gets a collection of the page borders.
booleangetBorderSurroundsFooter()
voidsetBorderSurroundsFooter(boolean value)
           Specifies whether the page border includes or excludes the footer.
booleangetBorderSurroundsHeader()
voidsetBorderSurroundsHeader(boolean value)
           Specifies whether the page border includes or excludes the header.
doublegetBottomMargin()
voidsetBottomMargin(double value)
           Returns or sets the distance (in points) between the bottom edge of the page and the bottom boundary of the body text.
intgetCharactersPerLine()
voidsetCharactersPerLine(int value)
           Gets or sets the number of characters per line in the document grid.
booleangetDifferentFirstPageHeaderFooter()
voidsetDifferentFirstPageHeaderFooter(boolean value)
          True if a different header or footer is used on the first page.
EndnoteOptionsgetEndnoteOptions()
           Provides options that control numbering and positioning of endnotes in this section.
intgetFirstPageTray()
voidsetFirstPageTray(int value)
           Gets or sets the paper tray (bin) to use for the first page of a section. The value is implementation (printer) specific.
doublegetFooterDistance()
voidsetFooterDistance(double value)
           Returns or sets the distance (in points) between the footer and the bottom of the page.
FootnoteOptionsgetFootnoteOptions()
           Provides options that control numbering and positioning of footnotes in this section.
doublegetGutter()
voidsetGutter(double value)
           Gets or sets the amount of extra space added to the margin for document binding.
doublegetHeaderDistance()
voidsetHeaderDistance(double value)
           Returns or sets the distance (in points) between the header and the top of the page.
intgetLayoutMode()
voidsetLayoutMode(int value)
           Gets or sets the layout mode of this section. The value of the property is SectionLayoutMode integer constant.
doublegetLeftMargin()
voidsetLeftMargin(double value)
           Returns or sets the distance (in points) between the left edge of the page and the left boundary of the body text.
intgetLineNumberCountBy()
voidsetLineNumberCountBy(int value)
           Returns or sets the numeric increment for line numbers.
doublegetLineNumberDistanceFromText()
voidsetLineNumberDistanceFromText(double value)
           Gets or sets distance between the right edge of line numbers and the left edge of the document.
intgetLineNumberRestartMode()
voidsetLineNumberRestartMode(int value)
           Gets or sets the way line numbering runs that is, whether it starts over at the beginning of a new page or section or runs continuously. The value of the property is LineNumberRestartMode integer constant.
intgetLinesPerPage()
voidsetLinesPerPage(int value)
           Gets or sets the number of lines per page in the document grid.
intgetLineStartingNumber()
voidsetLineStartingNumber(int value)
           Gets or sets the starting line number.
intgetMultiplePages()
voidsetMultiplePages(int value)
           For multiple page documents, gets or sets how a document is printed or rendered so that it can be bound as a booklet. The value of the property is MultiplePagesType integer constant.
booleangetOddAndEvenPagesHeaderFooter()
voidsetOddAndEvenPagesHeaderFooter(boolean value)
          True if the document has different headers and footers for odd-numbered and even-numbered pages.
intgetOrientation()
voidsetOrientation(int value)
           Returns or sets the orientation of the page. The value of the property is Orientation integer constant.
intgetOtherPagesTray()
voidsetOtherPagesTray(int value)
           Gets or sets the paper tray (bin) to be used for all but the first page of a section. The value is implementation (printer) specific.
doublegetPageHeight()
voidsetPageHeight(double value)
           Returns or sets the height of the page in points.
intgetPageNumberStyle()
voidsetPageNumberStyle(int value)
           Gets or sets the page number format. The value of the property is NumberStyle integer constant.
intgetPageStartingNumber()
voidsetPageStartingNumber(int value)
           Gets or sets the starting page number of the section.
doublegetPageWidth()
voidsetPageWidth(double value)
           Returns or sets the width of the page in points.
intgetPaperSize()
voidsetPaperSize(int value)
           Returns or sets the paper size. The value of the property is PaperSize integer constant.
booleangetRestartPageNumbering()
voidsetRestartPageNumbering(boolean value)
          True if page numbering restarts at the beginning of the section.
doublegetRightMargin()
voidsetRightMargin(double value)
           Returns or sets the distance (in points) between the right edge of the page and the right boundary of the body text.
booleangetRtlGutter()
voidsetRtlGutter(boolean value)
           Gets or sets whether Microsoft Word uses gutters for the section based on a right-to-left language or a left-to-right language.
intgetSectionStart()
voidsetSectionStart(int value)
           Returns or sets the type of section break for the specified object. The value of the property is SectionStart integer constant.
intgetSheetsPerBooklet()
voidsetSheetsPerBooklet(int value)
           Returns or sets the number of pages to be included in each booklet.
booleangetSuppressEndnotes()
voidsetSuppressEndnotes(boolean value)
          True if endnotes are printed at the end of the next section that doesn't suppress endnotes. Suppressed endnotes are printed before the endnotes in that section.
TextColumnCollectiongetTextColumns()
           Returns a collection that represents the set of text columns.
intgetTextOrientation()
voidsetTextOrientation(int value)
           Allows to specify TextOrientation for the whole page. Default value is TextOrientation.HORIZONTALThe value of the property is TextOrientation integer constant.
doublegetTopMargin()
voidsetTopMargin(double value)
           Returns or sets the distance (in points) between the top edge of the page and the top boundary of the body text.
intgetVerticalAlignment()
voidsetVerticalAlignment(int value)
           Returns or sets the vertical alignment of text on each page in a document or section. The value of the property is PageVerticalAlignment integer constant.
 
Method Summary
voidclearFormatting()
           Resets page setup to default paper size, margins and orientation.
 

Property Getters/Setters Detail

getBidi/setBidi

public boolean getBidi() / public void setBidi(boolean value)
Specifies that this section contains bidirectional (complex scripts) text.

When true, the columns in this section are laid out from right to left.

Example:

Shows how to change the order of columns.
Document doc = new Document();

PageSetup pageSetup = doc.getSections().get(0).getPageSetup();
pageSetup.getTextColumns().setCount(3);

DocumentBuilder builder = new DocumentBuilder(doc);

builder.write("Column 1.");
builder.insertBreak(BreakType.COLUMN_BREAK);
builder.write("Column 2.");
builder.insertBreak(BreakType.COLUMN_BREAK);
builder.write("Column 3.");

// Reverse the order of the columns
pageSetup.setBidi(true);

doc.save(getArtifactsDir() + "PageSetup.Bidi.docx");

getBorderAlwaysInFront/setBorderAlwaysInFront

public boolean getBorderAlwaysInFront() / public void setBorderAlwaysInFront(boolean value)
Specifies where the page border is positioned relative to intersecting texts and objects.

Example:

Creates a page border that looks like a wide blue band at the top of the first page only.
Document doc = new Document();

PageSetup ps = doc.getSections().get(0).getPageSetup();
ps.setBorderAlwaysInFront(false);
ps.setBorderDistanceFrom(PageBorderDistanceFrom.PAGE_EDGE);
ps.setBorderAppliesTo(PageBorderAppliesTo.FIRST_PAGE);

Border border = ps.getBorders().getByBorderType(BorderType.TOP);
border.setLineStyle(LineStyle.SINGLE);
border.setLineWidth(30);
border.setColor(Color.BLUE);
border.setDistanceFromText(0);

doc.save(getArtifactsDir() + "PageSetup.PageBorderTop.doc");

getBorderAppliesTo/setBorderAppliesTo

public int getBorderAppliesTo() / public void setBorderAppliesTo(int value)
Specifies which pages the page border is printed on. The value of the property is PageBorderAppliesTo integer constant.

Example:

Creates a page border that looks like a wide blue band at the top of the first page only.
Document doc = new Document();

PageSetup ps = doc.getSections().get(0).getPageSetup();
ps.setBorderAlwaysInFront(false);
ps.setBorderDistanceFrom(PageBorderDistanceFrom.PAGE_EDGE);
ps.setBorderAppliesTo(PageBorderAppliesTo.FIRST_PAGE);

Border border = ps.getBorders().getByBorderType(BorderType.TOP);
border.setLineStyle(LineStyle.SINGLE);
border.setLineWidth(30);
border.setColor(Color.BLUE);
border.setDistanceFromText(0);

doc.save(getArtifactsDir() + "PageSetup.PageBorderTop.doc");

getBorderDistanceFrom/setBorderDistanceFrom

public int getBorderDistanceFrom() / public void setBorderDistanceFrom(int value)
Gets or sets a value that indicates whether the specified page border is measured from the edge of the page or from the text it surrounds. The value of the property is PageBorderDistanceFrom integer constant.

Example:

Creates a page border that looks like a wide blue band at the top of the first page only.
Document doc = new Document();

PageSetup ps = doc.getSections().get(0).getPageSetup();
ps.setBorderAlwaysInFront(false);
ps.setBorderDistanceFrom(PageBorderDistanceFrom.PAGE_EDGE);
ps.setBorderAppliesTo(PageBorderAppliesTo.FIRST_PAGE);

Border border = ps.getBorders().getByBorderType(BorderType.TOP);
border.setLineStyle(LineStyle.SINGLE);
border.setLineWidth(30);
border.setColor(Color.BLUE);
border.setDistanceFromText(0);

doc.save(getArtifactsDir() + "PageSetup.PageBorderTop.doc");

getBorders

public BorderCollection getBorders()
Gets a collection of the page borders.

Example:

Creates a fancy looking green wavy page border with a shadow.
Document doc = new Document();
PageSetup ps = doc.getSections().get(0).getPageSetup();

ps.getBorders().setLineStyle(LineStyle.DOUBLE_WAVE);
ps.getBorders().setLineWidth(2);
ps.getBorders().setColor(Color.GREEN);
ps.getBorders().setDistanceFromText(24);
ps.getBorders().setShadow(true);

doc.save(getArtifactsDir() + "PageSetup.PageBorders.doc");

getBorderSurroundsFooter/setBorderSurroundsFooter

public boolean getBorderSurroundsFooter() / public void setBorderSurroundsFooter(boolean value)
Specifies whether the page border includes or excludes the footer. Note, changing this property affects all sections in the document.

Example:

Shows how to apply a border to the page and header/footer.
Document doc = new Document();

// Insert header and footer text
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.write("Header");
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.write("Footer");
builder.moveToDocumentEnd();

// Insert a page border and set the color and line style
PageSetup pageSetup = doc.getSections().get(0).getPageSetup();
pageSetup.getBorders().setLineStyle(LineStyle.DOUBLE);
pageSetup.getBorders().setColor(Color.BLUE);

// By default, page borders don't surround headers and footers
// We can change that by setting these flags
pageSetup.setBorderSurroundsFooter(true);
pageSetup.setBorderSurroundsHeader(true);

doc.save(getArtifactsDir() + "PageSetup.BorderSurrounds.docx");

getBorderSurroundsHeader/setBorderSurroundsHeader

public boolean getBorderSurroundsHeader() / public void setBorderSurroundsHeader(boolean value)
Specifies whether the page border includes or excludes the header. Note, changing this property affects all sections in the document.

Example:

Shows how to apply a border to the page and header/footer.
Document doc = new Document();

// Insert header and footer text
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.write("Header");
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.write("Footer");
builder.moveToDocumentEnd();

// Insert a page border and set the color and line style
PageSetup pageSetup = doc.getSections().get(0).getPageSetup();
pageSetup.getBorders().setLineStyle(LineStyle.DOUBLE);
pageSetup.getBorders().setColor(Color.BLUE);

// By default, page borders don't surround headers and footers
// We can change that by setting these flags
pageSetup.setBorderSurroundsFooter(true);
pageSetup.setBorderSurroundsHeader(true);

doc.save(getArtifactsDir() + "PageSetup.BorderSurrounds.docx");

getBottomMargin/setBottomMargin

public double getBottomMargin() / public void setBottomMargin(double value)
Returns or sets the distance (in points) between the bottom edge of the page and the bottom boundary of the body text.

Example:

Specifies paper size, orientation, margins and other settings for a section.
DocumentBuilder builder = new DocumentBuilder();

PageSetup ps = builder.getPageSetup();
ps.setPaperSize(PaperSize.LEGAL);
ps.setOrientation(Orientation.LANDSCAPE);
ps.setTopMargin(ConvertUtil.inchToPoint(1.0));
ps.setBottomMargin(ConvertUtil.inchToPoint(1.0));
ps.setLeftMargin(ConvertUtil.inchToPoint(1.5));
ps.setRightMargin(ConvertUtil.inchToPoint(1.5));
ps.setHeaderDistance(ConvertUtil.inchToPoint(0.2));
ps.setFooterDistance(ConvertUtil.inchToPoint(0.2));

builder.writeln("Hello world.");

builder.getDocument().save(getArtifactsDir() + "PageSetup.PageMargins.doc");

getCharactersPerLine/setCharactersPerLine

public int getCharactersPerLine() / public void setCharactersPerLine(int value)
Gets or sets the number of characters per line in the document grid.

Minimum value of the property is 1. Maximum value depends on page width and font size of the Normal style. Minimum character pitch is 90 percent of the font size. For example, maximum number of characters per line of a Letter page with one-inch margins is 43.

By default, the property has a value, on which character pitch equals to font size of the Normal style.

Example:

Creates headers and footers different for first, even and odd pages using DocumentBuilder.
DocumentBuilder builder = new DocumentBuilder();

PageSetup ps = builder.getPageSetup();
ps.setDifferentFirstPageHeaderFooter(true);
ps.setOddAndEvenPagesHeaderFooter(true);
ps.setLayoutMode(SectionLayoutMode.LINE_GRID);
ps.setCharactersPerLine(1);
ps.setLinesPerPage(1);

builder.moveToHeaderFooter(HeaderFooterType.HEADER_FIRST);
builder.writeln("First page header.");

builder.moveToHeaderFooter(HeaderFooterType.HEADER_EVEN);
builder.writeln("Even pages header.");

builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.writeln("Odd pages header.");

// Move back to the main story of the first section.
builder.moveToSection(0);
builder.writeln("Text page 1.");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.writeln("Text page 2.");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.writeln("Text page 3.");

builder.getDocument().save(getArtifactsDir() + "PageSetup.DifferentHeaders.doc");

getDifferentFirstPageHeaderFooter/setDifferentFirstPageHeaderFooter

public boolean getDifferentFirstPageHeaderFooter() / public void setDifferentFirstPageHeaderFooter(boolean value)
True if a different header or footer is used on the first page.

Example:

Creates headers and footers in a document using DocumentBuilder.
// Create a blank document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Specify that we want headers and footers different for first, even and odd pages.
builder.getPageSetup().setDifferentFirstPageHeaderFooter(true);
builder.getPageSetup().setOddAndEvenPagesHeaderFooter(true);

// Create the headers.
builder.moveToHeaderFooter(HeaderFooterType.HEADER_FIRST);
builder.write("Header First");
builder.moveToHeaderFooter(HeaderFooterType.HEADER_EVEN);
builder.write("Header Even");
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.write("Header Odd");

// Create three pages in the document.
builder.moveToSection(0);
builder.writeln("Page1");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.writeln("Page2");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.writeln("Page3");

doc.save(getArtifactsDir() + "DocumentBuilder.HeadersAndFooters.doc");

Example:

Creates headers and footers different for first, even and odd pages using DocumentBuilder.
DocumentBuilder builder = new DocumentBuilder();

PageSetup ps = builder.getPageSetup();
ps.setDifferentFirstPageHeaderFooter(true);
ps.setOddAndEvenPagesHeaderFooter(true);
ps.setLayoutMode(SectionLayoutMode.LINE_GRID);
ps.setCharactersPerLine(1);
ps.setLinesPerPage(1);

builder.moveToHeaderFooter(HeaderFooterType.HEADER_FIRST);
builder.writeln("First page header.");

builder.moveToHeaderFooter(HeaderFooterType.HEADER_EVEN);
builder.writeln("Even pages header.");

builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.writeln("Odd pages header.");

// Move back to the main story of the first section.
builder.moveToSection(0);
builder.writeln("Text page 1.");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.writeln("Text page 2.");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.writeln("Text page 3.");

builder.getDocument().save(getArtifactsDir() + "PageSetup.DifferentHeaders.doc");

getEndnoteOptions

public EndnoteOptions getEndnoteOptions()
Provides options that control numbering and positioning of endnotes in this section.

Example:

Shows how to set options for endnotes in current section
Document doc = new Document();

PageSetup pageSetup = doc.getSections().get(0).getPageSetup();

pageSetup.getEndnoteOptions().setPosition(EndnotePosition.END_OF_SECTION);
pageSetup.getEndnoteOptions().setNumberStyle(NumberStyle.BULLET);
pageSetup.getEndnoteOptions().setStartNumber(1);
pageSetup.getEndnoteOptions().setRestartRule(FootnoteNumberingRule.RESTART_PAGE);

getFirstPageTray/setFirstPageTray

public int getFirstPageTray() / public void setFirstPageTray(int value)
Gets or sets the paper tray (bin) to use for the first page of a section. The value is implementation (printer) specific.

Example:

Changes all sections in a document to use the default paper tray of the selected printer.
Document doc = new Document();

// Find the printer that will be used for printing this document. In this case it is the default printer.
// You can define a specific printer by using PrintServiceLookup.lookupPrintServices. 
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
Media defaultTray = (Media) printService.getDefaultAttributeValue(Media.class);

// The paper tray value stored in documents is completely printer specific. This means
// The code below resets all page tray values to use the current printers default tray.
// You can enumerate getSupportedAttributeValues for Media type to find the other valid
// paper tray values of the selected printer.
for (Section section : doc.getSections()) {
    section.getPageSetup().setFirstPageTray(defaultTray.getValue());
    section.getPageSetup().setOtherPagesTray(defaultTray.getValue());
}

Example:

Shows how to set up printing using different printer trays for different paper sizes.
Document doc = new Document();

// Choose the default printer to be used for printing this document.
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
Media[] trays = (Media[]) printService.getSupportedAttributeValues(Media.class, null, null);

// This is the tray we will use for A4 paper size. This is the first tray in the media set.
int printerTrayForA4 = trays[0].getValue();
// This is the tray we will use Letter paper size. This is the second tray in the media set.
int printerTrayForLetter = trays[1].getValue();

// Set the tray used for each section based off the paper size used in the section.
for (Section section : doc.getSections()) {
    if (section.getPageSetup().getPaperSize() == PaperSize.LETTER) {
        section.getPageSetup().setFirstPageTray(printerTrayForLetter);
        section.getPageSetup().setOtherPagesTray(printerTrayForLetter);
    } else if (section.getPageSetup().getPaperSize() == PaperSize.A4) {
        section.getPageSetup().setFirstPageTray(printerTrayForA4);
        section.getPageSetup().setOtherPagesTray(printerTrayForA4);
    }
}

getFooterDistance/setFooterDistance

public double getFooterDistance() / public void setFooterDistance(double value)
Returns or sets the distance (in points) between the footer and the bottom of the page.

Example:

Specifies paper size, orientation, margins and other settings for a section.
DocumentBuilder builder = new DocumentBuilder();

PageSetup ps = builder.getPageSetup();
ps.setPaperSize(PaperSize.LEGAL);
ps.setOrientation(Orientation.LANDSCAPE);
ps.setTopMargin(ConvertUtil.inchToPoint(1.0));
ps.setBottomMargin(ConvertUtil.inchToPoint(1.0));
ps.setLeftMargin(ConvertUtil.inchToPoint(1.5));
ps.setRightMargin(ConvertUtil.inchToPoint(1.5));
ps.setHeaderDistance(ConvertUtil.inchToPoint(0.2));
ps.setFooterDistance(ConvertUtil.inchToPoint(0.2));

builder.writeln("Hello world.");

builder.getDocument().save(getArtifactsDir() + "PageSetup.PageMargins.doc");

getFootnoteOptions

public FootnoteOptions getFootnoteOptions()
Provides options that control numbering and positioning of footnotes in this section.

Example:

Shows how to set options for footnotes in current section
Document doc = new Document();

PageSetup pageSetup = doc.getSections().get(0).getPageSetup();

pageSetup.getFootnoteOptions().setPosition(FootnotePosition.BOTTOM_OF_PAGE);
pageSetup.getFootnoteOptions().setNumberStyle(NumberStyle.BULLET);
pageSetup.getFootnoteOptions().setStartNumber(1);
pageSetup.getFootnoteOptions().setRestartRule(FootnoteNumberingRule.RESTART_PAGE);
pageSetup.getFootnoteOptions().setColumns(0);

getGutter/setGutter

public double getGutter() / public void setGutter(double value)
Gets or sets the amount of extra space added to the margin for document binding.

Example:

Shows how to set gutter margins.
Document doc = new Document();

// Insert text spanning several pages
DocumentBuilder builder = new DocumentBuilder(doc);
for (int i = 0; i < 6; i++) {
    builder.write("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.");
    builder.insertBreak(BreakType.PAGE_BREAK);
}

// We can access the gutter margin in the section's page options,
// which is a margin which is added to the page margin at one side of the page
PageSetup pageSetup = doc.getSections().get(0).getPageSetup();
pageSetup.setGutter(100.0);

// If our text is LTR, the gutter will appear on the left side of the page
// Setting this flag will move it to the right side
pageSetup.setRtlGutter(true);

// Mirroring the margins will make the gutter alternate in position from page to page
pageSetup.setMultiplePages(MultiplePagesType.MIRROR_MARGINS);

doc.save(getArtifactsDir() + "PageSetup.Gutter.docx");

getHeaderDistance/setHeaderDistance

public double getHeaderDistance() / public void setHeaderDistance(double value)
Returns or sets the distance (in points) between the header and the top of the page.

Example:

Specifies paper size, orientation, margins and other settings for a section.
DocumentBuilder builder = new DocumentBuilder();

PageSetup ps = builder.getPageSetup();
ps.setPaperSize(PaperSize.LEGAL);
ps.setOrientation(Orientation.LANDSCAPE);
ps.setTopMargin(ConvertUtil.inchToPoint(1.0));
ps.setBottomMargin(ConvertUtil.inchToPoint(1.0));
ps.setLeftMargin(ConvertUtil.inchToPoint(1.5));
ps.setRightMargin(ConvertUtil.inchToPoint(1.5));
ps.setHeaderDistance(ConvertUtil.inchToPoint(0.2));
ps.setFooterDistance(ConvertUtil.inchToPoint(0.2));

builder.writeln("Hello world.");

builder.getDocument().save(getArtifactsDir() + "PageSetup.PageMargins.doc");

getLayoutMode/setLayoutMode

public int getLayoutMode() / public void setLayoutMode(int value)
Gets or sets the layout mode of this section. The value of the property is SectionLayoutMode integer constant.

Example:

Creates headers and footers different for first, even and odd pages using DocumentBuilder.
DocumentBuilder builder = new DocumentBuilder();

PageSetup ps = builder.getPageSetup();
ps.setDifferentFirstPageHeaderFooter(true);
ps.setOddAndEvenPagesHeaderFooter(true);
ps.setLayoutMode(SectionLayoutMode.LINE_GRID);
ps.setCharactersPerLine(1);
ps.setLinesPerPage(1);

builder.moveToHeaderFooter(HeaderFooterType.HEADER_FIRST);
builder.writeln("First page header.");

builder.moveToHeaderFooter(HeaderFooterType.HEADER_EVEN);
builder.writeln("Even pages header.");

builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.writeln("Odd pages header.");

// Move back to the main story of the first section.
builder.moveToSection(0);
builder.writeln("Text page 1.");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.writeln("Text page 2.");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.writeln("Text page 3.");

builder.getDocument().save(getArtifactsDir() + "PageSetup.DifferentHeaders.doc");

getLeftMargin/setLeftMargin

public double getLeftMargin() / public void setLeftMargin(double value)
Returns or sets the distance (in points) between the left edge of the page and the left boundary of the body text.

Example:

Specifies paper size, orientation, margins and other settings for a section.
DocumentBuilder builder = new DocumentBuilder();

PageSetup ps = builder.getPageSetup();
ps.setPaperSize(PaperSize.LEGAL);
ps.setOrientation(Orientation.LANDSCAPE);
ps.setTopMargin(ConvertUtil.inchToPoint(1.0));
ps.setBottomMargin(ConvertUtil.inchToPoint(1.0));
ps.setLeftMargin(ConvertUtil.inchToPoint(1.5));
ps.setRightMargin(ConvertUtil.inchToPoint(1.5));
ps.setHeaderDistance(ConvertUtil.inchToPoint(0.2));
ps.setFooterDistance(ConvertUtil.inchToPoint(0.2));

builder.writeln("Hello world.");

builder.getDocument().save(getArtifactsDir() + "PageSetup.PageMargins.doc");

getLineNumberCountBy/setLineNumberCountBy

public int getLineNumberCountBy() / public void setLineNumberCountBy(int value)
Returns or sets the numeric increment for line numbers.

Example:

Turns on Microsoft Word line numbering for a section.
DocumentBuilder builder = new DocumentBuilder();

PageSetup ps = builder.getPageSetup();
ps.setLineStartingNumber(1);
ps.setLineNumberCountBy(5);
ps.setLineNumberRestartMode(LineNumberRestartMode.RESTART_PAGE);
ps.setLineNumberDistanceFromText(50.0d);

// The line counter will skip any paragraph with this flag set to true
Assert.assertFalse(builder.getParagraphFormat().getSuppressLineNumbers());

for (int i = 1; i <= 20; i++) {
    builder.writeln(java.text.MessageFormat.format("Line {0}.", i));
}

builder.getDocument().save(getArtifactsDir() + "PageSetup.LineNumbers.docx");

getLineNumberDistanceFromText/setLineNumberDistanceFromText

public double getLineNumberDistanceFromText() / public void setLineNumberDistanceFromText(double value)
Gets or sets distance between the right edge of line numbers and the left edge of the document. Set this property to zero for automatic distance between the line numbers and text of the document.

Example:

Turns on Microsoft Word line numbering for a section.
DocumentBuilder builder = new DocumentBuilder();

PageSetup ps = builder.getPageSetup();
ps.setLineStartingNumber(1);
ps.setLineNumberCountBy(5);
ps.setLineNumberRestartMode(LineNumberRestartMode.RESTART_PAGE);
ps.setLineNumberDistanceFromText(50.0d);

// The line counter will skip any paragraph with this flag set to true
Assert.assertFalse(builder.getParagraphFormat().getSuppressLineNumbers());

for (int i = 1; i <= 20; i++) {
    builder.writeln(java.text.MessageFormat.format("Line {0}.", i));
}

builder.getDocument().save(getArtifactsDir() + "PageSetup.LineNumbers.docx");

getLineNumberRestartMode/setLineNumberRestartMode

public int getLineNumberRestartMode() / public void setLineNumberRestartMode(int value)
Gets or sets the way line numbering runs that is, whether it starts over at the beginning of a new page or section or runs continuously. The value of the property is LineNumberRestartMode integer constant.

Example:

Turns on Microsoft Word line numbering for a section.
DocumentBuilder builder = new DocumentBuilder();

PageSetup ps = builder.getPageSetup();
ps.setLineStartingNumber(1);
ps.setLineNumberCountBy(5);
ps.setLineNumberRestartMode(LineNumberRestartMode.RESTART_PAGE);
ps.setLineNumberDistanceFromText(50.0d);

// The line counter will skip any paragraph with this flag set to true
Assert.assertFalse(builder.getParagraphFormat().getSuppressLineNumbers());

for (int i = 1; i <= 20; i++) {
    builder.writeln(java.text.MessageFormat.format("Line {0}.", i));
}

builder.getDocument().save(getArtifactsDir() + "PageSetup.LineNumbers.docx");

getLinesPerPage/setLinesPerPage

public int getLinesPerPage() / public void setLinesPerPage(int value)
Gets or sets the number of lines per page in the document grid.

Minimum value of the property is 1. Maximum value depends on page height and font size of the Normal style. Minimum line pitch is 136 percent of the font size. For example, maximum number of lines per page of a Letter page with one-inch margins is 39.

By default, the property has a value, on which line pitch is in 1.5 times greater than font size of the Normal style.

Example:

Creates headers and footers different for first, even and odd pages using DocumentBuilder.
DocumentBuilder builder = new DocumentBuilder();

PageSetup ps = builder.getPageSetup();
ps.setDifferentFirstPageHeaderFooter(true);
ps.setOddAndEvenPagesHeaderFooter(true);
ps.setLayoutMode(SectionLayoutMode.LINE_GRID);
ps.setCharactersPerLine(1);
ps.setLinesPerPage(1);

builder.moveToHeaderFooter(HeaderFooterType.HEADER_FIRST);
builder.writeln("First page header.");

builder.moveToHeaderFooter(HeaderFooterType.HEADER_EVEN);
builder.writeln("Even pages header.");

builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.writeln("Odd pages header.");

// Move back to the main story of the first section.
builder.moveToSection(0);
builder.writeln("Text page 1.");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.writeln("Text page 2.");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.writeln("Text page 3.");

builder.getDocument().save(getArtifactsDir() + "PageSetup.DifferentHeaders.doc");

getLineStartingNumber/setLineStartingNumber

public int getLineStartingNumber() / public void setLineStartingNumber(int value)
Gets or sets the starting line number.

Example:

Turns on Microsoft Word line numbering for a section.
DocumentBuilder builder = new DocumentBuilder();

PageSetup ps = builder.getPageSetup();
ps.setLineStartingNumber(1);
ps.setLineNumberCountBy(5);
ps.setLineNumberRestartMode(LineNumberRestartMode.RESTART_PAGE);
ps.setLineNumberDistanceFromText(50.0d);

// The line counter will skip any paragraph with this flag set to true
Assert.assertFalse(builder.getParagraphFormat().getSuppressLineNumbers());

for (int i = 1; i <= 20; i++) {
    builder.writeln(java.text.MessageFormat.format("Line {0}.", i));
}

builder.getDocument().save(getArtifactsDir() + "PageSetup.LineNumbers.docx");

getMultiplePages/setMultiplePages

public int getMultiplePages() / public void setMultiplePages(int value)
For multiple page documents, gets or sets how a document is printed or rendered so that it can be bound as a booklet. The value of the property is MultiplePagesType integer constant.

Example:

Shows how to set gutter margins.
Document doc = new Document();

// Insert text spanning several pages
DocumentBuilder builder = new DocumentBuilder(doc);
for (int i = 0; i < 6; i++) {
    builder.write("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.");
    builder.insertBreak(BreakType.PAGE_BREAK);
}

// We can access the gutter margin in the section's page options,
// which is a margin which is added to the page margin at one side of the page
PageSetup pageSetup = doc.getSections().get(0).getPageSetup();
pageSetup.setGutter(100.0);

// If our text is LTR, the gutter will appear on the left side of the page
// Setting this flag will move it to the right side
pageSetup.setRtlGutter(true);

// Mirroring the margins will make the gutter alternate in position from page to page
pageSetup.setMultiplePages(MultiplePagesType.MIRROR_MARGINS);

doc.save(getArtifactsDir() + "PageSetup.Gutter.docx");

getOddAndEvenPagesHeaderFooter/setOddAndEvenPagesHeaderFooter

public boolean getOddAndEvenPagesHeaderFooter() / public void setOddAndEvenPagesHeaderFooter(boolean value)
True if the document has different headers and footers for odd-numbered and even-numbered pages. Note, changing this property affects all sections in the document.

Example:

Creates headers and footers in a document using DocumentBuilder.
// Create a blank document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Specify that we want headers and footers different for first, even and odd pages.
builder.getPageSetup().setDifferentFirstPageHeaderFooter(true);
builder.getPageSetup().setOddAndEvenPagesHeaderFooter(true);

// Create the headers.
builder.moveToHeaderFooter(HeaderFooterType.HEADER_FIRST);
builder.write("Header First");
builder.moveToHeaderFooter(HeaderFooterType.HEADER_EVEN);
builder.write("Header Even");
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.write("Header Odd");

// Create three pages in the document.
builder.moveToSection(0);
builder.writeln("Page1");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.writeln("Page2");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.writeln("Page3");

doc.save(getArtifactsDir() + "DocumentBuilder.HeadersAndFooters.doc");

Example:

Creates headers and footers different for first, even and odd pages using DocumentBuilder.
DocumentBuilder builder = new DocumentBuilder();

PageSetup ps = builder.getPageSetup();
ps.setDifferentFirstPageHeaderFooter(true);
ps.setOddAndEvenPagesHeaderFooter(true);
ps.setLayoutMode(SectionLayoutMode.LINE_GRID);
ps.setCharactersPerLine(1);
ps.setLinesPerPage(1);

builder.moveToHeaderFooter(HeaderFooterType.HEADER_FIRST);
builder.writeln("First page header.");

builder.moveToHeaderFooter(HeaderFooterType.HEADER_EVEN);
builder.writeln("Even pages header.");

builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.writeln("Odd pages header.");

// Move back to the main story of the first section.
builder.moveToSection(0);
builder.writeln("Text page 1.");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.writeln("Text page 2.");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.writeln("Text page 3.");

builder.getDocument().save(getArtifactsDir() + "PageSetup.DifferentHeaders.doc");

getOrientation/setOrientation

public int getOrientation() / public void setOrientation(int value)
Returns or sets the orientation of the page. The value of the property is Orientation integer constant.

Changing Orientation swaps PageWidth and PageHeight.

Example:

Specifies paper size, orientation, margins and other settings for a section.
DocumentBuilder builder = new DocumentBuilder();

PageSetup ps = builder.getPageSetup();
ps.setPaperSize(PaperSize.LEGAL);
ps.setOrientation(Orientation.LANDSCAPE);
ps.setTopMargin(ConvertUtil.inchToPoint(1.0));
ps.setBottomMargin(ConvertUtil.inchToPoint(1.0));
ps.setLeftMargin(ConvertUtil.inchToPoint(1.5));
ps.setRightMargin(ConvertUtil.inchToPoint(1.5));
ps.setHeaderDistance(ConvertUtil.inchToPoint(0.2));
ps.setFooterDistance(ConvertUtil.inchToPoint(0.2));

builder.writeln("Hello world.");

builder.getDocument().save(getArtifactsDir() + "PageSetup.PageMargins.doc");

Example:

Shows how to insert sections using DocumentBuilder, specify page setup for a section and reset page setup to defaults.
DocumentBuilder builder = new DocumentBuilder();

// Modify the first section in the document.
builder.getPageSetup().setOrientation(Orientation.LANDSCAPE);
builder.getPageSetup().setVerticalAlignment(PageVerticalAlignment.CENTER);
builder.writeln("Section 1, landscape oriented and text vertically centered.");

// Start a new section and reset its formatting to defaults.
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
builder.getPageSetup().clearFormatting();
builder.writeln("Section 2, back to default Letter paper size, portrait orientation and top alignment.");

builder.getDocument().save(getArtifactsDir() + "PageSetup.ClearFormatting.doc");

getOtherPagesTray/setOtherPagesTray

public int getOtherPagesTray() / public void setOtherPagesTray(int value)
Gets or sets the paper tray (bin) to be used for all but the first page of a section. The value is implementation (printer) specific.

Example:

Changes all sections in a document to use the default paper tray of the selected printer.
Document doc = new Document();

// Find the printer that will be used for printing this document. In this case it is the default printer.
// You can define a specific printer by using PrintServiceLookup.lookupPrintServices. 
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
Media defaultTray = (Media) printService.getDefaultAttributeValue(Media.class);

// The paper tray value stored in documents is completely printer specific. This means
// The code below resets all page tray values to use the current printers default tray.
// You can enumerate getSupportedAttributeValues for Media type to find the other valid
// paper tray values of the selected printer.
for (Section section : doc.getSections()) {
    section.getPageSetup().setFirstPageTray(defaultTray.getValue());
    section.getPageSetup().setOtherPagesTray(defaultTray.getValue());
}

Example:

Shows how to set up printing using different printer trays for different paper sizes.
Document doc = new Document();

// Choose the default printer to be used for printing this document.
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
Media[] trays = (Media[]) printService.getSupportedAttributeValues(Media.class, null, null);

// This is the tray we will use for A4 paper size. This is the first tray in the media set.
int printerTrayForA4 = trays[0].getValue();
// This is the tray we will use Letter paper size. This is the second tray in the media set.
int printerTrayForLetter = trays[1].getValue();

// Set the tray used for each section based off the paper size used in the section.
for (Section section : doc.getSections()) {
    if (section.getPageSetup().getPaperSize() == PaperSize.LETTER) {
        section.getPageSetup().setFirstPageTray(printerTrayForLetter);
        section.getPageSetup().setOtherPagesTray(printerTrayForLetter);
    } else if (section.getPageSetup().getPaperSize() == PaperSize.A4) {
        section.getPageSetup().setFirstPageTray(printerTrayForA4);
        section.getPageSetup().setOtherPagesTray(printerTrayForA4);
    }
}

getPageHeight/setPageHeight

public double getPageHeight() / public void setPageHeight(double value)
Returns or sets the height of the page in points.

Example:

Inserts a watermark image into a document using DocumentBuilder.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// The best place for the watermark image is in the header or footer so it is shown on every page.
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);

BufferedImage image = ImageIO.read(new File(getImageDir() + "Watermark.png"));

// Insert a floating picture.
Shape shape = builder.insertImage(image);
shape.setWrapType(WrapType.NONE);
shape.setBehindText(true);

shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);

// Calculate image left and top position so it appears in the center of the page.
shape.setLeft((builder.getPageSetup().getPageWidth() - shape.getWidth()) / 2);
shape.setTop((builder.getPageSetup().getPageHeight() - shape.getHeight()) / 2);

doc.save(getArtifactsDir() + "DocumentBuilder.InsertWatermark.doc");

getPageNumberStyle/setPageNumberStyle

public int getPageNumberStyle() / public void setPageNumberStyle(int value)
Gets or sets the page number format. The value of the property is NumberStyle integer constant.

Example:

Shows how to control page numbering per section.
// This document has two sections, but no page numbers yet.
Document doc = new Document(getMyDir() + "PageSetup.PageNumbering.doc");

// Use document builder to create a header with a page number field for the first section.
// The page number will look like "Page V".
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToSection(0);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.write("Page ");
builder.insertField("PAGE", "");

// Set first section page numbering.
Section section = doc.getSections().get(0);
section.getPageSetup().setRestartPageNumbering(true);
section.getPageSetup().setPageStartingNumber(5);
section.getPageSetup().setPageNumberStyle(NumberStyle.UPPERCASE_ROMAN);

// Create a header for the section. 
// The page number will look like " - 10 - ".
builder.moveToSection(1);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.write(" - ");
builder.insertField("PAGE", "");
builder.write(" - ");

// Set second section page numbering.
section = doc.getSections().get(1);
section.getPageSetup().setPageStartingNumber(10);
section.getPageSetup().setRestartPageNumbering(true);
section.getPageSetup().setPageNumberStyle(NumberStyle.ARABIC);

doc.save(getArtifactsDir() + "PageSetup.PageNumbering.doc");

getPageStartingNumber/setPageStartingNumber

public int getPageStartingNumber() / public void setPageStartingNumber(int value)
Gets or sets the starting page number of the section. The RestartPageNumbering property, if set to false, will override the PageStartingNumber property so that page numbering can continue from the previous section.

Example:

Shows how to control page numbering per section.
// This document has two sections, but no page numbers yet.
Document doc = new Document(getMyDir() + "PageSetup.PageNumbering.doc");

// Use document builder to create a header with a page number field for the first section.
// The page number will look like "Page V".
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToSection(0);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.write("Page ");
builder.insertField("PAGE", "");

// Set first section page numbering.
Section section = doc.getSections().get(0);
section.getPageSetup().setRestartPageNumbering(true);
section.getPageSetup().setPageStartingNumber(5);
section.getPageSetup().setPageNumberStyle(NumberStyle.UPPERCASE_ROMAN);

// Create a header for the section. 
// The page number will look like " - 10 - ".
builder.moveToSection(1);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.write(" - ");
builder.insertField("PAGE", "");
builder.write(" - ");

// Set second section page numbering.
section = doc.getSections().get(1);
section.getPageSetup().setPageStartingNumber(10);
section.getPageSetup().setRestartPageNumbering(true);
section.getPageSetup().setPageNumberStyle(NumberStyle.ARABIC);

doc.save(getArtifactsDir() + "PageSetup.PageNumbering.doc");

getPageWidth/setPageWidth

public double getPageWidth() / public void setPageWidth(double value)
Returns or sets the width of the page in points.

Example:

Inserts a watermark image into a document using DocumentBuilder.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// The best place for the watermark image is in the header or footer so it is shown on every page.
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);

BufferedImage image = ImageIO.read(new File(getImageDir() + "Watermark.png"));

// Insert a floating picture.
Shape shape = builder.insertImage(image);
shape.setWrapType(WrapType.NONE);
shape.setBehindText(true);

shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);

// Calculate image left and top position so it appears in the center of the page.
shape.setLeft((builder.getPageSetup().getPageWidth() - shape.getWidth()) / 2);
shape.setTop((builder.getPageSetup().getPageHeight() - shape.getHeight()) / 2);

doc.save(getArtifactsDir() + "DocumentBuilder.InsertWatermark.doc");

Example:

Shows how to insert a floating image and specify its position and size.
// This creates a builder and also an empty document inside the builder.
DocumentBuilder builder = new DocumentBuilder();

// By default, the image is inline.
Shape shape = builder.insertImage(getImageDir() + "Hammer.wmf");

// Make the image float, put it behind text and center on the page.
shape.setWrapType(WrapType.NONE);

// Make position relative to the page.
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);

// Make the shape occupy a band 50 points high at the very top of the page.
shape.setLeft(0);
shape.setTop(0);
shape.setWidth(builder.getCurrentSection().getPageSetup().getPageWidth());
shape.setHeight(50);

// The width will be scaled to the height and the dimensions of the real image
final double delta = 0.05;
Assert.assertEquals(shape.getWidth(), 50.85d, delta);

// The Bottom and Right members contain the locations of the bottom and right edges of the image
Assert.assertEquals(shape.getBottom(), shape.getTop() + shape.getHeight());
Assert.assertEquals(shape.getRight(), shape.getLeft() + shape.getWidth());

builder.getDocument().save(getArtifactsDir() + "Image.CreateFloatingPositionSize.doc");

getPaperSize/setPaperSize

public int getPaperSize() / public void setPaperSize(int value)
Returns or sets the paper size. The value of the property is PaperSize integer constant.

Setting this property updates PageWidth and PageHeight values. Setting this value to PaperSize.CUSTOM does not change existing values.

Example:

Specifies paper size, orientation, margins and other settings for a section.
DocumentBuilder builder = new DocumentBuilder();

PageSetup ps = builder.getPageSetup();
ps.setPaperSize(PaperSize.LEGAL);
ps.setOrientation(Orientation.LANDSCAPE);
ps.setTopMargin(ConvertUtil.inchToPoint(1.0));
ps.setBottomMargin(ConvertUtil.inchToPoint(1.0));
ps.setLeftMargin(ConvertUtil.inchToPoint(1.5));
ps.setRightMargin(ConvertUtil.inchToPoint(1.5));
ps.setHeaderDistance(ConvertUtil.inchToPoint(0.2));
ps.setFooterDistance(ConvertUtil.inchToPoint(0.2));

builder.writeln("Hello world.");

builder.getDocument().save(getArtifactsDir() + "PageSetup.PageMargins.doc");

Example:

Creates a simple document from scratch using the Aspose.Words object model.
// Create an "empty" document. Note that like in Microsoft Word,
// the empty document has one section, body and one paragraph in it.
Document doc = new Document();

// This truly makes the document empty. No sections (not possible in Microsoft Word).
doc.removeAllChildren();

// Create a new section node.
// Note that the section has not yet been added to the document,
// but we have to specify the parent document.
Section section = new Section(doc);

// Append the section to the document.
doc.appendChild(section);

// Lets set some properties for the section.
section.getPageSetup().setSectionStart(SectionStart.NEW_PAGE);
section.getPageSetup().setPaperSize(PaperSize.LETTER);

// The section that we created is empty, lets populate it. The section needs at least the Body node.
Body body = new Body(doc);
section.appendChild(body);

// The body needs to have at least one paragraph.
// Note that the paragraph has not yet been added to the document,
// but we have to specify the parent document.
// The parent document is needed so the paragraph can correctly work
// with styles and other document-wide information.
Paragraph para = new Paragraph(doc);
body.appendChild(para);

// We can set some formatting for the paragraph
para.getParagraphFormat().setStyleName("Heading 1");
para.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);

// So far we have one empty paragraph in the document.
// The document is valid and can be saved, but lets add some text before saving.
// Create a new run of text and add it to our paragraph.
Run run = new Run(doc);
run.setText("Hello World!");
run.getFont().setColor(Color.RED);
para.appendChild(run);

// As a matter of interest, you can retrieve text of the whole document and
// see that \x000c is automatically appended. \x000c is the end of section character.
System.out.println(doc.getText());

// Save the document.
doc.save(getArtifactsDir() + "Section.CreateFromScratch.doc");

getRestartPageNumbering/setRestartPageNumbering

public boolean getRestartPageNumbering() / public void setRestartPageNumbering(boolean value)
True if page numbering restarts at the beginning of the section. If set to false, the RestartPageNumbering property will override the PageStartingNumber property so that page numbering can continue from the previous section.

Example:

Shows how to control page numbering per section.
// This document has two sections, but no page numbers yet.
Document doc = new Document(getMyDir() + "PageSetup.PageNumbering.doc");

// Use document builder to create a header with a page number field for the first section.
// The page number will look like "Page V".
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToSection(0);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.write("Page ");
builder.insertField("PAGE", "");

// Set first section page numbering.
Section section = doc.getSections().get(0);
section.getPageSetup().setRestartPageNumbering(true);
section.getPageSetup().setPageStartingNumber(5);
section.getPageSetup().setPageNumberStyle(NumberStyle.UPPERCASE_ROMAN);

// Create a header for the section. 
// The page number will look like " - 10 - ".
builder.moveToSection(1);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.write(" - ");
builder.insertField("PAGE", "");
builder.write(" - ");

// Set second section page numbering.
section = doc.getSections().get(1);
section.getPageSetup().setPageStartingNumber(10);
section.getPageSetup().setRestartPageNumbering(true);
section.getPageSetup().setPageNumberStyle(NumberStyle.ARABIC);

doc.save(getArtifactsDir() + "PageSetup.PageNumbering.doc");

getRightMargin/setRightMargin

public double getRightMargin() / public void setRightMargin(double value)
Returns or sets the distance (in points) between the right edge of the page and the right boundary of the body text.

Example:

Specifies paper size, orientation, margins and other settings for a section.
DocumentBuilder builder = new DocumentBuilder();

PageSetup ps = builder.getPageSetup();
ps.setPaperSize(PaperSize.LEGAL);
ps.setOrientation(Orientation.LANDSCAPE);
ps.setTopMargin(ConvertUtil.inchToPoint(1.0));
ps.setBottomMargin(ConvertUtil.inchToPoint(1.0));
ps.setLeftMargin(ConvertUtil.inchToPoint(1.5));
ps.setRightMargin(ConvertUtil.inchToPoint(1.5));
ps.setHeaderDistance(ConvertUtil.inchToPoint(0.2));
ps.setFooterDistance(ConvertUtil.inchToPoint(0.2));

builder.writeln("Hello world.");

builder.getDocument().save(getArtifactsDir() + "PageSetup.PageMargins.doc");

getRtlGutter/setRtlGutter

public boolean getRtlGutter() / public void setRtlGutter(boolean value)
Gets or sets whether Microsoft Word uses gutters for the section based on a right-to-left language or a left-to-right language.

Example:

Shows how to set gutter margins.
Document doc = new Document();

// Insert text spanning several pages
DocumentBuilder builder = new DocumentBuilder(doc);
for (int i = 0; i < 6; i++) {
    builder.write("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.");
    builder.insertBreak(BreakType.PAGE_BREAK);
}

// We can access the gutter margin in the section's page options,
// which is a margin which is added to the page margin at one side of the page
PageSetup pageSetup = doc.getSections().get(0).getPageSetup();
pageSetup.setGutter(100.0);

// If our text is LTR, the gutter will appear on the left side of the page
// Setting this flag will move it to the right side
pageSetup.setRtlGutter(true);

// Mirroring the margins will make the gutter alternate in position from page to page
pageSetup.setMultiplePages(MultiplePagesType.MIRROR_MARGINS);

doc.save(getArtifactsDir() + "PageSetup.Gutter.docx");

getSectionStart/setSectionStart

public int getSectionStart() / public void setSectionStart(int value)
Returns or sets the type of section break for the specified object. The value of the property is SectionStart integer constant.

Example:

Specifies how the section starts, from a new page, on the same page or other.
Document doc = new Document();
doc.getSections().get(0).getPageSetup().setSectionStart(com.aspose.words.SectionStart.CONTINUOUS);

Example:

Creates a simple document from scratch using the Aspose.Words object model.
// Create an "empty" document. Note that like in Microsoft Word,
// the empty document has one section, body and one paragraph in it.
Document doc = new Document();

// This truly makes the document empty. No sections (not possible in Microsoft Word).
doc.removeAllChildren();

// Create a new section node.
// Note that the section has not yet been added to the document,
// but we have to specify the parent document.
Section section = new Section(doc);

// Append the section to the document.
doc.appendChild(section);

// Lets set some properties for the section.
section.getPageSetup().setSectionStart(SectionStart.NEW_PAGE);
section.getPageSetup().setPaperSize(PaperSize.LETTER);

// The section that we created is empty, lets populate it. The section needs at least the Body node.
Body body = new Body(doc);
section.appendChild(body);

// The body needs to have at least one paragraph.
// Note that the paragraph has not yet been added to the document,
// but we have to specify the parent document.
// The parent document is needed so the paragraph can correctly work
// with styles and other document-wide information.
Paragraph para = new Paragraph(doc);
body.appendChild(para);

// We can set some formatting for the paragraph
para.getParagraphFormat().setStyleName("Heading 1");
para.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);

// So far we have one empty paragraph in the document.
// The document is valid and can be saved, but lets add some text before saving.
// Create a new run of text and add it to our paragraph.
Run run = new Run(doc);
run.setText("Hello World!");
run.getFont().setColor(Color.RED);
para.appendChild(run);

// As a matter of interest, you can retrieve text of the whole document and
// see that \x000c is automatically appended. \x000c is the end of section character.
System.out.println(doc.getText());

// Save the document.
doc.save(getArtifactsDir() + "Section.CreateFromScratch.doc");

getSheetsPerBooklet/setSheetsPerBooklet

public int getSheetsPerBooklet() / public void setSheetsPerBooklet(int value)
Returns or sets the number of pages to be included in each booklet.

Example:

Shows how to create a booklet.
Document doc = new Document();

// Use a document builder to create 16 pages of content that will be compiled in a booklet
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("My Booklet:");

for (int i = 0; i < 15; i++) {
    builder.insertBreak(BreakType.PAGE_BREAK);
    builder.write(MessageFormat.format("Booklet face #{0}", i));
}

// Set the number of sheets that will be used by the printer to create the booklet
// After being printed on both sides, the sheets can be stacked and folded down the centre
// The contents that we placed in such a way that they will be in order once the booklet is folded
// We can only specify the number of sheets in multiples of 4
PageSetup pageSetup = doc.getSections().get(0).getPageSetup();
pageSetup.setMultiplePages(MultiplePagesType.BOOK_FOLD_PRINTING);
pageSetup.setSheetsPerBooklet(4);

doc.save(getArtifactsDir() + "PageSetup.Booklet.docx");

getSuppressEndnotes/setSuppressEndnotes

public boolean getSuppressEndnotes() / public void setSuppressEndnotes(boolean value)
True if endnotes are printed at the end of the next section that doesn't suppress endnotes. Suppressed endnotes are printed before the endnotes in that section.

Example:

Shows how to store endnotes at the end of each section instead of the document and manipulate their positions.
public void suppressEndnotes() throws Exception {
    // Create a new document and make it empty
    Document doc = new Document();
    doc.removeAllChildren();

    // Normally endnotes are all stored at the end of a document, but this option lets us store them at the end of each section
    doc.getEndnoteOptions().setPosition(EndnotePosition.END_OF_SECTION);

    // Create 3 new sections, each having a paragraph and an endnote at the end
    insertSection(doc, "Section 1", "Endnote 1, will stay in section 1");
    insertSection(doc, "Section 2", "Endnote 2, will be pushed down to section 3");
    insertSection(doc, "Section 3", "Endnote 3, will stay in section 3");

    // Each section contains its own page setup object
    // Setting this value will push this section's endnotes down to the next section
    PageSetup pageSetup = doc.getSections().get(1).getPageSetup();
    pageSetup.setSuppressEndnotes(true);

    doc.save(getArtifactsDir() + "PageSetup.SuppressEndnotes.docx");
}

/// <summary>
/// Add a section to the end of a document, give it a body and a paragraph, then add text and an endnote to that paragraph
/// </summary>
private void insertSection(Document doc, String sectionBodyText, String endnoteText) {
    Section section = new Section(doc);

    doc.appendChild(section);

    Body body = new Body(doc);
    section.appendChild(body);

    Assert.assertEquals(body.getParentNode(), section);

    Paragraph para = new Paragraph(doc);
    body.appendChild(para);

    Assert.assertEquals(para.getParentNode(), body);

    DocumentBuilder builder = new DocumentBuilder(doc);
    builder.moveTo(para);
    builder.write(sectionBodyText);
    builder.insertFootnote(FootnoteType.ENDNOTE, endnoteText);
}

getTextColumns

public TextColumnCollection getTextColumns()
Returns a collection that represents the set of text columns.

Example:

Creates multiple evenly spaced columns in a section using DocumentBuilder.
DocumentBuilder builder = new DocumentBuilder();

TextColumnCollection columns = builder.getPageSetup().getTextColumns();
// Make spacing between columns wider.
columns.setSpacing(100);
// This creates two columns of equal width.
columns.setCount(2);

builder.writeln("Text in column 1.");
builder.insertBreak(BreakType.COLUMN_BREAK);
builder.writeln("Text in column 2.");

builder.getDocument().save(getArtifactsDir() + "PageSetup.ColumnsSameWidth.doc");

getTextOrientation/setTextOrientation

public int getTextOrientation() / public void setTextOrientation(int value)
Allows to specify TextOrientation for the whole page. Default value is TextOrientation.HORIZONTALThe value of the property is TextOrientation integer constant.This property is only supported for MS Word native formats DOCX, WML, RTF and DOC.

Example:

Shows how to set text orientation.
Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world!");

// Setting this value will rotate the section's text 90 degrees to the right
PageSetup pageSetup = doc.getSections().get(0).getPageSetup();
pageSetup.setTextOrientation(com.aspose.words.TextOrientation.UPWARD);

doc.save(getArtifactsDir() + "PageSetup.TextOrientation.docx");

getTopMargin/setTopMargin

public double getTopMargin() / public void setTopMargin(double value)
Returns or sets the distance (in points) between the top edge of the page and the top boundary of the body text.

Example:

Specifies paper size, orientation, margins and other settings for a section.
DocumentBuilder builder = new DocumentBuilder();

PageSetup ps = builder.getPageSetup();
ps.setPaperSize(PaperSize.LEGAL);
ps.setOrientation(Orientation.LANDSCAPE);
ps.setTopMargin(ConvertUtil.inchToPoint(1.0));
ps.setBottomMargin(ConvertUtil.inchToPoint(1.0));
ps.setLeftMargin(ConvertUtil.inchToPoint(1.5));
ps.setRightMargin(ConvertUtil.inchToPoint(1.5));
ps.setHeaderDistance(ConvertUtil.inchToPoint(0.2));
ps.setFooterDistance(ConvertUtil.inchToPoint(0.2));

builder.writeln("Hello world.");

builder.getDocument().save(getArtifactsDir() + "PageSetup.PageMargins.doc");

getVerticalAlignment/setVerticalAlignment

public int getVerticalAlignment() / public void setVerticalAlignment(int value)
Returns or sets the vertical alignment of text on each page in a document or section. The value of the property is PageVerticalAlignment integer constant.

Example:

Shows how to insert sections using DocumentBuilder, specify page setup for a section and reset page setup to defaults.
DocumentBuilder builder = new DocumentBuilder();

// Modify the first section in the document.
builder.getPageSetup().setOrientation(Orientation.LANDSCAPE);
builder.getPageSetup().setVerticalAlignment(PageVerticalAlignment.CENTER);
builder.writeln("Section 1, landscape oriented and text vertically centered.");

// Start a new section and reset its formatting to defaults.
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
builder.getPageSetup().clearFormatting();
builder.writeln("Section 2, back to default Letter paper size, portrait orientation and top alignment.");

builder.getDocument().save(getArtifactsDir() + "PageSetup.ClearFormatting.doc");

Method Detail

clearFormatting

public void clearFormatting()
Resets page setup to default paper size, margins and orientation.

Example:

Shows how to insert sections using DocumentBuilder, specify page setup for a section and reset page setup to defaults.
DocumentBuilder builder = new DocumentBuilder();

// Modify the first section in the document.
builder.getPageSetup().setOrientation(Orientation.LANDSCAPE);
builder.getPageSetup().setVerticalAlignment(PageVerticalAlignment.CENTER);
builder.writeln("Section 1, landscape oriented and text vertically centered.");

// Start a new section and reset its formatting to defaults.
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
builder.getPageSetup().clearFormatting();
builder.writeln("Section 2, back to default Letter paper size, portrait orientation and top alignment.");

builder.getDocument().save(getArtifactsDir() + "PageSetup.ClearFormatting.doc");

See Also:
          Aspose.Words Documentation - the home page for the Aspose.Words Product Documentation.
          Aspose.Words Support Forum - our preferred method of support.