Edit PDF� - C++ 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
6#include <PDF/PDFNet.h>
7#include <PDF/PDFDoc.h>
8#include <PDF/ElementWriter.h>
9#include <PDF/ElementReader.h>
10#include <SDF/Obj.h>
11#include <set>
12#include <iostream>
13#include "../../LicenseKey/CPP/LicenseKey.h"
14
15using namespace pdftron;
16using namespace std;
17using namespace SDF;
18using namespace PDF;
19
20
21//---------------------------------------------------------------------------------------
22// The sample code shows how to edit the page display list and how to modify graphics state
23// attributes on existing Elements. In particular the sample program strips all images from
24// the page, changes path fill color to red, and changes text fill color to blue.
25//---------------------------------------------------------------------------------------
26
27// XObjects are guaranteed to have unique object numbers
28typedef set<pdftron::UInt32> XObjSet;
29
30
31static void ProcessElements(ElementReader& reader, ElementWriter& writer, XObjSet& visited)
32{
33 Element element;
34 while (element = reader.Next()) // Read page contents
35 {
36 switch (element.GetType())
37 {
38 case Element::e_image:
39 case Element::e_inline_image:
40 // remove all images by skipping them
41 break;
42 case Element::e_path:
43 {
44 // Set all paths to red color.
45 GState gs = element.GetGState();
46 gs.SetFillColorSpace(ColorSpace::CreateDeviceRGB());
47 ColorPt cp(1, 0, 0);
48 gs.SetFillColor(cp);
49 writer.WriteElement(element);
50 break;
51 }
52 case Element::e_text:
53 {
54 // Set all text to blue color.
55 GState gs = element.GetGState();
56 gs.SetFillColorSpace(ColorSpace::CreateDeviceRGB());
57 ColorPt cp(0, 0, 1);
58 gs.SetFillColor(cp);
59 writer.WriteElement(element);
60 break;
61 }
62 case Element::e_form:
63 {
64 writer.WriteElement(element); // write Form XObject reference to current stream
65
66 Obj form_obj = element.GetXObject();
67 if (visited.find(form_obj.GetObjNum()) == visited.end()) // if this XObject has not been processed
68 {
69 // recursively process the Form XObject
70 visited.insert(form_obj.GetObjNum());
71 ElementWriter new_writer;
72 reader.FormBegin();
73 new_writer.Begin(form_obj);
74
75 reader.ClearChangeList();
76 new_writer.SetDefaultGState(reader);
77
78 ProcessElements(reader, new_writer, visited);
79 new_writer.End();
80 reader.End();
81 }
82 break;
83 }
84 default:
85 writer.WriteElement(element);
86 }
87 }
88}
89
90int main(int argc, char *argv[])
91{
92 int ret = 0;
93 PDFNet::Initialize(LicenseKey);
94
95 // Relative path to the folder containing test files.
96 string input_path = "../../TestFiles/";
97 string output_path = "../../TestFiles/Output/";
98 string input_filename = "newsletter.pdf";
99 string output_filename = "newsletter_edited.pdf";
100
101 try
102 {
103 cout << "Opening the input file..." << endl;
104 PDFDoc doc(input_path + input_filename);
105 doc.InitSecurityHandler();
106
107 ElementWriter writer;
108 ElementReader reader;
109 XObjSet visited;
110
111 // Process each page in the document
112 for (PageIterator itr = doc.GetPageIterator();itr.HasNext();itr.Next())
113 {
114 try {
115 Page page = itr.Current();
116 visited.insert(page.GetSDFObj().GetObjNum());
117
118 reader.Begin(page);
119 writer.Begin(page, ElementWriter::e_replacement, false, true, page.GetResourceDict());
120 ProcessElements(reader, writer, visited);
121 writer.End();
122 reader.End();
123 }
124 catch (Common::Exception& e)
125 {
126 cout << e << endl;
127 }
128 }
129
130 // Save modified document
131 doc.Save(output_path + output_filename, SDFDoc::e_remove_unused, 0);
132 cout << "Done. Result saved in " << output_filename <<"..." << endl;
133 }
134 catch(Common::Exception& e)
135 {
136 cout << e << endl;
137 ret = 1;
138 }
139 catch(...)
140 {
141 cout << "Unknown Exception" << endl;
142 ret = 1;
143 }
144 PDFNet::Terminate();
145 return ret;
146}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales