1//
2// Copyright (c) 2001-2021 by PDFTron Systems 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
16using NUnit.Framework;
17
18namespace MiscellaneousSamples
19{
20 /// <summary>
21 /// The sample code shows how to edit the page display list and how to modify graphics state
22 /// attributes on existing Elements. In particular the sample program strips all images from
23 /// the page, changes path fill color to red, and changes text fill color to blue.
24 /// </summary>
25 [TestFixture]
26 public class ElementEditTest
27 {
28
29 static void ProcessElements(ElementReader reader, ElementWriter writer, XSet visited)
30 {
31 Element element;
32 while ((element = reader.Next()) != null) // Read page contents
33 {
34 switch (element.GetType())
35 {
36 case Element.Type.e_image:
37 case Element.Type.e_inline_image:
38 // remove all images by skipping them
39 break;
40 case Element.Type.e_path:
41 {
42 // Set all paths to red color.
43 GState gs = element.GetGState();
44 gs.SetFillColorSpace(ColorSpace.CreateDeviceRGB());
45 gs.SetFillColor(new ColorPt(1, 0, 0));
46 writer.WriteElement(element);
47 break;
48 }
49 case Element.Type.e_text:
50 {
51 // Set all text to blue color.
52 GState gs = element.GetGState();
53 gs.SetFillColorSpace(ColorSpace.CreateDeviceRGB());
54 gs.SetFillColor(new ColorPt(0, 0, 1));
55 writer.WriteElement(element);
56 break;
57 }
58 case Element.Type.e_form:
59 {
60 writer.WriteElement(element); // write Form XObject reference to current stream
61
62 Obj form_obj = element.GetXObject();
63 if (!visited.Contains(form_obj.GetObjNum())) // if this XObject has not been processed
64 {
65 // recursively process the Form XObject
66 visited.Add(form_obj.GetObjNum());
67 ElementWriter new_writer = new ElementWriter();
68
69 reader.FormBegin();
70 new_writer.Begin(form_obj, true);
71
72 reader.ClearChangeList();
73 new_writer.SetDefaultGState(reader);
74
75 ProcessElements(reader, new_writer, visited);
76 new_writer.End();
77 reader.End();
78 }
79 break;
80 }
81 default:
82 writer.WriteElement(element);
83 break;
84 }
85 }
86 }
87
88 /// <summary>
89 /// The main entry point for the application.
90 /// </summary>
91 [Test]
92 public static void Sample()
93 {
94
95 // Relative path to the folder containing test files.
96 const string input_path = "TestFiles/";
97 string input_filename = "newsletter.pdf";
98 string output_filename = "newsletter_edited.pdf";
99
100 try
101 {
102 Console.WriteLine("Opening the input file...");
103 using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + input_filename)))
104 {
105 doc.InitSecurityHandler();
106
107 ElementWriter writer = new ElementWriter();
108 ElementReader reader = new ElementReader();
109 XSet visited = new XSet();
110
111 PageIterator itr = doc.GetPageIterator();
112
113 while (itr.HasNext())
114 {
115 try
116 {
117 Page page = itr.Current();
118 visited.Add(page.GetSDFObj().GetObjNum());
119
120 reader.Begin(page);
121 writer.Begin(page, ElementWriter.WriteMode.e_replacement, false, true, page.GetResourceDict());
122
123 ProcessElements(reader, writer, visited);
124 writer.End();
125 reader.End();
126
127 itr.Next();
128 }
129 catch (PDFNetException e)
130 {
131 Console.WriteLine(e.Message);
132 Assert.True(false);
133 }
134 }
135
136 doc.Save(Utils.CreateExternalFile(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 Assert.True(false);
144 }
145 }
146 }
147}