In
the process of manipulating PDF documents, you may not want to directly place
annotations or stamps over the text of the document, but the side, top or
bottom margins are too narrow. In this case, adding an extra margin will give
you more room. This article will demonstrate how to increase or decrease margins of a PDF document using Java
codes.
DEPENDENCY
First
of all, you’re required to download the package of Free Spire.PDF for Java from this link, and then manually add the
Spire.Pdf.jar as a dependency in your Java program. Or if you use Maven, you
can add the following code in the pom.xml file to easily import the JAR 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 a PDF Document
In
order to increase the margins of a PDF document, Free Spire.PDF for Java
provides relavant methods for creating a new PDF with a larger page size, and
then placing the original pages at the proper position on the new page. The
following are detailed steps to finish the operation listed above.
l Load
the original PDF document while initialing the PdfDocument object.
l Create
a new PDF document with a larger page size while creating another PdfDocument instance.
l Set
the increasing values of the margins.
l Set
the page size of the new PDF document.
l Loop
through the pages of the original document, and create a template based on a
certain page using PdfPageBase.createTemplate()
method.
l Add a
page to the new PDF document using PdfDocument.getPages().add()
method.
l Draw
the template on the page from (0, 0) using PdfTemplate.draw()
method.
l Save
the new PDF document to another file using PdfDocument.saveToFile()
method.
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 IncreaseMargin {
public static void main(String[] args) {
//Load the original PDF document
PdfDocument originalPdf = new PdfDocument("C:\\Users\\Test1\\Desktop\\Test.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(70);
margins.setBottom(70);
margins.setLeft(70);
margins.setRight(70);
//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);
}
}

Decrease Margins
of a PDF Document
Likewise,
the way to decrease the margins of a PDF document is to create a new PDF with a
smaller page size and then draw the original pages on the smaller pages at a
specified coordinate. You can follow the detailed steps below.
l Load
the original PDF document while initialing the PdfDocument object.
l Create
another PdfDocument instance, which
is used to create a new PDF document with a smaller page size.
l Set
the decreasing values of the margins.
l Set
the page size of the new PDF document.
l Loop
through the pages in the original document, and create a template based on a
certain page using PdfPageBase.createTemplate()
method.
l Add a
page to the new PDF document using PdfDocument.getPages().add()
method.
l Draw
the template on the page at the specified position using PdfTemplate.draw() method.
l Save
the new PDF document to another file using PdfDocument.saveToFile()
method.
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 DecreaseMargin {
public static void main(String[] args) {
//Load the original PDF document
PdfDocument originalPdf = new PdfDocument("C:\\Users\\Test1\\Desktop\\Test.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);
}
}
