Some test text!

menu
search
chevron_right Java samples

Change a PDF Page's MediaBox in Java

Sample Java code for using PDFTron SDK to change a page's MediaBox using Rect class. Learn more about our PDF Editing & Manipulation Library.

To run this sample, get started with a free trial of PDFTron SDK.

//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

import com.pdftron.pdf.*;
import com.pdftron.sdf.SDFDoc;

public class RectTest {

    public static void main(String[] args) {
        PDFNet.initialize();

        // Relative path to the folder containing test files.
        String input_path = "../../TestFiles/";
        String output_path = "../../TestFiles/Output/";

        try // Test  - Adjust the position of content within the page.
        {
            System.out.println("_______________________________________________");
            System.out.println("Opening the input pdf...");

            PDFDoc input_doc = new PDFDoc((input_path + "tiger.pdf"));
            input_doc.initSecurityHandler();

            PageIterator pg_itr1 = input_doc.getPageIterator();

            Rect media_box = pg_itr1.next().getMediaBox();

            media_box.setX1(media_box.getX1() - 200);    // translate the page 200 units (1 uint = 1/72 inch)
            media_box.setX2(media_box.getX2() - 200);

            media_box.update();

            input_doc.save(output_path + "tiger_shift.pdf", SDFDoc.SaveMode.NO_FLAGS, null);
            // output PDF input_doc
            input_doc.close();
            System.out.println("Done. Result saved in tiger_shift...");
        } catch (Exception e) {
            System.out.println(e);
        }

        PDFNet.terminate();
    }
}
close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

PDFTron Receives USD$71 Million Growth Investment Led By Silversmith Capital Partners

Learn more
close