ElementEdit

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}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales