Sample Java code for using Apryse SDK to programmatically edit an existing PDF document's page display list and the graphics state attributes on existing elements. In particular, this sample strips all images from the page and changes the text color to blue. You can also build a GUI with interactive PDF editor widgets. Some of Apryse SDK's other functions for programmatically editing PDFs include the Cos/SDF low-level API, page manipulation, and more. 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.common.PDFNetException;
13import com.pdftron.pdf.ColorPt;
14import com.pdftron.pdf.ColorSpace;
15import com.pdftron.pdf.Element;
16import com.pdftron.pdf.ElementReader;
17import com.pdftron.pdf.ElementWriter;
18import com.pdftron.pdf.GState;
19import com.pdftron.pdf.PDFDoc;
20import com.pdftron.pdf.Page;
21import com.pdftron.pdf.PageIterator;
22import com.pdftron.sdf.Obj;
23import com.pdftron.sdf.SDFDoc;
24
25import java.util.ArrayList;
26import java.util.Set;
27import java.util.TreeSet;
28
29public class ElementEditTest extends PDFNetSample {
30
31 private static OutputListener mOutputListener;
32
33 private static ArrayList<String> mFileList = new ArrayList<>();
34
35 public ElementEditTest() {
36 setTitle(R.string.sample_elementedit_title);
37 setDescription(R.string.sample_elementedit_description);
38 }
39
40 @Override
41 public void run(OutputListener outputListener) {
42 super.run(outputListener);
43 mOutputListener = outputListener;
44 mFileList.clear();
45 printHeader(outputListener);
46
47 String input_filename = "newsletter.pdf";
48 String output_filename = "newsletter_edited.pdf";
49
50 mOutputListener.println("Opening the input file...");
51 try (PDFDoc doc = new PDFDoc((Utils.getAssetTempFile(INPUT_PATH + input_filename).getAbsolutePath()))) {
52 doc.initSecurityHandler();
53
54 ElementWriter writer = new ElementWriter();
55 ElementReader reader = new ElementReader();
56 Set<Integer> visited = new TreeSet<Integer>();
57
58 PageIterator itr = doc.getPageIterator();
59 while (itr.hasNext()) {
60 try{
61 Page page = itr.next();
62 visited.add((int) page.getSDFObj().getObjNum());
63
64 reader.begin(page);
65 writer.begin(page, ElementWriter.e_replacement, false, true, page.getResourceDict());
66
67 processElements(writer, reader, visited);
68 writer.end();
69 reader.end();
70 } catch (Exception e) {
71 mOutputListener.printError(e.getStackTrace());
72 }
73 }
74
75 // Save modified document
76 doc.save(Utils.createExternalFile(output_filename, mFileList).getAbsolutePath(), SDFDoc.SaveMode.REMOVE_UNUSED, null);
77 mOutputListener.println("Done. Result saved in " + output_filename + "...");
78 } catch (Exception e) {
79 mOutputListener.printError(e.getStackTrace());
80 }
81
82 for (String file : mFileList) {
83 addToFileList(file);
84 }
85 printFooter(outputListener);
86 }
87 public static void processElements(ElementWriter writer, ElementReader reader, Set<Integer> visited) throws PDFNetException {
88 Element element;
89 while ((element = reader.next()) != null) {
90 switch (element.getType()) {
91 case Element.e_image:
92 case Element.e_inline_image:
93 // remove all images by skipping them
94 break;
95 case Element.e_path: {
96 // Set all paths to red color.
97 GState gs = element.getGState();
98 gs.setFillColorSpace(ColorSpace.createDeviceRGB());
99 gs.setFillColor(new ColorPt(1, 0, 0));
100 writer.writeElement(element);
101 }
102 break;
103 case Element.e_text: {
104 // Set all text to blue color.
105 GState gs = element.getGState();
106 gs.setFillColorSpace(ColorSpace.createDeviceRGB());
107 gs.setFillColor(new ColorPt(0, 0, 1));
108 writer.writeElement(element);
109 }
110 break;
111 case Element.e_form: {
112 writer.writeElement(element); // write Form XObject reference to current stream
113 Obj form_obj = element.getXObject();
114 if (!visited.contains((int) form_obj.getObjNum())) // if this XObject has not been processed
115 {
116 // recursively process the Form XObject
117 visited.add((int) form_obj.getObjNum());
118 ElementWriter new_writer = new ElementWriter();
119 reader.formBegin();
120 new_writer.begin(form_obj);
121
122 reader.clearChangeList();
123 new_writer.setDefaultGState(reader);
124
125 processElements(new_writer, reader, visited);
126 new_writer.end();
127 reader.end();
128 }
129 }
130 break;
131 default:
132 writer.writeElement(element);
133 break;
134 }
135 }
136
137 }
138
139}
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.common.PDFNetException
13import com.pdftron.pdf.*
14import com.pdftron.sdf.SDFDoc
15import java.util.*
16
17class ElementEditTest : PDFNetSample() {
18 init {
19 setTitle(R.string.sample_elementedit_title)
20 setDescription(R.string.sample_elementedit_description)
21 }
22
23 override fun run(outputListener: OutputListener?) {
24 super.run(outputListener)
25 mOutputListener = outputListener
26 mFileList.clear()
27 printHeader(outputListener!!)
28
29 val input_filename = "newsletter.pdf"
30 val output_filename = "newsletter_edited.pdf"
31
32 try {
33 mOutputListener!!.println("Opening the input file...")
34 PDFDoc(Utils.getAssetTempFile(INPUT_PATH + input_filename)!!.absolutePath).use { doc ->
35 doc.initSecurityHandler()
36 val writer = ElementWriter()
37 val reader = ElementReader()
38 val visited: MutableSet<Int?> = TreeSet()
39 val itr = doc.pageIterator
40 while (itr.hasNext()) {
41 try {
42 val page = itr.next()
43 visited.add(page!!.sdfObj.objNum.toInt())
44 reader.begin(page)
45 writer.begin(page, ElementWriter.e_replacement, false, true, page!!.resourceDict)
46 processElements(writer, reader, visited)
47 writer.end()
48 reader.end()
49 } catch (e: Exception) {
50 mOutputListener!!.printError(e.stackTrace)
51 }
52 }
53
54 // Save modified document
55 doc.save(Utils.createExternalFile(output_filename, mFileList).absolutePath, SDFDoc.SaveMode.REMOVE_UNUSED, null)
56 mOutputListener!!.println("Done. Result saved in $output_filename...")
57 }
58 } catch (e: Exception) {
59 mOutputListener!!.printError(e.stackTrace)
60 }
61
62 for (file in mFileList) {
63 addToFileList(file!!)
64 }
65 printFooter(outputListener!!)
66 }
67
68 companion object {
69
70 private var mOutputListener: OutputListener? = null
71
72 private val mFileList = ArrayList<String>()
73
74 @Throws(PDFNetException::class)
75 fun processElements(writer: ElementWriter, reader: ElementReader, visited: MutableSet<Int?>) {
76 var element: Element?
77 while (true) {
78 element = reader.next()
79 if (element == null) {
80 break
81 }
82 when (element.type) {
83 Element.e_image, Element.e_inline_image -> {
84 }
85 Element.e_path -> {
86
87 // Set all paths to red color.
88 val gs = element.gState
89 gs.fillColorSpace = ColorSpace.createDeviceRGB()
90 gs.fillColor = ColorPt(1.0, 0.0, 0.0)
91 writer.writeElement(element)
92 }
93 Element.e_text -> {
94
95 // Set all text to blue color.
96 val gs = element.gState
97 gs.fillColorSpace = ColorSpace.createDeviceRGB()
98 gs.fillColor = ColorPt(0.0, 0.0, 1.0)
99 writer.writeElement(element)
100 }
101 Element.e_form -> {
102 writer.writeElement(element) // write Form XObject reference to current stream
103 val form_obj = element.xObject
104 if (!visited.contains(form_obj.objNum.toInt())) // if this XObject has not been processed
105 {
106 // recursively process the Form XObject
107 visited.add(form_obj.objNum.toInt())
108 val new_writer = ElementWriter()
109 reader.formBegin()
110 new_writer.begin(form_obj)
111 reader.clearChangeList()
112 new_writer.setDefaultGState(reader)
113 processElements(new_writer, reader, visited)
114 new_writer.end()
115 reader.end()
116 }
117 }
118 else -> writer.writeElement(element)
119 }
120 }
121 }
122 }
123
124}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales