DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on

Change/Reset PDF Page Size to Standard or Custom Paper Size in Java

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 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.

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

Output PDF:
Alt Text

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.

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

Output PDF:
Alt Text

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.

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

Output PDF:
Alt Text

Top comments (0)