Change/Reset PDF Page Size to Standard or Custom Paper Size in Java
Tags: java, pdf, changepagesize
Published: 2021-08-20 09:15:36

---
title: Change/Reset PDF Page Size to Standard or Custom Paper Size in Java
published: true
description: This article demonstrates how to reset pdf page size to standard or custom paper size in Java.
tags: Java, Pdf, change#page#size
//cover_image: https://direct_url_to_image.jpg
---
Here I am going to introduce how to change/reset PDF page size programmatically in Java using Free Spire.PDF for Java library.

Free Spire.PDF for Java supports to resize pages in a PDF document by creating a new document with new pages of a desired paper size, then creating templates from the original PDF document and later drawing the templates to the new pages of the new document. This process will preserve text, images and all other elements present in the original PDF. 
      
**Contents Summary**
* Change PDF Page Size to a Standard Paper Size
* Change PDF Page Size to a Custom Paper Size
* Change PDF Page Size by Scaling Down Original Page Size

Before running the following examples, you need to add dependencies for including [Free Spire.PDF for Java library](https://www.e-iceblue.com/Download/pdf-for-java-free.html) in your Java project.

**Change PDF Page Size to a Standard Paper Size**

Free Spire.PDF for Java supports various standard paper sizes, such as A0, A1, A2, A3, A4…, B0, B1, B2, B3, B4… and many more. The complete types of supported paper sizes can be found in the **PdfPageSize** Enum. 

In the following example, you will see how to change the page size of a PDF document to A1.
```java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.PdfPageSize;
import com.spire.pdf.graphics.*;

import java.awt.geom.Point2D;

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

        //Create a new PDF document
        PdfDocument newPdf = new PdfDocument();
        //Set page size as A1
        newPdf.getPageSettings().setSize(PdfPageSize.A1);
        //Remove page margins
        newPdf.getPageSettings().setMargins(new PdfMargins(0));

        //Load the sample PDF document
        PdfDocument pdf = new PdfDocument("Sample.pdf");

        //Iterate through the pages in the sample PDF
        for(int i = 0; i< pdf.getPages().getCount(); i++)
        {
            //Add pages to the new PDF
            PdfPageBase newPage = newPdf.getPages().add();
            //Set text layout as 1 page, if not set the content will not scale to fit page size
            PdfTextLayout layout = new PdfTextLayout();
            layout.setLayout(PdfLayoutType.One_Page);
            //Create templates for the pages in sample PDF and draw templates to the pages of the new PDF
            pdf.getPages().get(i).createTemplate().draw(newPage, new Point2D.Float(0,0), layout);
        }

        //Save the result document
        newPdf.saveToFile("ChangePageSizeToA1.pdf");
    }
}
```
Output PDF:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dy49ujhh1ay9to7oa2e2.png)
 
**Change PDF Page Size to a Custom Paper Size**

The unit used in Free Spire.PDF library is **pt** (point). In order to reset PDF page size to a custom paper size, you need to convert the unit from inch, millimeter/centimeter to point.

The following example shows how to change the page size of a PDF document to a custom paper size of **6.5*8.5** inches.
```java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.*;

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

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

        //Create a new PDF
        PdfDocument newPdf = new PdfDocument();
        //Convert inches to points 
        PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
        float width = unitCvtr.convertUnits(6.5f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);
        float height = unitCvtr.convertUnits(8.5f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);
        //Specify custom size
        Dimension2D customSize = new Dimension((int)width, (int)height);
        //Set page size as the custom size
        newPdf.getPageSettings().setSize(customSize);
        //Remove page margins
        newPdf.getPageSettings().setMargins(new PdfMargins(0));

        //Load the sample PDF
        PdfDocument pdf = new PdfDocument("Sample.pdf");

        //Iterate through the pages in the sampe PDF
        for(int i = 0; i< pdf.getPages().getCount(); i++)
        {
            //Add pages to the new PDF
            PdfPageBase newPage = newPdf.getPages().add();
            //Set text layout as 1 page, if not set the content will not scale to fit page size
            PdfTextLayout layout = new PdfTextLayout();
            layout.setLayout(PdfLayoutType.One_Page);
            //Create templates for the pages in sample PDF and draw templates to the pages of the new PDF
            pdf.getPages().get(i).createTemplate().draw(newPage, new Point2D.Float(0,0), layout);
        }

        //Save the result document
        newPdf.saveToFile("ChangePageSizetoCustomPaperSize.pdf");
    }
}
```
Output PDF:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4lyiweno07ymsq5u11xj.png) 

**Change PDF Page Size by Scaling Down Original Page Size**

The following example shows how to change PDF page size by scaling down the page size of the original PDF document.
```java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.*;

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

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

        //Load the sample PDF
        PdfDocument pdf = new PdfDocument("Sample.pdf");

        //Sepecify new size by scaling down the orginal page size
        float width = pdf.getPageSettings().getWidth() * 0.8f;
        float height = pdf.getPageSettings().getHeight() * 0.8f;
        Dimension2D newSize = new Dimension((int)width, (int)height);

        //Create a new PDF
        PdfDocument newPdf = new PdfDocument();
        //Set page size as the new size
        newPdf.getPageSettings().setSize(newSize);
        //Remove page margins
        newPdf.getPageSettings().setMargins(new PdfMargins(0));

        //Iterate through the pages in the sampe PDF
        for(int i = 0; i< pdf.getPages().getCount(); i++)
        {
            //Add pages to the new PDF
            PdfPageBase newPage = newPdf.getPages().add();
            //Set text layout as 1 page, if not set the content will not scale to fit page size
            PdfTextLayout layout = new PdfTextLayout();
            layout.setLayout(PdfLayoutType.One_Page);
            //Create templates for the pages in sample PDF and draw templates to the pages of the new PDF
            pdf.getPages().get(i).createTemplate().draw(newPage, new Point2D.Float(0,0), layout);
        }

        //Save the result document
        newPdf.saveToFile("ScaleDownPageSize.pdf");
    }
}
```
Output PDF:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lazok7y38mdv59krj3cm.png)