Edit PDF� - Java Sample Code

Sample 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, provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, Go and VB. 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 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;
8import com.pdftron.common.PDFNetException;
9
10import java.util.*;
11
12import com.pdftron.sdf.Obj;
13
14//---------------------------------------------------------------------------------------
15// The sample code shows how to edit the page display list and how to modify graphics state
16// attributes on existing Elements. In particular the sample program strips all images from
17// the page, changes path fill color to red, and changes text color to blue.
18//---------------------------------------------------------------------------------------
19public class ElementEditTest {
20 public static void processElements(ElementWriter writer, ElementReader reader, Set<Integer> visited) throws PDFNetException {
21 Element element;
22 while ((element = reader.next()) != null) {
23 switch (element.getType()) {
24 case Element.e_image:
25 case Element.e_inline_image:
26 // remove all images by skipping them
27 break;
28 case Element.e_path: {
29 // Set all paths to red color.
30 GState gs = element.getGState();
31 gs.setFillColorSpace(ColorSpace.createDeviceRGB());
32 gs.setFillColor(new ColorPt(1, 0, 0));
33 writer.writeElement(element);
34 }
35 break;
36 case Element.e_text: {
37 // Set all text to blue color.
38 GState gs = element.getGState();
39 gs.setFillColorSpace(ColorSpace.createDeviceRGB());
40 gs.setFillColor(new ColorPt(0, 0, 1));
41 writer.writeElement(element);
42 }
43 break;
44 case Element.e_form: {
45 writer.writeElement(element); // write Form XObject reference to current stream
46 Obj form_obj = element.getXObject();
47 if (!visited.contains((int) form_obj.getObjNum())) // if this XObject has not been processed
48 {
49 // recursively process the Form XObject
50 visited.add((int) form_obj.getObjNum());
51 ElementWriter new_writer = new ElementWriter();
52 reader.formBegin();
53 new_writer.begin(form_obj);
54
55 reader.clearChangeList();
56 new_writer.setDefaultGState(reader);
57
58 processElements(new_writer, reader, visited);
59 new_writer.end();
60 reader.end();
61 }
62 }
63 break;
64 default:
65 writer.writeElement(element);
66 break;
67 }
68 }
69
70 }
71
72 public static void main(String[] args) {
73
74 PDFNet.initialize(PDFTronLicense.Key());
75
76 // Relative path to the folder containing test files.
77 String input_path = "../../TestFiles/";
78 String output_path = "../../TestFiles/Output/";
79 String input_filename = "newsletter.pdf";
80 String output_filename = "newsletter_edited.pdf";
81
82 System.out.println("Opening the input file...");
83 try (PDFDoc doc = new PDFDoc((input_path + input_filename))) {
84 doc.initSecurityHandler();
85
86 ElementWriter writer = new ElementWriter();
87 ElementReader reader = new ElementReader();
88 Set<Integer> visited = new TreeSet<Integer>();
89
90 PageIterator itr = doc.getPageIterator();
91 while (itr.hasNext()) {
92 try{
93 Page page = itr.next();
94 visited.add((int) page.getSDFObj().getObjNum());
95
96 reader.begin(page);
97 writer.begin(page, ElementWriter.e_replacement, false, true, page.getResourceDict());
98
99 processElements(writer, reader, visited);
100 writer.end();
101 reader.end();
102 } catch (Exception e) {
103 e.printStackTrace();
104 }
105 }
106
107 // Save modified document
108 doc.save(output_path + output_filename, SDFDoc.SaveMode.REMOVE_UNUSED, null);
109 System.out.println("Done. Result saved in " + output_filename + "...");
110 } catch (Exception e) {
111 e.printStackTrace();
112 }
113
114 PDFNet.terminate();
115 }
116}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales