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 UWP SDK and PDF Editing & Manipulation Library.

1//
2// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
3//
4
5using System;
6using System.IO;
7using System.Threading.Tasks;
8using Windows.Foundation;
9
10using pdftron.PDF;
11using pdftron.SDF;
12
13using PDFNetUniversalSamples.ViewModels;
14
15namespace PDFNetSamples
16{
17 public sealed class ElementEditTest : Sample
18 {
19 public ElementEditTest() :
20 base("ElementEdit", "The sample code shows how to edit the page display list and how to modify graphics state attributes on existing Elements. In particular the sample program strips all images from the page and changes text color to blue.")
21 {
22 }
23
24 public override IAsyncAction RunAsync()
25 {
26 return Task.Run(new System.Action(async () => {
27 WriteLine("--------------------------------");
28 WriteLine("Starting ElementEdit Test...");
29 WriteLine("--------------------------------\n");
30
31 try
32 {
33 string input_file_path = Path.Combine(InputPath, "newsletter.pdf");
34 WriteLine("Opening input file " + input_file_path);
35 PDFDoc doc = new PDFDoc(input_file_path);
36 doc.InitSecurityHandler();
37
38 int num_pages = doc.GetPageCount();
39
40 PageIterator itr = doc.GetPageIterator();
41
42 ElementWriter writer = new ElementWriter();
43 ElementReader reader = new ElementReader();
44
45 while (itr.HasNext())
46 {
47 Page page = itr.Current();
48 reader.Begin(page);
49 writer.Begin(page, ElementWriterWriteMode.e_replacement, false);
50 ProcessElements(reader, writer);
51 writer.End();
52 reader.End();
53
54 itr.Next();
55 }
56
57 String output_file_path = Path.Combine(OutputPath, "newsletter_edited.pdf");
58 await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_remove_unused);
59 doc.Destroy();
60 WriteLine("Done. Result saved in " + output_file_path);
61 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
62 }
63 catch (Exception e)
64 {
65 WriteLine(GetExceptionMessage(e));
66 }
67
68 WriteLine("\n--------------------------------");
69 WriteLine("Done ElementEdit Test.");
70 WriteLine("--------------------------------\n");
71 })).AsAsyncAction();
72 }
73
74 void ProcessElements(ElementReader reader, ElementWriter writer)
75 {
76 Element element;
77 while ((element = reader.Next()) != null) // Read page contents
78 {
79 switch (element.GetType())
80 {
81 case ElementType.e_image:
82 case ElementType.e_inline_image:
83 // remove all images by skipping them
84 continue;
85 case ElementType.e_path: // Process path data...
86 {
87 // Set all paths to red color.
88 GState gs = element.GetGState();
89 gs.SetFillColorSpace(ColorSpace.CreateDeviceRGB());
90 gs.SetFillColor(new ColorPt(1, 0, 0));
91 writer.WriteElement(element);
92 break;
93 }
94 case ElementType.e_text: // Process text strings...
95 {
96 // Set all text to blue color.
97 GState gs = element.GetGState();
98 gs.SetFillColorSpace(ColorSpace.CreateDeviceRGB());
99 gs.SetFillColor(new ColorPt(0, 0, 1));
100 writer.WriteElement(element);
101 break;
102 }
103 case ElementType.e_form: // Recursively process form XObjects
104 {
105 writer.WriteElement(element);
106
107 reader.FormBegin();
108 ElementWriter new_writer = new ElementWriter();
109 new_writer.Begin(element.GetXObject(), true);
110 ProcessElements(reader, new_writer);
111 new_writer.End();
112 reader.End();
113
114 break;
115 }
116 default:
117 {
118 writer.WriteElement(element);
119 break;
120 }
121 }
122 }
123 }
124 }
125}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales