Adjust Margins of an Existing PDF Document in Java

Adjust Margins of an Existing PDF Document in Java

·

3 min read

Margins are white areas around a PDF page. Sometimes, you may need to increase the margins to add additional information. In some other cases, you may also have to reduce the margins. This article demonstrates how to adjust the margins of an existing PDF document using Free Spire.PDF for Java library.

Install the Free Library (2 Methods)

Method 1: Download the Free Spire.PDF for Java and unzip it. Then add the Spire.Pdf.jar file to your Java application as dependency.
Method 2: Directly add the jar dependency to maven project by adding the following configurations 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.pdf.free</artifactId>
        <version>5.1.0</version>
    </dependency>
</dependencies>

Increase Margins of an Existing PDF

To the increase the margin of a PDF document, you can create a new PDF that has a larger page size and then draw the original pages on the larger pages at the proper location. The complete sample code are as follows.

import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfMargins;
import com.spire.pdf.graphics.PdfTemplate;

import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;

public class IncreaseMargins {

    public static void main(String[] args) {

        //Load the original PDF document
        PdfDocument originalPdf = new PdfDocument("C:\\Users\\Administrator\\Desktop\\Olympics.pdf");

        //Get the first page
        PdfPageBase firstPage = originalPdf.getPages().get(0);

        //Create a new PdfDocument object
        PdfDocument newPdf = new PdfDocument();

        //Set increasing value of the margins
        PdfMargins margins = newPdf.getPageSettings().getMargins();
        margins.setTop(40);
        margins.setBottom(40);
        margins.setLeft(40);
        margins.setRight(40);

        //Set the size for the new PDF document
        Dimension2D dimension2D = new Dimension();
        dimension2D.setSize(firstPage.getSize().getWidth() + margins.getLeft() + margins.getRight(), firstPage.getSize().getHeight() + margins.getTop() + margins.getBottom());

        //Loop through the pages in the original document
        for (int i = 0; i < originalPdf.getPages().getCount(); i++) {

            //Create a template based on the source page
            PdfTemplate template = originalPdf.getPages().get(i).createTemplate();

            //Add a page to the new PDF
            PdfPageBase page = newPdf.getPages().add(dimension2D);

            //Draw template on the page
            template.draw(page.getCanvas(), new Point2D.Float(0, 0));
        }

        //Save the new document to file
        newPdf.saveToFile("output/IncreaseMargins.pdf", FileFormat.PDF);
    }
}

increseMargin.jpg

Decrease Margins of an Existing PDF

Likewise, to decrease the margins of a PDF, you can create a new PDF that has a smaller page size and then draw the original pages on the smaller pages at a specified coordinate. The complete sample code are as follows.

import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfMargins;
import com.spire.pdf.graphics.PdfTemplate;

import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;

public class DecreaseMargins {
    public static void main(String[] args) {

        //Load the original PDF document
        PdfDocument originalPdf = new PdfDocument("C:\\Users\\Administrator\\Desktop\\Olympics.pdf");

        //Get the first page
        PdfPageBase firstPage = originalPdf.getPages().get(0);

        //Create a new PdfDocument object
        PdfDocument newPdf = new PdfDocument();

        //Set decreasing value
        double left = -20;
        double right = -20;
        double top = -20;
        double bottom = -20;

        //Set the page size of the new PDF document
        Dimension2D dimension2D = new Dimension();
        dimension2D.setSize(originalPdf.getPages().get(0).getSize().getWidth() + left + right, originalPdf.getPages().get(0).getSize().getHeight() + top + bottom);

        //Loop through the pages in the original document
        for (int i = 0; i < originalPdf.getPages().getCount(); i++) {

            //Create template based on the source page
            PdfTemplate template = originalPdf.getPages().get(i).createTemplate();

            //Add a page to the new PDF
            PdfPageBase page = newPdf.getPages().add(dimension2D, new PdfMargins(0));

            //Draw template on the page
            template.draw(page.getCanvas(), new Point2D.Float((float) left, (float) top));
        }

        //Save the new document to file
        newPdf.saveToFile("output/DecreaseMargins.pdf", FileFormat.PDF);
    }
}

reduceMargin.jpg