Skip to main content

Resize PDF Pages

Note

Page size is measured in points (1 inch = 72 points).

Use the Page.resize() method to resize a page. Specify new page bounds and content alignment within the new area.

The following code snippet resizes all pages in a PDF document to A4 size, centers the page content, and saves the result to a new PDF file:

import com.devexpress.docs.pdf.*;
import com.devexpress.drawing.printing.*;
import com.devexpress.system.drawing.*;

import java.nio.channels.*;
import java.nio.file.*;

public class Main {
    public static void main(String[] args) throws Exception {
        try (PdfDocument pdfDocument = new PdfDocument(FileChannel.open(
                Path.of("document.pdf")))) {

            try {
                // Resize all pages to A4 and center their content.
                for(Page page : pdfDocument.getPages()) {
                    page.resize(
                        new RectangleF(0, 0, 595.28f, 841.89f),
                        PageContentHorizontalAlignment.CENTER,
                        PageContentVerticalAlignment.CENTER
                    );
                }
            } catch (Exception e) {
                System.out.println("The page resize operation failed.\n" +
                    "The target page size must be compatible with the existing page layout.");
            }
            // Save the document to a PDF file.
            try (WritableByteChannel writableByteChannel =
                    FileChannel.open(Path.of("result.pdf"),
                        StandardOpenOption.CREATE,
                        StandardOpenOption.WRITE,
                        StandardOpenOption.TRUNCATE_EXISTING)) {

                pdfDocument.save(writableByteChannel);
            }
        }
    }
}

Note

The target page size must be compatible with the existing page layout. If the new page bounds are incompatible, the resize method throws an exception.

Next Steps