Change PDF Media Box - Rect - Java Sample Code

Sample code for using Apryse SDK to change a page's MediaBox using Rect class. Sample code provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby and VB.

Learn more about our Server SDK and PDF Editing & Manipulation Library.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import com.pdftron.pdf.*;
7import com.pdftron.sdf.SDFDoc;
8
9public class RectTest {
10
11 public static void main(String[] args) {
12 PDFNet.initialize(PDFTronLicense.Key());
13
14 // Relative path to the folder containing test files.
15 String input_path = "../../TestFiles/";
16 String output_path = "../../TestFiles/Output/";
17
18 try (PDFDoc input_doc = new PDFDoc((input_path + "tiger.pdf"))) // Test - Adjust the position of content within the page.
19 {
20 System.out.println("_______________________________________________");
21 System.out.println("Opening the input pdf...");
22
23 input_doc.initSecurityHandler();
24
25 PageIterator pg_itr1 = input_doc.getPageIterator();
26
27 Rect media_box = pg_itr1.next().getMediaBox();
28
29 media_box.setX1(media_box.getX1() - 200); // translate the page 200 units (1 uint = 1/72 inch)
30 media_box.setX2(media_box.getX2() - 200);
31
32 media_box.update();
33
34 input_doc.save(output_path + "tiger_shift.pdf", SDFDoc.SaveMode.NO_FLAGS, null);
35 System.out.println("Done. Result saved in tiger_shift...");
36 } catch (Exception e) {
37 System.out.println(e);
38 }
39
40 PDFNet.terminate();
41 }
42}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales