public class BorderCollection
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");
Property Getters/Setters Summary | ||
---|---|---|
Border | getBottom() | |
Gets the bottom border.
|
||
java.awt.Color | getColor() | |
void | setColor(java.awt.Colorvalue) | |
Gets or sets the border color. | ||
int | getCount() | |
Gets the number of borders in the collection.
|
||
double | getDistanceFromText() | |
void | setDistanceFromText(doublevalue) | |
Gets or sets distance of the border from text in points. | ||
Border | getHorizontal() | |
Gets the horizontal border that is used between cells or conforming paragraphs.
|
||
Border | getLeft() | |
Gets the left border.
|
||
int | getLineStyle() | |
void | setLineStyle(intvalue) | |
Gets or sets the border style. The value of the property is LineStyle integer constant. | ||
double | getLineWidth() | |
void | setLineWidth(doublevalue) | |
Gets or sets the border width in points. | ||
Border | getRight() | |
Gets the right border.
|
||
boolean | getShadow() | |
void | setShadow(booleanvalue) | |
Gets or sets a value indicating whether the border has a shadow. | ||
Border | getTop() | |
Gets the top border.
|
||
Border | getVertical() | |
Gets the vertical border that is used between cells.
|
||
Border | get(int index) | |
Retrieves a Border object by index.
|
||
Border | getByBorderType(int borderType) | |
Retrieves a Border object by border type.
|
Method Summary | ||
---|---|---|
void | clearFormatting() | |
Removes all borders of an object.
|
||
boolean | equals(BorderCollection brColl) | |
Compares collections of borders.
|
||
java.util.Iterator<Border> | iterator() | |
Returns an enumerator object that can be used to iterate over all borders in the collection.
|
public Border getBottom()
Example:
Shows how to apply border and shading color while building a table.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Start a table, and set a default color/thickness for its borders. Table table = builder.startTable(); table.setBorders(LineStyle.SINGLE, 2.0, Color.BLACK); // Create a row with two cells with different background colors. builder.insertCell(); builder.getCellFormat().getShading().setBackgroundPatternColor(Color.RED); builder.writeln("Row 1, Cell 1."); builder.insertCell(); builder.getCellFormat().getShading().setBackgroundPatternColor(Color.GREEN); builder.writeln("Row 1, Cell 2."); builder.endRow(); // Reset cell formatting to disable the background colors // set a custom border thickness for all new cells created by the builder, // then build a second row. builder.getCellFormat().clearFormatting(); builder.getCellFormat().getBorders().getLeft().setLineWidth(4.0); builder.getCellFormat().getBorders().getRight().setLineWidth(4.0); builder.getCellFormat().getBorders().getTop().setLineWidth(4.0); builder.getCellFormat().getBorders().getBottom().setLineWidth(4.0); builder.insertCell(); builder.writeln("Row 2, Cell 1."); builder.insertCell(); builder.writeln("Row 2, Cell 2."); doc.save(getArtifactsDir() + "DocumentBuilder.TableBordersAndShading.docx");
public java.awt.Color getColor() / public void setColor(java.awt.Color value)
Returns the color of the first border in the collection.
Sets the color of all borders in the collection excluding diagonal borders.
Example:
Shows how to create green wavy page border with a shadow.Document doc = new Document(); PageSetup pageSetup = doc.getSections().get(0).getPageSetup(); pageSetup.getBorders().setLineStyle(LineStyle.DOUBLE_WAVE); pageSetup.getBorders().setLineWidth(2.0); pageSetup.getBorders().setColor(Color.GREEN); pageSetup.getBorders().setDistanceFromText(24.0); pageSetup.getBorders().setShadow(true); doc.save(getArtifactsDir() + "PageSetup.PageBorders.docx");
public int getCount()
Example:
Shows how border collections can share elements.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("Paragraph 1."); builder.write("Paragraph 2."); // Since both paragraphs were created with the same border configuration, // the border collections of the two paragraphs share the same elements. BorderCollection firstParagraphBorders = doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().getBorders(); BorderCollection secondParagraphBorders = builder.getCurrentParagraph().getParagraphFormat().getBorders(); for (int i = 0; i < firstParagraphBorders.getCount(); i++) { Assert.assertTrue(firstParagraphBorders.get(i).equals(secondParagraphBorders.get(i))); Assert.assertEquals(firstParagraphBorders.get(i).hashCode(), secondParagraphBorders.get(i).hashCode()); Assert.assertFalse(firstParagraphBorders.get(i).isVisible()); } for (Border border : secondParagraphBorders) border.setLineStyle(LineStyle.DOT_DASH); // After changing the line style of the borders in just the second paragraph, // the border collections no longer share the same elements. for (int i = 0; i < firstParagraphBorders.getCount(); i++) { Assert.assertFalse(firstParagraphBorders.get(i).equals(secondParagraphBorders.get(i))); Assert.assertNotEquals(firstParagraphBorders.get(i).hashCode(), secondParagraphBorders.get(i).hashCode()); // Changing the appearance of an empty border makes it visible. Assert.assertTrue(secondParagraphBorders.get(i).isVisible()); } doc.save(getArtifactsDir() + "Border.SharedElements.docx");
public double getDistanceFromText() / public void setDistanceFromText(double value)
Gets the distance from text for the first border.
Sets the distance from text for all borders in the collection excluding diagonal borders.
Has no effect and will be automatically reset to zero for borders of table cells.
Example:
Shows how to create green wavy page border with a shadow.Document doc = new Document(); PageSetup pageSetup = doc.getSections().get(0).getPageSetup(); pageSetup.getBorders().setLineStyle(LineStyle.DOUBLE_WAVE); pageSetup.getBorders().setLineWidth(2.0); pageSetup.getBorders().setColor(Color.GREEN); pageSetup.getBorders().setDistanceFromText(24.0); pageSetup.getBorders().setShadow(true); doc.save(getArtifactsDir() + "PageSetup.PageBorders.docx");
public Border getHorizontal()
Example:
Shows how to apply settings to horizontal borders to a paragraph's format.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Create a red horizontal border for the paragraph. Any paragraphs created afterwards will inherit these border settings. BorderCollection borders = doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().getBorders(); borders.getHorizontal().setColor(Color.RED); borders.getHorizontal().setLineStyle(LineStyle.DASH_SMALL_GAP); borders.getHorizontal().setLineWidth(3.0); // Write text to the document without creating a new paragraph afterward. // Since there is no paragraph underneath, the horizontal border will not be visible. builder.write("Paragraph above horizontal border."); // Once we add a second paragraph, the border of the first paragraph will become visible. builder.insertParagraph(); builder.write("Paragraph below horizontal border."); doc.save(getArtifactsDir() + "Border.HorizontalBorders.docx");
Example:
Shows how to apply settings to vertical borders to a table row's format.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Create a table with red and blue inner borders. Table table = builder.startTable(); for (int i = 0; i < 3; i++) { builder.insertCell(); builder.write(MessageFormat.format("Row {0}, Column 1", i + 1)); builder.insertCell(); builder.write(MessageFormat.format("Row {0}, Column 2", i + 1)); Row row = builder.endRow(); BorderCollection borders = row.getRowFormat().getBorders(); // Adjust the appearance of borders that will appear between rows. borders.getHorizontal().setColor(Color.RED); borders.getHorizontal().setLineStyle(LineStyle.DOT); borders.getHorizontal().setLineWidth(2.0d); // Adjust the appearance of borders that will appear between cells. borders.getVertical().setColor(Color.BLUE); borders.getVertical().setLineStyle(LineStyle.DOT); borders.getVertical().setLineWidth(2.0d); } // A row format's border settings are separate from those of the cell paragraph. Border border = table.getFirstRow().getFirstCell().getLastParagraph().getParagraphFormat().getBorders().getVertical(); Assert.assertEquals(0, border.getColor().getRGB()); Assert.assertEquals(0.0d, border.getLineWidth()); Assert.assertEquals(LineStyle.NONE, border.getLineStyle()); doc.save(getArtifactsDir() + "Border.VerticalBorders.docx");
public Border getLeft()
Example:
Shows how to apply border and shading color while building a table.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Start a table, and set a default color/thickness for its borders. Table table = builder.startTable(); table.setBorders(LineStyle.SINGLE, 2.0, Color.BLACK); // Create a row with two cells with different background colors. builder.insertCell(); builder.getCellFormat().getShading().setBackgroundPatternColor(Color.RED); builder.writeln("Row 1, Cell 1."); builder.insertCell(); builder.getCellFormat().getShading().setBackgroundPatternColor(Color.GREEN); builder.writeln("Row 1, Cell 2."); builder.endRow(); // Reset cell formatting to disable the background colors // set a custom border thickness for all new cells created by the builder, // then build a second row. builder.getCellFormat().clearFormatting(); builder.getCellFormat().getBorders().getLeft().setLineWidth(4.0); builder.getCellFormat().getBorders().getRight().setLineWidth(4.0); builder.getCellFormat().getBorders().getTop().setLineWidth(4.0); builder.getCellFormat().getBorders().getBottom().setLineWidth(4.0); builder.insertCell(); builder.writeln("Row 2, Cell 1."); builder.insertCell(); builder.writeln("Row 2, Cell 2."); doc.save(getArtifactsDir() + "DocumentBuilder.TableBordersAndShading.docx");
public int getLineStyle() / public void setLineStyle(int value)
Returns the style of the first border in the collection.
Sets the style of all borders in the collection excluding diagonal borders.
Example:
Shows how to create green wavy page border with a shadow.Document doc = new Document(); PageSetup pageSetup = doc.getSections().get(0).getPageSetup(); pageSetup.getBorders().setLineStyle(LineStyle.DOUBLE_WAVE); pageSetup.getBorders().setLineWidth(2.0); pageSetup.getBorders().setColor(Color.GREEN); pageSetup.getBorders().setDistanceFromText(24.0); pageSetup.getBorders().setShadow(true); doc.save(getArtifactsDir() + "PageSetup.PageBorders.docx");
public double getLineWidth() / public void setLineWidth(double value)
Returns the width of the first border in the collection.
Sets the width of all borders in the collection excluding diagonal borders.
Example:
Shows how to create green wavy page border with a shadow.Document doc = new Document(); PageSetup pageSetup = doc.getSections().get(0).getPageSetup(); pageSetup.getBorders().setLineStyle(LineStyle.DOUBLE_WAVE); pageSetup.getBorders().setLineWidth(2.0); pageSetup.getBorders().setColor(Color.GREEN); pageSetup.getBorders().setDistanceFromText(24.0); pageSetup.getBorders().setShadow(true); doc.save(getArtifactsDir() + "PageSetup.PageBorders.docx");
public Border getRight()
Example:
Shows how to apply border and shading color while building a table.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Start a table, and set a default color/thickness for its borders. Table table = builder.startTable(); table.setBorders(LineStyle.SINGLE, 2.0, Color.BLACK); // Create a row with two cells with different background colors. builder.insertCell(); builder.getCellFormat().getShading().setBackgroundPatternColor(Color.RED); builder.writeln("Row 1, Cell 1."); builder.insertCell(); builder.getCellFormat().getShading().setBackgroundPatternColor(Color.GREEN); builder.writeln("Row 1, Cell 2."); builder.endRow(); // Reset cell formatting to disable the background colors // set a custom border thickness for all new cells created by the builder, // then build a second row. builder.getCellFormat().clearFormatting(); builder.getCellFormat().getBorders().getLeft().setLineWidth(4.0); builder.getCellFormat().getBorders().getRight().setLineWidth(4.0); builder.getCellFormat().getBorders().getTop().setLineWidth(4.0); builder.getCellFormat().getBorders().getBottom().setLineWidth(4.0); builder.insertCell(); builder.writeln("Row 2, Cell 1."); builder.insertCell(); builder.writeln("Row 2, Cell 2."); doc.save(getArtifactsDir() + "DocumentBuilder.TableBordersAndShading.docx");
public boolean getShadow() / public void setShadow(boolean value)
Gets the value from the first border in the collection.
Sets the value for all borders in the collection excluding diagonal borders.
Example:
Shows how to create green wavy page border with a shadow.Document doc = new Document(); PageSetup pageSetup = doc.getSections().get(0).getPageSetup(); pageSetup.getBorders().setLineStyle(LineStyle.DOUBLE_WAVE); pageSetup.getBorders().setLineWidth(2.0); pageSetup.getBorders().setColor(Color.GREEN); pageSetup.getBorders().setDistanceFromText(24.0); pageSetup.getBorders().setShadow(true); doc.save(getArtifactsDir() + "PageSetup.PageBorders.docx");
public Border getTop()
Example:
Shows how to apply border and shading color while building a table.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Start a table, and set a default color/thickness for its borders. Table table = builder.startTable(); table.setBorders(LineStyle.SINGLE, 2.0, Color.BLACK); // Create a row with two cells with different background colors. builder.insertCell(); builder.getCellFormat().getShading().setBackgroundPatternColor(Color.RED); builder.writeln("Row 1, Cell 1."); builder.insertCell(); builder.getCellFormat().getShading().setBackgroundPatternColor(Color.GREEN); builder.writeln("Row 1, Cell 2."); builder.endRow(); // Reset cell formatting to disable the background colors // set a custom border thickness for all new cells created by the builder, // then build a second row. builder.getCellFormat().clearFormatting(); builder.getCellFormat().getBorders().getLeft().setLineWidth(4.0); builder.getCellFormat().getBorders().getRight().setLineWidth(4.0); builder.getCellFormat().getBorders().getTop().setLineWidth(4.0); builder.getCellFormat().getBorders().getBottom().setLineWidth(4.0); builder.insertCell(); builder.writeln("Row 2, Cell 1."); builder.insertCell(); builder.writeln("Row 2, Cell 2."); doc.save(getArtifactsDir() + "DocumentBuilder.TableBordersAndShading.docx");
public Border getVertical()
Example:
Shows how to apply settings to vertical borders to a table row's format.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Create a table with red and blue inner borders. Table table = builder.startTable(); for (int i = 0; i < 3; i++) { builder.insertCell(); builder.write(MessageFormat.format("Row {0}, Column 1", i + 1)); builder.insertCell(); builder.write(MessageFormat.format("Row {0}, Column 2", i + 1)); Row row = builder.endRow(); BorderCollection borders = row.getRowFormat().getBorders(); // Adjust the appearance of borders that will appear between rows. borders.getHorizontal().setColor(Color.RED); borders.getHorizontal().setLineStyle(LineStyle.DOT); borders.getHorizontal().setLineWidth(2.0d); // Adjust the appearance of borders that will appear between cells. borders.getVertical().setColor(Color.BLUE); borders.getVertical().setLineStyle(LineStyle.DOT); borders.getVertical().setLineWidth(2.0d); } // A row format's border settings are separate from those of the cell paragraph. Border border = table.getFirstRow().getFirstCell().getLastParagraph().getParagraphFormat().getBorders().getVertical(); Assert.assertEquals(0, border.getColor().getRGB()); Assert.assertEquals(0.0d, border.getLineWidth()); Assert.assertEquals(LineStyle.NONE, border.getLineStyle()); doc.save(getArtifactsDir() + "Border.VerticalBorders.docx");
public Border get(int index)
index
- Zero-based index of the border to retrieve.Example:
Shows how border collections can share elements.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("Paragraph 1."); builder.write("Paragraph 2."); // Since both paragraphs were created with the same border configuration, // the border collections of the two paragraphs share the same elements. BorderCollection firstParagraphBorders = doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().getBorders(); BorderCollection secondParagraphBorders = builder.getCurrentParagraph().getParagraphFormat().getBorders(); for (int i = 0; i < firstParagraphBorders.getCount(); i++) { Assert.assertTrue(firstParagraphBorders.get(i).equals(secondParagraphBorders.get(i))); Assert.assertEquals(firstParagraphBorders.get(i).hashCode(), secondParagraphBorders.get(i).hashCode()); Assert.assertFalse(firstParagraphBorders.get(i).isVisible()); } for (Border border : secondParagraphBorders) border.setLineStyle(LineStyle.DOT_DASH); // After changing the line style of the borders in just the second paragraph, // the border collections no longer share the same elements. for (int i = 0; i < firstParagraphBorders.getCount(); i++) { Assert.assertFalse(firstParagraphBorders.get(i).equals(secondParagraphBorders.get(i))); Assert.assertNotEquals(firstParagraphBorders.get(i).hashCode(), secondParagraphBorders.get(i).hashCode()); // Changing the appearance of an empty border makes it visible. Assert.assertTrue(secondParagraphBorders.get(i).isVisible()); } doc.save(getArtifactsDir() + "Border.SharedElements.docx");
public Border getByBorderType(int borderType)
Note that not all borders are present for different document elements. This method throws an exception if you request a border not applicable to the current object.
borderType
- A 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 void clearFormatting()
Example:
Shows how to remove all borders from all paragraphs in a document.Document doc = new Document(getMyDir() + "Borders.docx"); // The first paragraph of this document has visible borders with these settings. BorderCollection firstParagraphBorders = doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().getBorders(); Assert.assertEquals(Color.RED.getRGB(), firstParagraphBorders.getColor().getRGB()); Assert.assertEquals(LineStyle.SINGLE, firstParagraphBorders.getLineStyle()); Assert.assertEquals(3.0d, firstParagraphBorders.getLineWidth()); // Apply the ClearFormatting method to each paragraph to remove all of its borders. for (Paragraph paragraph : (Iterable<Paragraph>) doc.getFirstSection().getBody().getParagraphs()) { paragraph.getParagraphFormat().getBorders().clearFormatting(); for (Border border : paragraph.getParagraphFormat().getBorders()) { Assert.assertEquals(0, border.getColor().getRGB()); Assert.assertEquals(LineStyle.NONE, border.getLineStyle()); Assert.assertEquals(0.0d, border.getLineWidth()); } } doc.save(getArtifactsDir() + "BorderCollection.RemoveAllBorders.docx");
public boolean equals(BorderCollection brColl) throws java.lang.Exception
Example:
Shows how border collections can share elements.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("Paragraph 1."); builder.write("Paragraph 2."); // Since both paragraphs were created with the same border configuration, // the border collections of the two paragraphs share the same elements. BorderCollection firstParagraphBorders = doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().getBorders(); BorderCollection secondParagraphBorders = builder.getCurrentParagraph().getParagraphFormat().getBorders(); for (int i = 0; i < firstParagraphBorders.getCount(); i++) { Assert.assertTrue(firstParagraphBorders.get(i).equals(secondParagraphBorders.get(i))); Assert.assertEquals(firstParagraphBorders.get(i).hashCode(), secondParagraphBorders.get(i).hashCode()); Assert.assertFalse(firstParagraphBorders.get(i).isVisible()); } for (Border border : secondParagraphBorders) border.setLineStyle(LineStyle.DOT_DASH); // After changing the line style of the borders in just the second paragraph, // the border collections no longer share the same elements. for (int i = 0; i < firstParagraphBorders.getCount(); i++) { Assert.assertFalse(firstParagraphBorders.get(i).equals(secondParagraphBorders.get(i))); Assert.assertNotEquals(firstParagraphBorders.get(i).hashCode(), secondParagraphBorders.get(i).hashCode()); // Changing the appearance of an empty border makes it visible. Assert.assertTrue(secondParagraphBorders.get(i).isVisible()); } doc.save(getArtifactsDir() + "Border.SharedElements.docx");
public java.util.Iterator<Border> iterator()
Example:
Shows how to iterate over and edit all of the borders in a paragraph format object.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Configure the builder's paragraph format settings to create a green wave border on all sides. BorderCollection borders = builder.getParagraphFormat().getBorders(); Iterator<Border> enumerator = borders.iterator(); while (enumerator.hasNext()) { Border border = enumerator.next(); border.setColor(Color.green); border.setLineStyle(LineStyle.WAVE); border.setLineWidth(3.0); } // Insert a paragraph. Our border settings will determine the appearance of its border. builder.writeln("Hello world!"); doc.save(getArtifactsDir() + "BorderCollection.GetBordersEnumerator.docx");