ElementEdit

Sample C# 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 Server SDK and PDF Editing & Manipulation Library.

1//
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3//
4
5using System;
6using System.Collections.Generic;
7
8using pdftron;
9using pdftron.Common;
10using pdftron.Filters;
11using pdftron.SDF;
12using pdftron.PDF;
13
14using XSet = System.Collections.Generic.List<int>;
15
16namespace ElementEditTestCS
17{
18 /// <summary>
19 /// The sample code shows how to edit the page display list and how to modify graphics state
20 /// attributes on existing Elements. In particular the sample program strips all images from
21 /// the page, changes path fill color to red, and changes text fill color to blue.
22 /// </summary>
23 class Class1
24 {
25 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
26 static Class1() {}
27
28 static void ProcessElements(ElementReader reader, ElementWriter writer, XSet visited)
29 {
30 Element element;
31 while ((element = reader.Next()) != null) // Read page contents
32 {
33 switch (element.GetType())
34 {
35 case Element.Type.e_image:
36 case Element.Type.e_inline_image:
37 // remove all images by skipping them
38 break;
39 case Element.Type.e_path:
40 {
41 // Set all paths to red color.
42 GState gs = element.GetGState();
43 gs.SetFillColorSpace(ColorSpace.CreateDeviceRGB());
44 gs.SetFillColor(new ColorPt(1, 0, 0));
45 writer.WriteElement(element);
46 break;
47 }
48 case Element.Type.e_text:
49 {
50 // Set all text to blue color.
51 GState gs = element.GetGState();
52 gs.SetFillColorSpace(ColorSpace.CreateDeviceRGB());
53 gs.SetFillColor(new ColorPt(0, 0, 1));
54 writer.WriteElement(element);
55 break;
56 }
57 case Element.Type.e_form:
58 {
59 writer.WriteElement(element); // write Form XObject reference to current stream
60
61 Obj form_obj = element.GetXObject();
62 if (!visited.Contains(form_obj.GetObjNum())) // if this XObject has not been processed
63 {
64 // recursively process the Form XObject
65 visited.Add(form_obj.GetObjNum());
66 ElementWriter new_writer = new ElementWriter();
67
68 reader.FormBegin();
69 new_writer.Begin(form_obj, true);
70
71 reader.ClearChangeList();
72 new_writer.SetDefaultGState(reader);
73
74 ProcessElements(reader, new_writer, visited);
75 new_writer.End();
76 reader.End();
77 }
78 break;
79 }
80 default:
81 writer.WriteElement(element);
82 break;
83 }
84 }
85 }
86
87 /// <summary>
88 /// The main entry point for the application.
89 /// </summary>
90 [STAThread]
91 static void Main(string[] args)
92 {
93 PDFNet.Initialize(PDFTronLicense.Key);
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 Console.WriteLine("Opening the input file...");
104 using (PDFDoc doc = new PDFDoc(input_path + input_filename))
105 {
106 doc.InitSecurityHandler();
107
108 ElementWriter writer = new ElementWriter();
109 ElementReader reader = new ElementReader();
110 XSet visited = new XSet();
111
112 PageIterator itr = doc.GetPageIterator();
113
114 while (itr.HasNext())
115 {
116 try
117 {
118 Page page = itr.Current();
119 visited.Add(page.GetSDFObj().GetObjNum());
120
121 reader.Begin(page);
122 writer.Begin(page, ElementWriter.WriteMode.e_replacement, false, true, page.GetResourceDict());
123
124 ProcessElements(reader, writer, visited);
125 writer.End();
126 reader.End();
127
128 itr.Next();
129 }
130 catch (PDFNetException e)
131 {
132 Console.WriteLine(e.Message);
133 }
134 }
135
136 doc.Save(output_path + output_filename, SDFDoc.SaveOptions.e_remove_unused);
137 Console.WriteLine("Done. Result saved in {0}...", output_filename);
138 }
139 }
140 catch (PDFNetException e)
141 {
142 Console.WriteLine(e.Message);
143 }
144 PDFNet.Terminate();
145 }
146 }
147}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales