Page borders can be a useful design element in Microsoft Word documents. They can help to frame the content and provide a polished, professional. Page borders draw the reader's eye to the main content area and create a sense of structure and cohesion. Conversely, you may want to remove page borders if they are not needed or if they distract from the content.
In this article, you will learn how to add, adjust, and remove page borders in a Word document using Java and Spire.Doc for Java.
- Add Page Borders to a Word Document
- Adjust Page Borders in a Word Document
- Remove Page Borders from a Word Document
Install Spire.Doc for Java
First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's 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</artifactId> <version>12.11.0</version> </dependency> </dependencies>
Add Page Borders to a Word Document in Java
Spire.Doc for Java includes the Borders class, which enables developers to manage the page borders in a Word document. This class provides a collection of methods that allow you to control various aspects of the page border, such as the border type, color, and line width.
To add borders to all pages in a Word document using Java, the general steps are as follows:
- Create a Document object.
- Load a Word file from the given file path.
- Iterate through the sections in the document.
- Get a specific section.
- Get the PageSetup object of the section.
- Apply borders to all page by passing PageBordersApplyType.All_Pages as the parameter of PageSetup.setPageBordersApplyType() method.
- Get the Borders object using PageSetup.getBorders() method.
- Set the border type, color, line width and other attributes using the methods under the Borders object.
- Save the updated document to a different Word file.
- Java
import com.spire.doc.*; import com.spire.doc.documents.BorderStyle; import java.awt.*; public class AddPageBorder { public static void main(String[] args) { // Create a Document object Document doc = new Document(); // Load a Word file doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.docx"); // Iterate through the sections in the document for (int i = 0; i < doc.getSections().getCount(); i++) { // Get a specific section Section section = doc.getSections().get(i); // Get page setup object PageSetup pageSetup = section.getPageSetup(); // Apply page border to all pages pageSetup.setPageBordersApplyType(PageBordersApplyType.All_Pages); // Set the border type pageSetup.getBorders().setBorderType(BorderStyle.Dash_Large_Gap); // Set the border width pageSetup.getBorders().setLineWidth(2); // Set the border color pageSetup.getBorders().setColor(Color.RED); // Set the spacing between borders and text within them pageSetup.getBorders().getTop().setSpace(30); pageSetup.getBorders().getBottom().setSpace(30); pageSetup.getBorders().getLeft().setSpace(30); pageSetup.getBorders().getRight().setSpace(30); } // Save the updated document to a different file doc.saveToFile("AddPageBorder.docx", FileFormat.Docx); // Dispose resources doc.dispose(); } }
Adjust Page Borders in a Word Document in Java
The page borders of an existing Word document can be obtained using the PageSetup.getBorders() method. You can change the appearance of the page borders using the setBorderType(), setColor(), and setLineWidth() methods.
The steps to adjust page borders in a Word document using Java are as follows.
- Create a Document object.
- Load a Word file from the given file path.
- Iterate through the sections in the document.
- Get a specific section.
- Get the PageSetup object of the section.
- Get the Borders object using PageSetup.getBorders() method.
- Set the border type, color, line width and other attributes using the methods under the Borders object.
- Save the updated document to a different Word file.
- Java
import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.PageSetup; import com.spire.doc.Section; import com.spire.doc.documents.BorderStyle; import java.awt.*; public class AdjustPageBorders { public static void main(String[] args) { // Create a Document object Document doc = new Document(); // Load a Word file doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Borders.docx"); // Iterate through the sections in the document for (int i = 0; i < doc.getSections().getCount(); i++) { // Get a specific section Section section = doc.getSections().get(i); // Get page setup of the section PageSetup pageSetup = section.getPageSetup(); // Change the border type section.getPageSetup().getBorders().setBorderType(BorderStyle.Double); // Change the border color section.getPageSetup().getBorders().setColor(Color.DARK_GRAY); // Change the border width section.getPageSetup().getBorders().setLineWidth(3); } // Save the updated document to a different file doc.saveToFile("AdjustBorder.docx", FileFormat.Docx); // Dispose resources doc.dispose(); } }
Remove Page Borders from a Word Document in Java
To move page borders from a Word document, pass the BorderStyle.None as the parameter of the Borders.setBorderType() method. By setting the border type as none, you are instructing the document to remove any existing page borders, resulting in a clean, border-free document layout.
The steps to remove page borders from a Word document using Java are as follows:
- Create a Document object.
- Load a Word file from the given file path.
- Iterate through the sections in the document.
- Get a specific section.
- Get the PageSetup object of the section.
- Get the Borders object using PageSetup.getBorders() method.
- Set the border type as BorderStyle.None using Borders.setBorderType() method.
- Save the updated document to a different Word file.
- Java
import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.PageSetup; import com.spire.doc.Section; import com.spire.doc.documents.BorderStyle; public class RemovePageBorders { public static void main(String[] args) { // Create a Document object Document doc = new Document(); // Load a Word file doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Borders.docx"); // Iterate through the sections in the document for (int i = 0; i < doc.getSections().getCount(); i++) { // Get a specific section Section section = doc.getSections().get(i); // Get page setup object PageSetup pageSetup = section.getPageSetup(); // Set the border type to none pageSetup.getBorders().setBorderType(BorderStyle.None); } // Save the updated document to a different file doc.saveToFile("RemovePageBorders.docx", FileFormat.Docx); // Dispose resources doc.dispose(); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.