Margins are white areas around a PDF page. In cases where you need more white space to add additional information, you can increase the margins. 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 Spire.PDF for Java.
Install Spire.PDF for Java
First of all, you're required to add the Spire.Pdf.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.pdf</artifactId> <version>10.11.2</version> </dependency> </dependencies>
Increase Margins of an Existing PDF
The way to enlarge the margin of a PDF document is to 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 following are the main steps to increase margins of an existing PDF document using Spire.PDF for Java.
- Load the original PDF document while initialing the PdfDocument object.
- Create another PdfDocument object, which is used to create a new PDF document that has a larger page size.
- Set the increasing values of the margins.
- Set the page size of the new PDF document.
- Loop through the pages in the original document, and create a template based on a certain page using PdfPageBase.createTemplate() method.
- Add a page to the new PDF document using PdfDocument.getPages().add() method.
- Draw the template on the page from (0, 0) using PdfTemplate.draw() method.
- Save the new PDF document to file using PdfDocument.saveToFile() method.
- Java
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\\sample.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); } }
Decrease Margins of an Existing PDF
Likewise, the way to decrease the margins of a PDF is to 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 following are the main steps to decrease margins of an existing PDF document using Spire.PDF for Java.
- Load the original PDF document while initialing the PdfDocument object.
- Create another PdfDocument object, which is used to create a new PDF document that has a smaller page size.
- Set the decreasing values of the margins.
- Set the page size of the new PDF document.
- Loop through the pages in the original document, and create a template based on a certain page using PdfPageBase.createTemplate() method.
- Add a page to the new PDF document using PdfDocument.getPages().add() method.
- Draw the template on the page at the specified position using PdfTemplate.draw() method.
- Save the new PDF document to file using PdfDocument.saveToFile() method.
- Java
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\\sample.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); } }
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.