In Microsoft Word, borders can be used to make documents more attractive and beautiful. You can add them to specific text of a paragraph, a whole paragraph or a page. This article will introduce how to add borders to specific text or a whole paragraph using Java codes.
DEPENDENCY
First of all, you’re required to add Spire.Doc.jar to your Java program. You can get it from the link, or create a Maven project, and import the following codes to the pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url> https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
USING THE CODE
Free Spire.Doc for Java supports not only adding a border to a specific text of a paragraph, but also appending a border to a whole paragraph. The following are detailed steps for reference.
l Create
a Document instance and a new Word
document.
l Add a
section to the document using Document.addSection()
method.
l Add a
paragraph to the section using Section.addParagraph()
method.
l Append
text to the paragraph, add border to the text and set style and color of the
border.
l Append
other text to the same paragraph using Paragraph.appendText()
method.
l Append
break to the end of the paragraph using Paragraph.appedBreak()
method.
l Add
another paragraph to the document using Section.addParagraph()
method.
l Append
text to the paragraph, add border to it and set style and color of the border.
l Save
the document to another file using Document.saveToFile()
method.
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.BorderStyle;
import com.spire.doc.documents.BreakType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class AddBorder {
public static void main(String[] args) {
//Create a Document instance
Document doc = new Document();
//Add a section
Section section = doc.addSection();
//Append text and add a border to it
Paragraph para = section.addParagraph();
TextRange tr = para.appendText("Java");
tr.getCharacterFormat().getBorder().setBorderType(BorderStyle.Single);
tr.getCharacterFormat().getBorder().setColor(Color.RED);
String text = " is a popular programming language, created in 1995. " +
"It is owned by Oracle, and more than 3 billion devices run Java.";
para.appendText(text);
para.appendBreak(BreakType.Line_Break);
//Add a border to a paragraph
para = section.addParagraph();
String text2 = "Java is an object oriented language which gives a clear structure to programs " +
"and allows code to be reused, lowering development costs" ;
para.appendText(text2);
para.getFormat().getBorders().setBorderType(BorderStyle.Single);
para.getFormat().getBorders().setColor(Color.RED);
//Save the document
doc.saveToFile("output/AddBorder.docx", FileFormat.Docx_2013);
}
}
No comments:
Post a Comment