Sample Java code for using Apryse SDK to change a page's MediaBox using Rect class. Learn more about our Android SDK and PDF Editing & Manipulation Library.
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6package com.pdftron.android.pdfnetsdksamples.samples;
7
8import com.pdftron.android.pdfnetsdksamples.OutputListener;
9import com.pdftron.android.pdfnetsdksamples.PDFNetSample;
10import com.pdftron.android.pdfnetsdksamples.R;
11import com.pdftron.android.pdfnetsdksamples.util.Utils;
12import com.pdftron.pdf.PDFDoc;
13import com.pdftron.pdf.PageIterator;
14import com.pdftron.pdf.Rect;
15import com.pdftron.sdf.SDFDoc;
16
17import java.util.ArrayList;
18
19public class RectTest extends PDFNetSample {
20
21 private static OutputListener mOutputListener;
22
23 private static ArrayList<String> mFileList = new ArrayList<>();
24
25 public RectTest() {
26 setTitle(R.string.sample_rect_title);
27 setDescription(R.string.sample_rect_description);
28 }
29
30 @Override
31 public void run(OutputListener outputListener) {
32 super.run(outputListener);
33 mOutputListener = outputListener;
34 mFileList.clear();
35 printHeader(outputListener);
36
37 try (PDFDoc input_doc = new PDFDoc((Utils.getAssetTempFile(INPUT_PATH + "tiger.pdf").getAbsolutePath()))) // Test - Adjust the position of content within the page.
38 {
39 mOutputListener.println("_______________________________________________");
40 mOutputListener.println("Opening the input pdf...");
41
42 input_doc.initSecurityHandler();
43
44 PageIterator pg_itr1 = input_doc.getPageIterator();
45
46 Rect media_box = pg_itr1.next().getMediaBox();
47
48 media_box.setX1(media_box.getX1() - 200); // translate the page 200 units (1 uint = 1/72 inch)
49 media_box.setX2(media_box.getX2() - 200);
50
51 media_box.update();
52
53 input_doc.save(Utils.createExternalFile("tiger_shift.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.NO_FLAGS, null);
54 mOutputListener.println("Done. Result saved in tiger_shift...");
55 } catch (Exception e) {
56 mOutputListener.printError(e.getStackTrace());
57 }
58
59 for (String file : mFileList) {
60 addToFileList(file);
61 }
62 printFooter(outputListener);
63 }
64
65}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6package com.pdftron.android.pdfnetsdksamples.samples
7
8import com.pdftron.android.pdfnetsdksamples.OutputListener
9import com.pdftron.android.pdfnetsdksamples.PDFNetSample
10import com.pdftron.android.pdfnetsdksamples.R
11import com.pdftron.android.pdfnetsdksamples.util.Utils
12import com.pdftron.pdf.PDFDoc
13import com.pdftron.sdf.SDFDoc
14import java.util.*
15
16class RectTest : PDFNetSample() {
17 init {
18 setTitle(R.string.sample_rect_title)
19 setDescription(R.string.sample_rect_description)
20 }
21
22 override fun run(outputListener: OutputListener?) {
23 super.run(outputListener)
24 mOutputListener = outputListener
25 mFileList.clear()
26 printHeader(outputListener!!)
27
28 try
29 // Test - Adjust the position of content within the page.
30 {
31 mOutputListener!!.println("_______________________________________________")
32 mOutputListener!!.println("Opening the input pdf...")
33
34 PDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "tiger.pdf")!!.absolutePath).use { input_doc ->
35 input_doc.initSecurityHandler()
36
37 val pg_itr1 = input_doc.pageIterator
38
39 val media_box = pg_itr1.next()!!.getMediaBox()
40
41 media_box.x1 = media_box.x1 - 200 // translate the page 200 units (1 uint = 1/72 inch)
42 media_box.x2 = media_box.x2 - 200
43
44 media_box.update()
45
46 input_doc.save(Utils.createExternalFile("tiger_shift.pdf", mFileList).absolutePath, SDFDoc.SaveMode.NO_FLAGS, null)
47 mOutputListener!!.println("Done. Result saved in tiger_shift...")
48 }
49 } catch (e: Exception) {
50 mOutputListener!!.printError(e.stackTrace)
51 }
52
53 for (file in mFileList) {
54 addToFileList(file)
55 }
56 printFooter(outputListener)
57 }
58
59 companion object {
60
61 private var mOutputListener: OutputListener? = null
62
63 private val mFileList = ArrayList<String>()
64 }
65
66}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales