Page margins are the blank spaces between the body content and the page edges. In Microsoft Word, the default margins of each page are set as 1 inch, but sometimes you may need to resize the margins to accordance with your requirements. In this article, you will learn how to set page margins for Word documents in Java using Spire.Doc for Java.

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>14.4.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.
  • 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);
    }
}

Java: Set Page Margins for Word Documents

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.

Java: Insert Images to Word Documents

2022-06-13 07:44:00 Written by Koohji

MS Word allows users to insert a lot of elements in Word documents, including text, images, charts, and files. Images are frequently used in Word documents to make them intuitive, to express ideas that are hard to express in words, or just to make them beautiful. This article provides you a convenient and efficient way to insert images to Word documents under the help of Spire.Doc for Java.

Install Spire.Doc for Java

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. 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>14.4.0</version>
    </dependency>
</dependencies>

Insert an Image to a Word Document with Specified Text Wrapping Style

The detailed steps of inserting images with specified wrapping style are as follows:

  • Create an object of Document class.
  • Load a Word document from disk using Document.loadFromFile() method.
  • Create an object of DocPicture class.
  • Load an image from disk using DocPicture.loadImage() method.
  • Set the size of the image using DocPicture.setWidth() and DocPicture.setHeight() method.
  • Set the text wrapping style of the image as Square using DocPicture.setTextWrappingStyle() method.
  • Insert the image at the start of the second paragraph using Paragraph.getChildObjects().insert() method.
  • Save the document using Document.saveToFile method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;

public class insertImage {
    public static void main(String[] args) throws Exception {
        //Create an object of Document class
        Document doc = new Document();

        //Load a Word document from disk
        doc.loadFromFile("D:/Samples/Sample.docx");

        //Create an object of DocPicture class
        DocPicture picture = new DocPicture(doc);

        //Load an image from disk
        picture.loadImage("D:/Samples/System.png");

        //Set the size of the image
        picture.setWidth(75);
        picture.setHeight(90);

        //Set the text wrapping style of the image as Square
        picture.setTextWrappingStyle( TextWrappingStyle.Square);

        //Insert the image at the start of the second paragraph
        doc.getSections().get(0).getParagraphs().get(1).getChildObjects().insert(0,picture);

        //Save the document
        doc.saveToFile("D:/javaOutput/insertImage.docx", FileFormat.Docx);
    }
}

Java: Insert Images to Word Documents

Insert an Image to a Word Document at a Specific Location

The detailed steps of inserting images at a specific location are as follows:

  • Create an object of Document class.
  • Load a Word document from disk using Document.loadFromFile() method.
  • Create an object of DocPicture class.
  • Load an image from disk using DocPicture.loadImage() method.
  • Set the size of the image using DocPicture.setWidth() and DocPicture.setHeight() method.
  • Set the text wrapping style of the image as Tight using DocPicture.setTextWrappingStyle() method.
  • Insert the image into the third paragraph using Paragraph.getChildObjects().insert() method.
  • Set the position of the image using DocPicture.setHorizontalPosition() and DocPicture.setVerticalPositon() method. The original location is at the start of the selected paragraph.
  • Save the document using Document.saveToFile method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;

public class insertImage {
    public static void main(String[] args) throws Exception {
        //Create an object of Document class
        Document doc = new Document();

        //Load a Word document from disk
        doc.loadFromFile("D:/Samples/Sample.docx");

        //Create an object of DocPicture class
        DocPicture picture = new DocPicture(doc);

        //Load an image from disk
        picture.loadImage("D:/Samples/PDF.png");

        //Set the size of the image
        picture.setWidth(75);
        picture.setHeight(90);

        //Set the text wrapping style of the image as Tight
        picture.setTextWrappingStyle( TextWrappingStyle.Tight);

        //Insert the image into the Third paragraph
        doc.getSections().get(0).getParagraphs().get(2).getChildObjects().insert(0,picture);

        //Set the position of the image
        picture.setHorizontalPosition(370.0F);
        picture.setVerticalPosition(10.0F);

        //Save the document
        doc.saveToFile("D:/javaOutput/insertImage.docx", FileFormat.Docx);
    }
}

Java: Insert Images to Word Documents

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.

The background of a Word document is blank by default. For documents like brochures, invitations, handouts, and marketing materials, blank backgrounds will be too tedious. A good-looking background can be a great attraction to readers. Therefore, you can add a color or insert an image as a background to make your documents more appealing. This article shows how to set background color or image for Word documents by programming using Spire.Doc for Java.

Install Spire.Doc for Java

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. 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>14.4.0</version>
    </dependency>
</dependencies>

Add a Background Color to a Word Document

Adding a background color to a Word document is very easy. You only need to set the background type as color and then choose a color as your background. The detailed steps are as follows.

  • Create an object of Document class.
  • Load a Word document using Document.loadFromFile() method.
  • Set the background type as color using Document.getBackground().setType() method.
  • Set the background color using Document.getBackground().setColor() method.
  • Save the document using Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.BackgroundType;

import java.awt.*;
import java.io.*;

public class addBackgroundColor {
    public static void main(String[] args) throws IOException {

        //Create an object of Document class
        Document document= new Document();

        //Load a Word document
        document.loadFromFile("C:/Sample.docx");

        //Set the background type as color
        document.getBackground().setType(BackgroundType.Color);

        //Set the background color as orange
        document.getBackground().setColor(Color.orange);

        //Save the document
        document.saveToFile("AddBackgroundColor.docx", FileFormat.Docx);
    }
}

Java: Add Background Color or Picture to Word Documents

Add a Gradient Background to a Word Document

Adding gradient background requires more steps. You need to set the background type as gradient, choose two colors, and then set shading variant and style. The detailed steps are as follows.

  • Create an object of Document class.
  • Load a Word document using Document.loadFromFile() method.
  • Set the background type as gradient using Document.getBackground().setType() method.
  • Choose two colors using >Background.getGradient().setColor1() and Background.getGradient().setColor2() method.
  • Set shading variant using Background.getGradient().setShadingVariant() method.
  • Set shading style using Background.getGradient().setShadingStyle() method.
  • Save the document using Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.BackgroundType;
import com.spire.doc.documents.GradientShadingStyle;
import com.spire.doc.documents.GradientShadingVariant;
import java.awt.*;
import java.io.*;

public class addBackgroundColor {
    public static void main(String[] args) throws IOException {

        //load a word document
        Document document= new Document("C:/Sample.docx");

        //Set the background type as gradient
        document.getBackground().setType(BackgroundType.Gradient);

        //Choose two colors
        Background background = document.getBackground();
        background.getGradient().setColor1(Color.white);
        background.getGradient().setColor2(Color.orange);

        //Choose gradient variant
        background.getGradient().setShadingVariant(GradientShadingVariant.Shading_Down);

        //Choose gradient style
        background.getGradient().setShadingStyle(GradientShadingStyle.Horizontal);

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

Java: Add Background Color or Picture to Word Documents

Insert Background Image to a Word Document

To insert a background picture to a Word document, you need to set the background type as picture, and then insert a picture as the background. The detailed steps are as follows.

  • Create an object of Document class.
  • Load a Word document using Document.loadFromFile() method.
  • Change the background type to picture using Document.getBackground().setType() method.
  • Insert a picture as the background using Document.getBackground().setPicture() method.
  • Save the document using Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.BackgroundType;
import java.io.*;

public class addBackgroundColor {
    public static void main(String[] args) throws IOException {

        //Create an object of Document class
        Document document= new Document();

        //Load a Word document
        document.loadFromFile("C:/Sample.docx");

        //Set the background type as picture
        document.getBackground().setType(BackgroundType.Picture);

        //Set the background picture
        document.getBackground().setPicture("C:/background.jpg");

        //Save the document
        document.saveToFile("AddBackgroundPicture.docx", FileFormat.Docx);
    }
}

Java: Add Background Color or Picture to Word Documents

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.

page 156