com.aspose.words

Class Border

Represents a border of an object.

Borders can be applied to various document elements including paragraph, run of text inside a paragraph or a table cell.

Example:

Shows how to insert a string surrounded by a border into a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.getFont().getBorder().setColor(Color.GREEN);
builder.getFont().getBorder().setLineWidth(2.5);
builder.getFont().getBorder().setLineStyle(LineStyle.DASH_DOT_STROKER);

builder.write("Text surrounded by green border.");

doc.save(getArtifactsDir() + "Border.FontBorder.docx");

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
java.awt.ColorgetColor()
void
setColor(java.awt.Colorvalue)
           Gets or sets the border color.
doublegetDistanceFromText()
void
setDistanceFromText(doublevalue)
           Gets or sets distance of the border from text or from the page edge in points.
booleanisVisible()
Returns true if the LineStyle is not LineStyle.None.
intgetLineStyle()
void
setLineStyle(intvalue)
           Gets or sets the border style. The value of the property is LineStyle integer constant.
doublegetLineWidth()
void
setLineWidth(doublevalue)
           Gets or sets the border width in points.
booleangetShadow()
void
setShadow(booleanvalue)
           Gets or sets a value indicating whether the border has a shadow.
 
Method Summary
voidclearFormatting()
Resets border properties to default values.
booleanequals(Border rhs)
Determines whether the specified border is equal in value to the current border.
booleanequals(java.lang.Object obj)
Determines whether the specified object is equal in value to the current object.
inthashCode()
Serves as a hash function for this type.
 

    • Property Getters/Setters Detail

      • getColor/setColor

        public java.awt.Color getColor() / public void setColor(java.awt.Color value)
        
        Gets or sets the border color.

        Example:

        Shows how to insert a string surrounded by a border into a document.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        builder.getFont().getBorder().setColor(Color.GREEN);
        builder.getFont().getBorder().setLineWidth(2.5);
        builder.getFont().getBorder().setLineStyle(LineStyle.DASH_DOT_STROKER);
        
        builder.write("Text surrounded by green border.");
        
        doc.save(getArtifactsDir() + "Border.FontBorder.docx");
      • getDistanceFromText/setDistanceFromText

        public double getDistanceFromText() / public void setDistanceFromText(double value)
        
        Gets or sets distance of the border from text or from the page edge in points. Has no effect and will be automatically reset to zero for borders of table cells.

        Example:

        Shows how to create a page border that looks like a wide blue band at the top of the first page only.
        Document doc = new Document();
        
        PageSetup pageSetup = doc.getSections().get(0).getPageSetup();
        pageSetup.setBorderAlwaysInFront(false);
        pageSetup.setBorderDistanceFrom(PageBorderDistanceFrom.PAGE_EDGE);
        pageSetup.setBorderAppliesTo(PageBorderAppliesTo.FIRST_PAGE);
        
        Border border = pageSetup.getBorders().getByBorderType(BorderType.TOP);
        border.setLineStyle(LineStyle.SINGLE);
        border.setLineWidth(30.0);
        border.setColor(Color.BLUE);
        border.setDistanceFromText(0.0);
        
        doc.save(getArtifactsDir() + "PageSetup.PageBorderProperties.docx");
        See Also:
        PageSetup.BorderDistanceFrom
      • isVisible

        public boolean isVisible()
        
        Returns true if the LineStyle is not LineStyle.None.

        Example:

        Shows the equality of BorderCollections as well counting, visibility of their elements.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        builder.getCurrentParagraph().appendChild(new Run(doc, "Paragraph 1."));
        
        Paragraph firstParagraph = doc.getFirstSection().getBody().getFirstParagraph();
        BorderCollection firstParaBorders = firstParagraph.getParagraphFormat().getBorders();
        
        builder.insertParagraph();
        builder.getCurrentParagraph().appendChild(new Run(doc, "Paragraph 2."));
        
        Paragraph secondParagraph = builder.getCurrentParagraph();
        BorderCollection secondParaBorders = secondParagraph.getParagraphFormat().getBorders();
        
        // Two paragraphs have two different BorderCollections, but share the elements that are in from the first paragraph
        for (int i = 0; i < firstParaBorders.getCount(); i++) {
            Assert.assertTrue(firstParaBorders.get(i).equals(secondParaBorders.get(i)));
            Assert.assertEquals(firstParaBorders.get(i).hashCode(), secondParaBorders.get(i).hashCode());
        
            // Borders are invisible by default
            Assert.assertFalse(firstParaBorders.get(i).isVisible());
        }
        
        // Each border in the second paragraph collection becomes no longer the same as its counterpart from the first paragraph collection
        // Change all the elements in the second collection to make it completely different from the first
        Assert.assertEquals(6, secondParaBorders.getCount()); // ExSkip
        for (Border border : secondParaBorders) {
            border.setLineStyle(LineStyle.DOT_DASH);
        }
        
        // Now the BorderCollections both have their own elements
        for (int i = 0; i < firstParaBorders.getCount(); i++) {
            Assert.assertFalse(firstParaBorders.get(i).equals(secondParaBorders.get(i)));
            Assert.assertNotEquals(firstParaBorders.get(i).hashCode(), secondParaBorders.get(i).hashCode());
        
            // Changing the line style made the borders visible
            Assert.assertTrue(secondParaBorders.get(i).isVisible());
        }
        
        doc.save(getArtifactsDir() + "Border.EqualityCountingAndVisibility.docx");
      • getLineStyle/setLineStyle

        public int getLineStyle() / public void setLineStyle(int value)
        
        Gets or sets the border style. The value of the property is LineStyle integer constant.

        If you set line style to none, then line width is automatically changed to zero.

        Example:

        Shows how to insert a string surrounded by a border into a document.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        builder.getFont().getBorder().setColor(Color.GREEN);
        builder.getFont().getBorder().setLineWidth(2.5);
        builder.getFont().getBorder().setLineStyle(LineStyle.DASH_DOT_STROKER);
        
        builder.write("Text surrounded by green border.");
        
        doc.save(getArtifactsDir() + "Border.FontBorder.docx");
      • getLineWidth/setLineWidth

        public double getLineWidth() / public void setLineWidth(double value)
        
        Gets or sets the border width in points.

        If you set line width greater than zero when line style is none, the line style is automatically changed to single line.

        Example:

        Shows how to insert a string surrounded by a border into a document.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        builder.getFont().getBorder().setColor(Color.GREEN);
        builder.getFont().getBorder().setLineWidth(2.5);
        builder.getFont().getBorder().setLineStyle(LineStyle.DASH_DOT_STROKER);
        
        builder.write("Text surrounded by green border.");
        
        doc.save(getArtifactsDir() + "Border.FontBorder.docx");
      • getShadow/setShadow

        public boolean getShadow() / public void setShadow(boolean value)
        
        Gets or sets a value indicating whether the border has a shadow.

        In Microsoft Word, for a border to have a shadow, the borders on all four sides (left, top, right and bottom) should be of the same type, width, color and all should have the Shadow property set to true.

        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");
    • Method Detail

      • clearFormatting

        public void clearFormatting()
        Resets border properties to default values. When border properties are reset to default values, the border is invisible.

        Example:

        Shows how to remove borders from a paragraph.
        Document doc = new Document(getMyDir() + "Borders.docx");
        
        // Get the first paragraph's collection of borders
        DocumentBuilder builder = new DocumentBuilder(doc);
        BorderCollection borders = builder.getParagraphFormat().getBorders();
        Assert.assertEquals(3.0d, borders.get(0).getLineWidth()); // ExSkip
        Assert.assertEquals(LineStyle.SINGLE, borders.get(0).getLineStyle()); // ExSkip
        
        for (Border border : borders) {
            border.clearFormatting();
        }
        
        builder.getCurrentParagraph().getRuns().get(0).setText("Paragraph with no border");
        
        doc.save(getArtifactsDir() + "Border.ClearFormatting.docx");
      • equals

        public boolean equals(Border rhs)
        Determines whether the specified border is equal in value to the current border.

        Example:

        Shows the equality of BorderCollections as well counting, visibility of their elements.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        builder.getCurrentParagraph().appendChild(new Run(doc, "Paragraph 1."));
        
        Paragraph firstParagraph = doc.getFirstSection().getBody().getFirstParagraph();
        BorderCollection firstParaBorders = firstParagraph.getParagraphFormat().getBorders();
        
        builder.insertParagraph();
        builder.getCurrentParagraph().appendChild(new Run(doc, "Paragraph 2."));
        
        Paragraph secondParagraph = builder.getCurrentParagraph();
        BorderCollection secondParaBorders = secondParagraph.getParagraphFormat().getBorders();
        
        // Two paragraphs have two different BorderCollections, but share the elements that are in from the first paragraph
        for (int i = 0; i < firstParaBorders.getCount(); i++) {
            Assert.assertTrue(firstParaBorders.get(i).equals(secondParaBorders.get(i)));
            Assert.assertEquals(firstParaBorders.get(i).hashCode(), secondParaBorders.get(i).hashCode());
        
            // Borders are invisible by default
            Assert.assertFalse(firstParaBorders.get(i).isVisible());
        }
        
        // Each border in the second paragraph collection becomes no longer the same as its counterpart from the first paragraph collection
        // Change all the elements in the second collection to make it completely different from the first
        Assert.assertEquals(6, secondParaBorders.getCount()); // ExSkip
        for (Border border : secondParaBorders) {
            border.setLineStyle(LineStyle.DOT_DASH);
        }
        
        // Now the BorderCollections both have their own elements
        for (int i = 0; i < firstParaBorders.getCount(); i++) {
            Assert.assertFalse(firstParaBorders.get(i).equals(secondParaBorders.get(i)));
            Assert.assertNotEquals(firstParaBorders.get(i).hashCode(), secondParaBorders.get(i).hashCode());
        
            // Changing the line style made the borders visible
            Assert.assertTrue(secondParaBorders.get(i).isVisible());
        }
        
        doc.save(getArtifactsDir() + "Border.EqualityCountingAndVisibility.docx");
      • equals

        public boolean equals(java.lang.Object obj)
        Determines whether the specified object is equal in value to the current object.

        Example:

        Shows the equality of BorderCollections as well counting, visibility of their elements.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        builder.getCurrentParagraph().appendChild(new Run(doc, "Paragraph 1."));
        
        Paragraph firstParagraph = doc.getFirstSection().getBody().getFirstParagraph();
        BorderCollection firstParaBorders = firstParagraph.getParagraphFormat().getBorders();
        
        builder.insertParagraph();
        builder.getCurrentParagraph().appendChild(new Run(doc, "Paragraph 2."));
        
        Paragraph secondParagraph = builder.getCurrentParagraph();
        BorderCollection secondParaBorders = secondParagraph.getParagraphFormat().getBorders();
        
        // Two paragraphs have two different BorderCollections, but share the elements that are in from the first paragraph
        for (int i = 0; i < firstParaBorders.getCount(); i++) {
            Assert.assertTrue(firstParaBorders.get(i).equals(secondParaBorders.get(i)));
            Assert.assertEquals(firstParaBorders.get(i).hashCode(), secondParaBorders.get(i).hashCode());
        
            // Borders are invisible by default
            Assert.assertFalse(firstParaBorders.get(i).isVisible());
        }
        
        // Each border in the second paragraph collection becomes no longer the same as its counterpart from the first paragraph collection
        // Change all the elements in the second collection to make it completely different from the first
        Assert.assertEquals(6, secondParaBorders.getCount()); // ExSkip
        for (Border border : secondParaBorders) {
            border.setLineStyle(LineStyle.DOT_DASH);
        }
        
        // Now the BorderCollections both have their own elements
        for (int i = 0; i < firstParaBorders.getCount(); i++) {
            Assert.assertFalse(firstParaBorders.get(i).equals(secondParaBorders.get(i)));
            Assert.assertNotEquals(firstParaBorders.get(i).hashCode(), secondParaBorders.get(i).hashCode());
        
            // Changing the line style made the borders visible
            Assert.assertTrue(secondParaBorders.get(i).isVisible());
        }
        
        doc.save(getArtifactsDir() + "Border.EqualityCountingAndVisibility.docx");
      • hashCode

        public int hashCode()
        Serves as a hash function for this type.

        Example:

        Shows the equality of BorderCollections as well counting, visibility of their elements.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        builder.getCurrentParagraph().appendChild(new Run(doc, "Paragraph 1."));
        
        Paragraph firstParagraph = doc.getFirstSection().getBody().getFirstParagraph();
        BorderCollection firstParaBorders = firstParagraph.getParagraphFormat().getBorders();
        
        builder.insertParagraph();
        builder.getCurrentParagraph().appendChild(new Run(doc, "Paragraph 2."));
        
        Paragraph secondParagraph = builder.getCurrentParagraph();
        BorderCollection secondParaBorders = secondParagraph.getParagraphFormat().getBorders();
        
        // Two paragraphs have two different BorderCollections, but share the elements that are in from the first paragraph
        for (int i = 0; i < firstParaBorders.getCount(); i++) {
            Assert.assertTrue(firstParaBorders.get(i).equals(secondParaBorders.get(i)));
            Assert.assertEquals(firstParaBorders.get(i).hashCode(), secondParaBorders.get(i).hashCode());
        
            // Borders are invisible by default
            Assert.assertFalse(firstParaBorders.get(i).isVisible());
        }
        
        // Each border in the second paragraph collection becomes no longer the same as its counterpart from the first paragraph collection
        // Change all the elements in the second collection to make it completely different from the first
        Assert.assertEquals(6, secondParaBorders.getCount()); // ExSkip
        for (Border border : secondParaBorders) {
            border.setLineStyle(LineStyle.DOT_DASH);
        }
        
        // Now the BorderCollections both have their own elements
        for (int i = 0; i < firstParaBorders.getCount(); i++) {
            Assert.assertFalse(firstParaBorders.get(i).equals(secondParaBorders.get(i)));
            Assert.assertNotEquals(firstParaBorders.get(i).hashCode(), secondParaBorders.get(i).hashCode());
        
            // Changing the line style made the borders visible
            Assert.assertTrue(secondParaBorders.get(i).isVisible());
        }
        
        doc.save(getArtifactsDir() + "Border.EqualityCountingAndVisibility.docx");