DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on • Edited on

How to Increase or Decrease Margins of an Existing PDF in Java

Margins refer to the blank area around a PDF page. In certain cases, you may need to adjust the margins of your PDF documents. For example, before merging multiple PDF documents into a single PDF, you may want to change the margins so that all documents have the same page size. This article will show you how to increase or decrease margins of an existing PDF document using Spire.PDF for Java.

Installing Spire.Pdf.jar

If you use Maven, you can easily import the Spire.Pdf.jar in your application by adding the following code to your project’s pom.xml file. For non-Maven projects, download the jar file from this link and manually add it as a dependency in your application.

<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</artifactId>
        <verson>4.11.2</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Example 1. Increase Margins of an Existing PDF

To enlarge the margin of a PDF document, the basic thought is to create a new PDF that has bigger page size and then draw the original page on the big page at a proper position. The following code snippet will show you how to turn ideas into reality step by step.

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\\Table.pdf");

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

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

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

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

        //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);

            //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);
    }
}
Enter fullscreen mode Exit fullscreen mode

IncreaseMargins

Example 2. Decrease Margins of an Existing PDF

Likewise, the way to decrease the margins of a PDF is to create a new PDF that has smaller page size and then draw the source page on the small page at a specified coordinate. It’s worth mentioning that the decreasing value on the left, right, top or bottom direction should be less than the existing margins. Otherwise, the content area might be cut off while decreasing the margins.

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\\Table.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(-20, -20));
        }

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

DecreaseMargins

Top comments (0)