How to Set Page Margins for Word Documents via Java

The margin is the distance between the edge of the page and the text. In the printable area, you can usually insert text and graphics, and you can also insert certain items in the margin area (such as headers, footers, page numbers, etc.). There will be a unified standard format for page margins in the Word document we use. For page margins, the standard is 2.54CM on top and bottom, and 2.8CM on the left and right. The margins can also be changed according to your own needs. The purpose of today's article is to show you how to programmatically set page margins for Word Documents. For your reference, I have attached Java code with the ideas and methods I have sorted out.

First, 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.

Method 1:

Introduced manually. Download Free Spire.Doc for Java locally, unzip it, and find the Spire.Doc.jar file in the lib folder. Open the following interface in IDEA, and import the jar file in the local path into the Java program:

Method 2:

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>

            <url>https://repo.e-iceblue.com/repository/maven-public/</url>

        </repository>

    </repositories>

<dependencies>

    <dependency>

        <groupId>e-iceblue</groupId>

        <artifactId>spire.doc.free</artifactId>

        <version>5.2.0</version>

    </dependency>

</dependencies>

Set Page Margins in Word in Java

  • The following are the steps to set page margins in a Word document:
  • Initialize an instance of Document class.
  • Load a Word document using Document.loadFromFile() method.
  • Get the desired section through Document.getSections().get(sectionIndex) method.
  • Set the top, bottom, left and right margins for the pages in the section through Section.getPageSetup().getMargins().setTop(), Section. getPageSetup().getMargins().setBottom(), Section. getPageSetup().getMargins().setLeft(), Section.getPageSetup().getMargins().setRight() methods.
  • Save the result document using Document.saveToFile() method.

Full Code

Java

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;

public class SetPageMargins {
public static void main(String []args){
//Create a Document instance
Document document = new Document();
//Load a Word document
document.loadFromFile("Sample.docx");

//Get the first section
Section section = document.getSections().get(0);

//Set top, bottom, left and right page margins for the section
section.getPageSetup().getMargins().setTop(17.9f);
section.getPageSetup().getMargins().setBottom(17.9f);
section.getPageSetup().getMargins().setLeft(17.9f);
section.getPageSetup().getMargins().setRight(17.9f);

//Save the result document
document.saveToFile("SetMargins.docx", FileFormat.Docx_2013);
}
}

Effective Shot

Conclusion:

In this post, you have learned how to hide a specific paragraph in a Word document in Java. Not only that, we also have other functions, such as, Java: Change Page Size and Page Orientation in WordJava: Set Gutter Margins in Word and so on. Apart from that, if you'd like to learn more, you can visit this link to explore more about for Spire.Doc for Java.








Comments

Popular posts from this blog

Java: How to encrypt or decrypt PDF documents?

Replace the existing image on the PDF file in C#/VB.NET

How to Convert PDF to Images in Java