public class ParagraphFormat
Example:
Document doc = new Document();
// A newly created blank document still comes one section, one body and one paragraph
// Calling this method will remove all those nodes to completely empty the document
doc.removeAllChildren();
// This document now has no composite nodes that content can be added to
// If we wish to edit it, we will need to repopulate its node collection,
// which we will start to do with by creating a new Section node
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);
// A section needs a body, which will contain all other nodes that can be edited
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);
// Now we can begin adding content to the document
Run run = new Run(doc);
run.setText("Hello World!");
run.getFont().setColor(Color.RED);
para.appendChild(run);
Assert.assertEquals("Hello World!" + ControlChar.SECTION_BREAK_CHAR, doc.getText());
doc.save(getArtifactsDir() + "Section.CreateFromScratch.docx");
Property Getters/Setters Summary | ||
---|---|---|
boolean | getAddSpaceBetweenFarEastAndAlpha() | |
void | setAddSpaceBetweenFarEastAndAlpha(booleanvalue) | |
Gets or sets a flag indicating whether inter-character spacing is automatically adjusted between regions of Latin text and regions of East Asian text in the current paragraph. | ||
boolean | getAddSpaceBetweenFarEastAndDigit() | |
void | setAddSpaceBetweenFarEastAndDigit(booleanvalue) | |
Gets or sets a flag indicating whether inter-character spacing is automatically adjusted between regions of numbers and regions of East Asian text in the current paragraph. | ||
int | getAlignment() | |
void | setAlignment(intvalue) | |
Gets or sets text alignment for the paragraph. The value of the property is ParagraphAlignment integer constant. | ||
boolean | getBidi() | |
void | setBidi(booleanvalue) | |
Gets or sets whether this is a right-to-left paragraph. | ||
BorderCollection | getBorders() | |
Gets collection of borders of the paragraph.
|
||
double | getCharacterUnitFirstLineIndent() | |
void | setCharacterUnitFirstLineIndent(doublevalue) | |
Gets or sets the value (in characters) for the first-line or hanging indent.
Use positive values to set the first-line indent, and negative values to set the hanging indent. |
||
double | getCharacterUnitLeftIndent() | |
void | setCharacterUnitLeftIndent(doublevalue) | |
Gets or sets the left indent value (in characters) for the specified paragraphs. | ||
double | getCharacterUnitRightIndent() | |
void | setCharacterUnitRightIndent(doublevalue) | |
Gets or sets the right indent value (in characters) for the specified paragraphs. | ||
int | getDropCapPosition() | |
void | setDropCapPosition(intvalue) | |
Gets or sets the position for a drop cap text. The value of the property is DropCapPosition integer constant. | ||
boolean | getFarEastLineBreakControl() | |
void | setFarEastLineBreakControl(booleanvalue) | |
Gets or sets a flag indicating whether East Asian line-breaking rules are applied to the current paragraph. | ||
double | getFirstLineIndent() | |
void | setFirstLineIndent(doublevalue) | |
Gets or sets the value (in points) for a first line or hanging indent.
Use positive values to set the first-line indent, and negative values to set the hanging indent. |
||
boolean | getHangingPunctuation() | |
void | setHangingPunctuation(booleanvalue) | |
Gets or sets a flag indicating whether hanging punctuation is enabled for the current paragraph. | ||
boolean | isHeading() | |
True when the paragraph style is one of the built-in Heading styles.
|
||
boolean | isListItem() | |
True when the paragraph is an item in a bulleted or numbered list.
|
||
boolean | getKeepTogether() | |
void | setKeepTogether(booleanvalue) | |
True if all lines in the paragraph are to remain on the same page. | ||
boolean | getKeepWithNext() | |
void | setKeepWithNext(booleanvalue) | |
True if the paragraph is to remains on the same page as the paragraph that follows it. | ||
double | getLeftIndent() | |
void | setLeftIndent(doublevalue) | |
Gets or sets the value (in points) that represents the left indent for paragraph. | ||
double | getLineSpacing() | |
void | setLineSpacing(doublevalue) | |
Gets or sets the line spacing (in points) for the paragraph. | ||
int | getLineSpacingRule() | |
void | setLineSpacingRule(intvalue) | |
Gets or sets the line spacing for the paragraph. The value of the property is LineSpacingRule integer constant. | ||
int | getLinesToDrop() | |
void | setLinesToDrop(intvalue) | |
Gets or sets the number of lines of the paragraph text used to calculate the drop cap height. | ||
double | getLineUnitAfter() | |
void | setLineUnitAfter(doublevalue) | |
Gets or sets the amount of spacing (in gridlines) after the paragraphs. | ||
double | getLineUnitBefore() | |
void | setLineUnitBefore(doublevalue) | |
Gets or sets the amount of spacing (in gridlines) before the paragraphs. | ||
boolean | getNoSpaceBetweenParagraphsOfSameStyle() | |
void | setNoSpaceBetweenParagraphsOfSameStyle(booleanvalue) | |
When true, |
||
int | getOutlineLevel() | |
void | setOutlineLevel(intvalue) | |
Specifies the outline level of the paragraph in the document. The value of the property is OutlineLevel integer constant. | ||
boolean | getPageBreakBefore() | |
void | setPageBreakBefore(booleanvalue) | |
True if a page break is forced before the paragraph. | ||
double | getRightIndent() | |
void | setRightIndent(doublevalue) | |
Gets or sets the value (in points) that represents the right indent for paragraph. | ||
Shading | getShading() | |
Returns a Shading object that refers to the shading formatting for the paragraph.
|
||
boolean | getSnapToGrid() | |
void | setSnapToGrid(booleanvalue) | |
Specifies whether the current paragraph should use the document grid lines per page settings when laying out the contents in the paragraph. | ||
double | getSpaceAfter() | |
void | setSpaceAfter(doublevalue) | |
Gets or sets the amount of spacing (in points) after the paragraph. | ||
boolean | getSpaceAfterAuto() | |
void | setSpaceAfterAuto(booleanvalue) | |
True if the amount of spacing after the paragraph is set automatically. | ||
double | getSpaceBefore() | |
void | setSpaceBefore(doublevalue) | |
Gets or sets the amount of spacing (in points) before the paragraph. | ||
boolean | getSpaceBeforeAuto() | |
void | setSpaceBeforeAuto(booleanvalue) | |
True if the amount of spacing before the paragraph is set automatically. | ||
Style | getStyle() | |
void | ||
Gets or sets the paragraph style applied to this formatting. | ||
int | getStyleIdentifier() | |
void | setStyleIdentifier(intvalue) | |
Gets or sets the locale independent style identifier of the paragraph style applied to this formatting. The value of the property is StyleIdentifier integer constant. | ||
java.lang.String | getStyleName() | |
void | setStyleName(java.lang.Stringvalue) | |
Gets or sets the name of the paragraph style applied to this formatting. | ||
boolean | getSuppressAutoHyphens() | |
void | setSuppressAutoHyphens(booleanvalue) | |
Specifies whether the current paragraph should be exempted from any hyphenation which is applied in the document settings. | ||
boolean | getSuppressLineNumbers() | |
void | setSuppressLineNumbers(booleanvalue) | |
Specifies whether the current paragraph's lines should be exempted from line numbering which is applied in the parent section. | ||
TabStopCollection | getTabStops() | |
Gets the collection of custom tab stops defined for this object.
|
||
boolean | getWidowControl() | |
void | setWidowControl(booleanvalue) | |
True if the first and last lines in the paragraph are to remain on the same page as the rest of the paragraph. | ||
boolean | getWordWrap() | |
void | setWordWrap(booleanvalue) | |
If this property is false, Latin text in the middle of a word can be wrapped for the current paragraph. Otherwise Latin text is wrapped by whole words. |
Method Summary | ||
---|---|---|
void | clearFormatting() | |
Resets to default paragraph formatting.
|
public boolean getAddSpaceBetweenFarEastAndAlpha() / public void setAddSpaceBetweenFarEastAndAlpha(boolean value)
public boolean getAddSpaceBetweenFarEastAndDigit() / public void setAddSpaceBetweenFarEastAndDigit(boolean value)
public int getAlignment() / public void setAlignment(int value)
Example:
Shows how to construct an Aspose Words document node by node.Document doc = new Document(); // A newly created blank document still comes one section, one body and one paragraph // Calling this method will remove all those nodes to completely empty the document doc.removeAllChildren(); // This document now has no composite nodes that content can be added to // If we wish to edit it, we will need to repopulate its node collection, // which we will start to do with by creating a new Section node 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); // A section needs a body, which will contain all other nodes that can be edited 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); // Now we can begin adding content to the document Run run = new Run(doc); run.setText("Hello World!"); run.getFont().setColor(Color.RED); para.appendChild(run); Assert.assertEquals("Hello World!" + ControlChar.SECTION_BREAK_CHAR, doc.getText()); doc.save(getArtifactsDir() + "Section.CreateFromScratch.docx");
public boolean getBidi() / public void setBidi(boolean value)
When true, the runs and other inline objects in this paragraph are laid out right to left.
Example:
Shows how to create right-to-left language-compatible lists with BIDIOUTLINE fields.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // The BIDIOUTLINE field numbers paragraphs like the AUTONUM/LISTNUM fields, // but is only visible when a right-to-left editing language is enabled, such as Hebrew or Arabic. // The following field will display ".1", the RTL equivalent of list number "1.". FieldBidiOutline field = (FieldBidiOutline)builder.insertField(FieldType.FIELD_BIDI_OUTLINE, true); builder.writeln("שלום"); Assert.assertEquals(" BIDIOUTLINE ", field.getFieldCode()); // Add two more BIDIOUTLINE fields, which will display ".2" and ".3". builder.insertField(FieldType.FIELD_BIDI_OUTLINE, true); builder.writeln("שלום"); builder.insertField(FieldType.FIELD_BIDI_OUTLINE, true); builder.writeln("שלום"); // Set the horizontal text alignment for every paragraph in the document to RTL. for (Paragraph para : (Iterable<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true)) { para.getParagraphFormat().setBidi(true); } // If we enable a right-to-left editing language in Microsoft Word, our fields will display numbers. // Otherwise, they will display "###". doc.save(getArtifactsDir() + "Field.BIDIOUTLINE.docx");
public BorderCollection getBorders()
Example:
Shows how to insert a paragraph with a top border.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Border topBorder = builder.getParagraphFormat().getBorders().getByBorderType(BorderType.TOP); topBorder.setColor(Color.RED); topBorder.setLineWidth(4.0d); topBorder.setLineStyle(LineStyle.DASH_SMALL_GAP); builder.writeln("Text with a red top border."); doc.save(getArtifactsDir() + "Border.ParagraphTopBorder.docx");
public double getCharacterUnitFirstLineIndent() / public void setCharacterUnitFirstLineIndent(double value)
Use positive values to set the first-line indent, and negative values to set the hanging indent.
Example:
Shows how to change paragraph spacing and indents.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); ParagraphFormat format = doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat(); // Also ParagraphFormat.LeftIndent will be updated format.setCharacterUnitLeftIndent(10.0); // Also ParagraphFormat.RightIndent will be updated format.setCharacterUnitRightIndent(-5.5); // Also ParagraphFormat.FirstLineIndent will be updated format.setCharacterUnitFirstLineIndent(20.3); // Also ParagraphFormat.SpaceBefore will be updated format.setLineUnitBefore(5.1); // Also ParagraphFormat.SpaceAfter will be updated format.setLineUnitAfter(10.9); builder.writeln("Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); builder.write("测试文档测试文档测试文档测试文档测试文档测试文档测试文档测试文档测试" + "文档测试文档测试文档测试文档测试文档测试文档测试文档测试文档测试文档测试文档");
public double getCharacterUnitLeftIndent() / public void setCharacterUnitLeftIndent(double value)
Example:
Shows how to change paragraph spacing and indents.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); ParagraphFormat format = doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat(); // Also ParagraphFormat.LeftIndent will be updated format.setCharacterUnitLeftIndent(10.0); // Also ParagraphFormat.RightIndent will be updated format.setCharacterUnitRightIndent(-5.5); // Also ParagraphFormat.FirstLineIndent will be updated format.setCharacterUnitFirstLineIndent(20.3); // Also ParagraphFormat.SpaceBefore will be updated format.setLineUnitBefore(5.1); // Also ParagraphFormat.SpaceAfter will be updated format.setLineUnitAfter(10.9); builder.writeln("Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); builder.write("测试文档测试文档测试文档测试文档测试文档测试文档测试文档测试文档测试" + "文档测试文档测试文档测试文档测试文档测试文档测试文档测试文档测试文档测试文档");
public double getCharacterUnitRightIndent() / public void setCharacterUnitRightIndent(double value)
Example:
Shows how to change paragraph spacing and indents.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); ParagraphFormat format = doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat(); // Also ParagraphFormat.LeftIndent will be updated format.setCharacterUnitLeftIndent(10.0); // Also ParagraphFormat.RightIndent will be updated format.setCharacterUnitRightIndent(-5.5); // Also ParagraphFormat.FirstLineIndent will be updated format.setCharacterUnitFirstLineIndent(20.3); // Also ParagraphFormat.SpaceBefore will be updated format.setLineUnitBefore(5.1); // Also ParagraphFormat.SpaceAfter will be updated format.setLineUnitAfter(10.9); builder.writeln("Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); builder.write("测试文档测试文档测试文档测试文档测试文档测试文档测试文档测试文档测试" + "文档测试文档测试文档测试文档测试文档测试文档测试文档测试文档测试文档测试文档");
public int getDropCapPosition() / public void setDropCapPosition(int value)
Example:
Shows how to start a numbered list, add a bulleted list inside it, then return to the numbered list.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Create an outline list for the headings List outlineList = doc.getLists().add(ListTemplate.OUTLINE_NUMBERS); builder.getListFormat().setList(outlineList); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1); builder.writeln("This is my Chapter 1"); // Create a numbered list List numberedList = doc.getLists().add(ListTemplate.NUMBER_DEFAULT); builder.getListFormat().setList(numberedList); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.NORMAL); builder.writeln("Numbered list item 1."); // Every paragraph that comprises a list will have this flag Assert.assertTrue(builder.getCurrentParagraph().isListItem()); Assert.assertTrue(builder.getParagraphFormat().isListItem()); // Create a bulleted list List bulletedList = doc.getLists().add(ListTemplate.BULLET_DEFAULT); builder.getListFormat().setList(bulletedList); builder.getParagraphFormat().setLeftIndent(72.0); builder.writeln("Bulleted list item 1."); builder.writeln("Bulleted list item 2."); builder.getParagraphFormat().clearFormatting(); // Revert to the numbered list builder.getListFormat().setList(numberedList); builder.writeln("Numbered list item 2."); builder.writeln("Numbered list item 3."); // Revert to the outline list builder.getListFormat().setList(outlineList); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1); builder.writeln("This is my Chapter 2"); builder.getParagraphFormat().clearFormatting(); builder.getDocument().save(getArtifactsDir() + "Lists.NestedLists.docx");
public boolean getFarEastLineBreakControl() / public void setFarEastLineBreakControl(boolean value)
public double getFirstLineIndent() / public void setFirstLineIndent(double value)
Use positive values to set the first-line indent, and negative values to set the hanging indent.
public boolean getHangingPunctuation() / public void setHangingPunctuation(boolean value)
public boolean isHeading()
Example:
Shows how to create PDF document outline entries for headings.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Creating TOC entries builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1); Assert.assertTrue(builder.getParagraphFormat().isHeading()); builder.writeln("Heading 1"); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_4); builder.writeln("Heading 1.1.1.1"); builder.writeln("Heading 1.1.1.2"); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_9); builder.writeln("Heading 1.1.1.1.1.1.1.1.1"); builder.writeln("Heading 1.1.1.1.1.1.1.1.2"); // Create "PdfSaveOptions" with some mandatory parameters // "HeadingsOutlineLevels" specifies how many levels of headings to include in the document outline // "CreateMissingOutlineLevels" determining whether or not to create missing heading levels PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); pdfSaveOptions.getOutlineOptions().setHeadingsOutlineLevels(9); pdfSaveOptions.getOutlineOptions().setCreateMissingOutlineLevels(true); pdfSaveOptions.setSaveFormat(SaveFormat.PDF); doc.save(getArtifactsDir() + "PdfSaveOptions.CreateMissingOutlineLevels.pdf", pdfSaveOptions);
public boolean isListItem()
Example:
Shows how to start a numbered list, add a bulleted list inside it, then return to the numbered list.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Create an outline list for the headings List outlineList = doc.getLists().add(ListTemplate.OUTLINE_NUMBERS); builder.getListFormat().setList(outlineList); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1); builder.writeln("This is my Chapter 1"); // Create a numbered list List numberedList = doc.getLists().add(ListTemplate.NUMBER_DEFAULT); builder.getListFormat().setList(numberedList); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.NORMAL); builder.writeln("Numbered list item 1."); // Every paragraph that comprises a list will have this flag Assert.assertTrue(builder.getCurrentParagraph().isListItem()); Assert.assertTrue(builder.getParagraphFormat().isListItem()); // Create a bulleted list List bulletedList = doc.getLists().add(ListTemplate.BULLET_DEFAULT); builder.getListFormat().setList(bulletedList); builder.getParagraphFormat().setLeftIndent(72.0); builder.writeln("Bulleted list item 1."); builder.writeln("Bulleted list item 2."); builder.getParagraphFormat().clearFormatting(); // Revert to the numbered list builder.getListFormat().setList(numberedList); builder.writeln("Numbered list item 2."); builder.writeln("Numbered list item 3."); // Revert to the outline list builder.getListFormat().setList(outlineList); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1); builder.writeln("This is my Chapter 2"); builder.getParagraphFormat().clearFormatting(); builder.getDocument().save(getArtifactsDir() + "Lists.NestedLists.docx");
public boolean getKeepTogether() / public void setKeepTogether(boolean value)
public boolean getKeepWithNext() / public void setKeepWithNext(boolean value)
Example:
Shows how to set a table to stay together on the same page.Document doc = new Document(getMyDir() + "Table spanning two pages.docx"); Table table = (Table) doc.getChild(NodeType.TABLE, 0, true); // Enabling KeepWithNext for every paragraph in the table except for the last ones in the last row // will prevent the table from being split across pages for (Cell cell : (Iterable<Cell>) table.getChildNodes(NodeType.CELL, true)) for (Paragraph para : cell.getParagraphs()) { Assert.assertTrue(para.isInCell()); if (!(cell.getParentRow().isLastRow() && para.isEndOfCell())) para.getParagraphFormat().setKeepWithNext(true); } doc.save(getArtifactsDir() + "Table.KeepTableTogether.docx");
public double getLeftIndent() / public void setLeftIndent(double value)
Example:
Shows how to configure paragraph formatting to create off-center text.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Center all text that the document builder writes, and set up indents. // The indent configuration below will create a body of text that will sit asymmetrically on the page. // The "center" that we align the text to will be the middle of the body of text, not the middle of the page. ParagraphFormat paragraphFormat = builder.getParagraphFormat(); paragraphFormat.setAlignment(ParagraphAlignment.CENTER); paragraphFormat.setLeftIndent(100.0); paragraphFormat.setRightIndent(50.0); paragraphFormat.setSpaceAfter(25.0); builder.writeln( "This paragraph demonstrates how left and right indentation affects word wrapping."); builder.writeln( "The space between the above paragraph and this one depends on the DocumentBuilder's paragraph format."); doc.save(getArtifactsDir() + "DocumentBuilder.SetParagraphFormatting.docx");
public double getLineSpacing() / public void setLineSpacing(double value)
When LineSpacingRule property is set to AtLeast, the line spacing can be greater than or equal to, but never less than the specified LineSpacing value.
When LineSpacingRule property is set to Exactly, the line spacing never changes from the specified LineSpacing value, even if a larger font is used within the paragraph.
Example:
Shows how to work with line spacing.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Set the paragraph's line spacing to have a minimum value // This will give vertical padding to lines of text of any size that's too small to maintain the line height builder.getParagraphFormat().setLineSpacingRule(LineSpacingRule.AT_LEAST); builder.getParagraphFormat().setLineSpacing(20.0); builder.writeln("Minimum line spacing of 20."); builder.writeln("Minimum line spacing of 20."); // Set the line spacing to always be exactly 5 points // If the font size is larger than the spacing, the top of the text will be truncated builder.insertParagraph(); builder.getParagraphFormat().setLineSpacingRule(LineSpacingRule.EXACTLY); builder.getParagraphFormat().setLineSpacing(5.0); builder.writeln("Line spacing of exactly 5."); builder.writeln("Line spacing of exactly 5."); // Set the line spacing to a multiple of the default line spacing, which is 12 points by default // 18 points will set the spacing to always be 1.5 lines, which will scale with different font sizes builder.insertParagraph(); builder.getParagraphFormat().setLineSpacingRule(LineSpacingRule.MULTIPLE); builder.getParagraphFormat().setLineSpacing(18.0); builder.writeln("Line spacing of 1.5 default lines."); builder.writeln("Line spacing of 1.5 default lines."); doc.save(getArtifactsDir() + "Paragraph.LineSpacing.docx");
public int getLineSpacingRule() / public void setLineSpacingRule(int value)
Example:
Shows how to work with line spacing.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Set the paragraph's line spacing to have a minimum value // This will give vertical padding to lines of text of any size that's too small to maintain the line height builder.getParagraphFormat().setLineSpacingRule(LineSpacingRule.AT_LEAST); builder.getParagraphFormat().setLineSpacing(20.0); builder.writeln("Minimum line spacing of 20."); builder.writeln("Minimum line spacing of 20."); // Set the line spacing to always be exactly 5 points // If the font size is larger than the spacing, the top of the text will be truncated builder.insertParagraph(); builder.getParagraphFormat().setLineSpacingRule(LineSpacingRule.EXACTLY); builder.getParagraphFormat().setLineSpacing(5.0); builder.writeln("Line spacing of exactly 5."); builder.writeln("Line spacing of exactly 5."); // Set the line spacing to a multiple of the default line spacing, which is 12 points by default // 18 points will set the spacing to always be 1.5 lines, which will scale with different font sizes builder.insertParagraph(); builder.getParagraphFormat().setLineSpacingRule(LineSpacingRule.MULTIPLE); builder.getParagraphFormat().setLineSpacing(18.0); builder.writeln("Line spacing of 1.5 default lines."); builder.writeln("Line spacing of 1.5 default lines."); doc.save(getArtifactsDir() + "Paragraph.LineSpacing.docx");
public int getLinesToDrop() / public void setLinesToDrop(int value)
Example:
Shows how to set the size of the drop cap text.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Setting this attribute will designate the current paragraph as a drop cap, // in this case with a height of 4 lines of text builder.getParagraphFormat().setLinesToDrop(4); builder.write("H"); // Any subsequent paragraphs will wrap around the drop cap builder.insertParagraph(); builder.write("ello world."); doc.save(getArtifactsDir() + "Paragraph.LinesToDrop.odt");
public double getLineUnitAfter() / public void setLineUnitAfter(double value)
Example:
Shows how to change paragraph spacing and indents.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); ParagraphFormat format = doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat(); // Also ParagraphFormat.LeftIndent will be updated format.setCharacterUnitLeftIndent(10.0); // Also ParagraphFormat.RightIndent will be updated format.setCharacterUnitRightIndent(-5.5); // Also ParagraphFormat.FirstLineIndent will be updated format.setCharacterUnitFirstLineIndent(20.3); // Also ParagraphFormat.SpaceBefore will be updated format.setLineUnitBefore(5.1); // Also ParagraphFormat.SpaceAfter will be updated format.setLineUnitAfter(10.9); builder.writeln("Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); builder.write("测试文档测试文档测试文档测试文档测试文档测试文档测试文档测试文档测试" + "文档测试文档测试文档测试文档测试文档测试文档测试文档测试文档测试文档测试文档");
public double getLineUnitBefore() / public void setLineUnitBefore(double value)
Example:
Shows how to change paragraph spacing and indents.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); ParagraphFormat format = doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat(); // Also ParagraphFormat.LeftIndent will be updated format.setCharacterUnitLeftIndent(10.0); // Also ParagraphFormat.RightIndent will be updated format.setCharacterUnitRightIndent(-5.5); // Also ParagraphFormat.FirstLineIndent will be updated format.setCharacterUnitFirstLineIndent(20.3); // Also ParagraphFormat.SpaceBefore will be updated format.setLineUnitBefore(5.1); // Also ParagraphFormat.SpaceAfter will be updated format.setLineUnitAfter(10.9); builder.writeln("Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); builder.write("测试文档测试文档测试文档测试文档测试文档测试文档测试文档测试文档测试" + "文档测试文档测试文档测试文档测试文档测试文档测试文档测试文档测试文档测试文档");
public boolean getNoSpaceBetweenParagraphsOfSameStyle() / public void setNoSpaceBetweenParagraphsOfSameStyle(boolean value)
This setting only takes affect when applied to a paragraph style. If applied to a paragraph directly, it has no effect.
Example:
Shows how to work with paragraph spacing.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Set the amount of white space before and after each paragraph to 12 points builder.getParagraphFormat().setSpaceBefore(12.0f); builder.getParagraphFormat().setSpaceAfter(12.0f); // We can set these flags to apply default spacing, effectively ignoring the spacing in the attributes we set above Assert.assertFalse(builder.getParagraphFormat().getSpaceAfterAuto()); Assert.assertFalse(builder.getParagraphFormat().getSpaceBeforeAuto()); Assert.assertFalse(builder.getParagraphFormat().getNoSpaceBetweenParagraphsOfSameStyle()); // Insert two paragraphs which will have padding above and below them and save the document builder.writeln("Paragraph 1."); builder.writeln("Paragraph 2."); doc.save(getArtifactsDir() + "Paragraph.ParagraphSpacing.docx");
public int getOutlineLevel() / public void setOutlineLevel(int value)
Example:
Shows how to set paragraph outline levels to create collapsible text.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Each paragraph has an OutlineLevel, which could be any number from 1 to 9, or at the default "BodyText" value // Setting the attribute to one of the numbered values will enable an arrow in Microsoft Word // next to the beginning of the paragraph that, when clicked, will collapse the paragraph builder.getParagraphFormat().setOutlineLevel(com.aspose.words.OutlineLevel.LEVEL_1); builder.writeln("Paragraph outline level 1."); // Level 1 is the topmost level, which practically means that clicking its arrow will also collapse // any following paragraph with a lower level, like the paragraphs below builder.getParagraphFormat().setOutlineLevel(com.aspose.words.OutlineLevel.LEVEL_2); builder.writeln("Paragraph outline level 2."); // Two paragraphs of the same level will not collapse each other builder.getParagraphFormat().setOutlineLevel(com.aspose.words.OutlineLevel.LEVEL_3); builder.writeln("Paragraph outline level 3."); builder.writeln("Paragraph outline level 3."); // The default "BodyText" value is the lowest builder.getParagraphFormat().setOutlineLevel(com.aspose.words.OutlineLevel.BODY_TEXT); builder.writeln("Paragraph at main text level."); doc.save(getArtifactsDir() + "Paragraph.OutlineLevel.docx");
public boolean getPageBreakBefore() / public void setPageBreakBefore(boolean value)
Example:
Shows how to force a page break before each paragraph.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Set this to insert a page break before this paragraph builder.getParagraphFormat().setPageBreakBefore(true); // The value we set is propagated to all paragraphs that are created afterwards builder.writeln("Paragraph 1, page 1."); builder.writeln("Paragraph 2, page 2."); doc.save(getArtifactsDir() + "Paragraph.PageBreakBefore.docx");
public double getRightIndent() / public void setRightIndent(double value)
Example:
Shows how to configure paragraph formatting to create off-center text.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Center all text that the document builder writes, and set up indents. // The indent configuration below will create a body of text that will sit asymmetrically on the page. // The "center" that we align the text to will be the middle of the body of text, not the middle of the page. ParagraphFormat paragraphFormat = builder.getParagraphFormat(); paragraphFormat.setAlignment(ParagraphAlignment.CENTER); paragraphFormat.setLeftIndent(100.0); paragraphFormat.setRightIndent(50.0); paragraphFormat.setSpaceAfter(25.0); builder.writeln( "This paragraph demonstrates how left and right indentation affects word wrapping."); builder.writeln( "The space between the above paragraph and this one depends on the DocumentBuilder's paragraph format."); doc.save(getArtifactsDir() + "DocumentBuilder.SetParagraphFormatting.docx");
public Shading getShading()
Example:
Shows how to decorate text with borders and shading.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); BorderCollection borders = builder.getParagraphFormat().getBorders(); borders.setDistanceFromText(20.0); borders.getByBorderType(BorderType.LEFT).setLineStyle(LineStyle.DOUBLE); borders.getByBorderType(BorderType.RIGHT).setLineStyle(LineStyle.DOUBLE); borders.getByBorderType(BorderType.TOP).setLineStyle(LineStyle.DOUBLE); borders.getByBorderType(BorderType.BOTTOM).setLineStyle(LineStyle.DOUBLE); Shading shading = builder.getParagraphFormat().getShading(); shading.setTexture(TextureIndex.TEXTURE_DIAGONAL_CROSS); shading.setBackgroundPatternColor(new Color(240, 128, 128)); // Light Coral shading.setForegroundPatternColor(new Color(255, 160, 122)); // Light Salmon builder.write("This paragraph is formatted with a double border and shading."); doc.save(getArtifactsDir() + "DocumentBuilder.ApplyBordersAndShading.docx");
public boolean getSnapToGrid() / public void setSnapToGrid(boolean value)
public double getSpaceAfter() / public void setSpaceAfter(double value)
Has no effect when
Valid values range from 0 to 1584 inclusive.
Example:
Shows how to work with paragraph spacing.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Set the amount of white space before and after each paragraph to 12 points builder.getParagraphFormat().setSpaceBefore(12.0f); builder.getParagraphFormat().setSpaceAfter(12.0f); // We can set these flags to apply default spacing, effectively ignoring the spacing in the attributes we set above Assert.assertFalse(builder.getParagraphFormat().getSpaceAfterAuto()); Assert.assertFalse(builder.getParagraphFormat().getSpaceBeforeAuto()); Assert.assertFalse(builder.getParagraphFormat().getNoSpaceBetweenParagraphsOfSameStyle()); // Insert two paragraphs which will have padding above and below them and save the document builder.writeln("Paragraph 1."); builder.writeln("Paragraph 2."); doc.save(getArtifactsDir() + "Paragraph.ParagraphSpacing.docx");
public boolean getSpaceAfterAuto() / public void setSpaceAfterAuto(boolean value)
When set to true, overrides the effect of
When you set paragraph Space Before and Space After to Auto, Microsoft Word adds 14 points spacing between paragraphs automatically according to the following rules:
Example:
Shows how to work with paragraph spacing.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Set the amount of white space before and after each paragraph to 12 points builder.getParagraphFormat().setSpaceBefore(12.0f); builder.getParagraphFormat().setSpaceAfter(12.0f); // We can set these flags to apply default spacing, effectively ignoring the spacing in the attributes we set above Assert.assertFalse(builder.getParagraphFormat().getSpaceAfterAuto()); Assert.assertFalse(builder.getParagraphFormat().getSpaceBeforeAuto()); Assert.assertFalse(builder.getParagraphFormat().getNoSpaceBetweenParagraphsOfSameStyle()); // Insert two paragraphs which will have padding above and below them and save the document builder.writeln("Paragraph 1."); builder.writeln("Paragraph 2."); doc.save(getArtifactsDir() + "Paragraph.ParagraphSpacing.docx");
public double getSpaceBefore() / public void setSpaceBefore(double value)
Has no effect when
Valid values range from 0 to 1584 inclusive.
Example:
Shows how to work with paragraph spacing.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Set the amount of white space before and after each paragraph to 12 points builder.getParagraphFormat().setSpaceBefore(12.0f); builder.getParagraphFormat().setSpaceAfter(12.0f); // We can set these flags to apply default spacing, effectively ignoring the spacing in the attributes we set above Assert.assertFalse(builder.getParagraphFormat().getSpaceAfterAuto()); Assert.assertFalse(builder.getParagraphFormat().getSpaceBeforeAuto()); Assert.assertFalse(builder.getParagraphFormat().getNoSpaceBetweenParagraphsOfSameStyle()); // Insert two paragraphs which will have padding above and below them and save the document builder.writeln("Paragraph 1."); builder.writeln("Paragraph 2."); doc.save(getArtifactsDir() + "Paragraph.ParagraphSpacing.docx");
public boolean getSpaceBeforeAuto() / public void setSpaceBeforeAuto(boolean value)
When set to true, overrides the effect of
When you set paragraph Space Before and Space After to Auto, Microsoft Word adds 14 points spacing between paragraphs automatically according to the following rules:
Example:
Shows how to work with paragraph spacing.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Set the amount of white space before and after each paragraph to 12 points builder.getParagraphFormat().setSpaceBefore(12.0f); builder.getParagraphFormat().setSpaceAfter(12.0f); // We can set these flags to apply default spacing, effectively ignoring the spacing in the attributes we set above Assert.assertFalse(builder.getParagraphFormat().getSpaceAfterAuto()); Assert.assertFalse(builder.getParagraphFormat().getSpaceBeforeAuto()); Assert.assertFalse(builder.getParagraphFormat().getNoSpaceBetweenParagraphsOfSameStyle()); // Insert two paragraphs which will have padding above and below them and save the document builder.writeln("Paragraph 1."); builder.writeln("Paragraph 2."); doc.save(getArtifactsDir() + "Paragraph.ParagraphSpacing.docx");
public Style getStyle() / public void setStyle(Style value)
Example:
Shows how to create and use a paragraph style with list formatting.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Create a paragraph style and specify some formatting for it Style style = doc.getStyles().add(StyleType.PARAGRAPH, "MyStyle1"); style.getFont().setSize(24.0); style.getFont().setName("Verdana"); style.getParagraphFormat().setSpaceAfter(12.0); // Create a list and make sure the paragraphs that use this style will use this list style.getListFormat().setList(doc.getLists().add(ListTemplate.BULLET_DEFAULT)); style.getListFormat().setListLevelNumber(0); // Apply the paragraph style to the current paragraph in the document and add some text builder.getParagraphFormat().setStyle(style); builder.writeln("Hello World: MyStyle1, bulleted list."); // Change to a paragraph style that has no list formatting builder.getParagraphFormat().setStyle(doc.getStyles().get("Normal")); builder.writeln("Hello World: Normal."); builder.getDocument().save(getArtifactsDir() + "Styles.ParagraphStyleBulletedList.docx");
public int getStyleIdentifier() / public void setStyleIdentifier(int value)
Example:
Shows how to insert a Table of contents (TOC) into a document using heading styles as entries.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert a table of contents for the first page of the document. // Configure the table to pick up paragraphs with headings of levels 1 to 3. // Also, set its entries to be hyperlinks that will take us // to the location of the heading when left-clicked in Microsoft Word. builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u"); builder.insertBreak(BreakType.PAGE_BREAK); // Populate the table of contents by adding paragraphs with heading styles. // Each such heading will create an entry in the table, as long as its heading level is between 1 and 3. builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1); builder.writeln("Heading 1"); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2); builder.writeln("Heading 1.1"); builder.writeln("Heading 1.2"); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1); builder.writeln("Heading 2"); builder.writeln("Heading 3"); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2); builder.writeln("Heading 3.1"); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_3); builder.writeln("Heading 3.1.1"); builder.writeln("Heading 3.1.2"); builder.writeln("Heading 3.1.3"); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_4); builder.writeln("Heading 3.1.3.1"); builder.writeln("Heading 3.1.3.2"); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2); builder.writeln("Heading 3.2"); builder.writeln("Heading 3.3"); // A table of contents is a field of a type that needs to be updated to show an up-to-date result. doc.updateFields(); doc.save(getArtifactsDir() + "DocumentBuilder.InsertToc.docx");
public java.lang.String getStyleName() / public void setStyleName(java.lang.String value)
Example:
Shows how to construct an Aspose Words document node by node.Document doc = new Document(); // A newly created blank document still comes one section, one body and one paragraph // Calling this method will remove all those nodes to completely empty the document doc.removeAllChildren(); // This document now has no composite nodes that content can be added to // If we wish to edit it, we will need to repopulate its node collection, // which we will start to do with by creating a new Section node 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); // A section needs a body, which will contain all other nodes that can be edited 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); // Now we can begin adding content to the document Run run = new Run(doc); run.setText("Hello World!"); run.getFont().setColor(Color.RED); para.appendChild(run); Assert.assertEquals("Hello World!" + ControlChar.SECTION_BREAK_CHAR, doc.getText()); doc.save(getArtifactsDir() + "Section.CreateFromScratch.docx");
public boolean getSuppressAutoHyphens() / public void setSuppressAutoHyphens(boolean value)
public boolean getSuppressLineNumbers() / public void setSuppressLineNumbers(boolean value)
Example:
Shows how to enable Microsoft Word line numbering for a section.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Line numbering for each section can be configured via PageSetup PageSetup pageSetup = builder.getPageSetup(); pageSetup.setLineStartingNumber(1); pageSetup.setLineNumberCountBy(3); pageSetup.setLineNumberRestartMode(LineNumberRestartMode.RESTART_PAGE); pageSetup.setLineNumberDistanceFromText(50.0d); // LineNumberCountBy is set to 3, so every line that's a multiple of 3 // will display that line number to the left of the text for (int i = 1; i <= 25; i++) builder.writeln(MessageFormat.format("Line {0}.", i)); // The line counter will skip any paragraph with this flag set to true // Normally, the number "15" would normally appear next to this paragraph, which says "Line 15" // Since we set this flag to true and this paragraph is not counted by numbering, // number 15 will appear next to the next paragraph, "Line 16", and from then on counting will carry on as normal // until it will restart according to LineNumberRestartMode doc.getFirstSection().getBody().getParagraphs().get(14).getParagraphFormat().setSuppressLineNumbers(true); doc.save(getArtifactsDir() + "PageSetup.LineNumbers.docx");
public TabStopCollection getTabStops()
Example:
Shows how to modify the position of the right tab stop in TOC related paragraphs.Document doc = new Document(getMyDir() + "Table of contents.docx"); // Iterate through all paragraphs formatted using the TOC result based styles; this is any style between TOC and TOC9 for (Paragraph para : (Iterable<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true)) { if (para.getParagraphFormat().getStyle().getStyleIdentifier() >= StyleIdentifier.TOC_1 && para.getParagraphFormat().getStyle().getStyleIdentifier() <= StyleIdentifier.TOC_9) { // Get the first tab used in this paragraph, this should be the tab used to align the page numbers TabStop tab = para.getParagraphFormat().getTabStops().get(0); // Remove the old tab from the collection para.getParagraphFormat().getTabStops().removeByPosition(tab.getPosition()); // Insert a new tab using the same properties but at a modified position // We could also change the separators used (dots) by passing a different Leader type para.getParagraphFormat().getTabStops().add(tab.getPosition() - 50, tab.getAlignment(), tab.getLeader()); } } doc.save(getArtifactsDir() + "Styles.ChangeTocsTabStops.docx");
public boolean getWidowControl() / public void setWidowControl(boolean value)
Example:
Shows how to enable widow/orphan control for a paragraph.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert text that will not fit on one page, with one line spilling into page 2 builder.getFont().setSize(68.0); builder.writeln("Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); // This line is referred to as an "Orphan", // and a line left behind on the end of the previous page is likewise called a "Widow" // These are not ideal for readability, and the alternative to changing size/line spacing/page margins // in order to accomodate ill fitting text is this flag, for which the corresponding Microsoft Word option is // found in Home > Paragraph > Paragraph Settings (button on the bottom right of the tab) // In our document this will add more text to the orphan by putting two lines of text into the second page builder.getParagraphFormat().setWidowControl(true); doc.save(getArtifactsDir() + "Paragraph.WidowControl.docx");
public boolean getWordWrap() / public void setWordWrap(boolean value)
public void clearFormatting()
Example:
Shows how to start a numbered list, add a bulleted list inside it, then return to the numbered list.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Create an outline list for the headings List outlineList = doc.getLists().add(ListTemplate.OUTLINE_NUMBERS); builder.getListFormat().setList(outlineList); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1); builder.writeln("This is my Chapter 1"); // Create a numbered list List numberedList = doc.getLists().add(ListTemplate.NUMBER_DEFAULT); builder.getListFormat().setList(numberedList); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.NORMAL); builder.writeln("Numbered list item 1."); // Every paragraph that comprises a list will have this flag Assert.assertTrue(builder.getCurrentParagraph().isListItem()); Assert.assertTrue(builder.getParagraphFormat().isListItem()); // Create a bulleted list List bulletedList = doc.getLists().add(ListTemplate.BULLET_DEFAULT); builder.getListFormat().setList(bulletedList); builder.getParagraphFormat().setLeftIndent(72.0); builder.writeln("Bulleted list item 1."); builder.writeln("Bulleted list item 2."); builder.getParagraphFormat().clearFormatting(); // Revert to the numbered list builder.getListFormat().setList(numberedList); builder.writeln("Numbered list item 2."); builder.writeln("Numbered list item 3."); // Revert to the outline list builder.getListFormat().setList(outlineList); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1); builder.writeln("This is my Chapter 2"); builder.getParagraphFormat().clearFormatting(); builder.getDocument().save(getArtifactsDir() + "Lists.NestedLists.docx");