Edit PDF� - Ruby 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-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6require '../../../PDFNetC/Lib/PDFNetRuby'
7include PDFNetRuby
8require '../../LicenseKey/RUBY/LicenseKey'
9
10$stdout.sync = true
11
12#---------------------------------------------------------------------------------------
13# The sample code shows how to edit the page display list and how to modify graphics state
14# attributes on existing Elements. In particular the sample program strips all images from
15# the page and changes text color to blue.
16#---------------------------------------------------------------------------------------
17
18def ProcessElements(reader, writer, map)
19 element = reader.Next() # Read page contents
20 while !element.nil? do
21 type = element.GetType()
22 case type
23 when Element::E_image
24 # remove all images by skipping them
25 when Element::E_inline_image
26 # remove all images by skipping them
27 when Element::E_path
28 # Set all paths to red color.
29 gs = element.GetGState()
30 gs.SetFillColorSpace(ColorSpace.CreateDeviceRGB())
31 gs.SetFillColor(ColorPt.new(1, 0, 0))
32 writer.WriteElement(element)
33 when Element::E_text # Process text strings...
34 # Set all text to blue color.
35 gs = element.GetGState()
36 gs.SetFillColorSpace(ColorSpace.CreateDeviceRGB())
37 cp = ColorPt.new(0, 0, 1)
38 gs.SetFillColor(cp)
39 writer.WriteElement(element)
40 when Element::E_form # Recursively process form XObjects
41 o = element.GetXObject()
42 map[o.GetObjNum()] = o
43 writer.WriteElement(element)
44 else
45 writer.WriteElement(element)
46 end
47 element = reader.Next()
48 end
49end
50
51 PDFNet.Initialize(PDFTronLicense.Key)
52
53 # Relative path to the folder containing the test files.
54 input_path = "../../TestFiles/"
55 output_path = "../../TestFiles/Output/"
56 input_filename = "newsletter.pdf"
57 output_filename = "newsletter_edited.pdf"
58
59
60 # Open the test file
61 puts "Opening the input file..."
62 doc = PDFDoc.new(input_path + input_filename)
63 doc.InitSecurityHandler()
64
65 writer = ElementWriter.new()
66 reader = ElementReader.new()
67
68 itr = doc.GetPageIterator()
69
70 while itr.HasNext() do
71 page = itr.Current()
72 reader.Begin(page)
73 writer.Begin(page, ElementWriter::E_replacement, false)
74 map1 = {}
75 ProcessElements(reader, writer, map1)
76 writer.End()
77 reader.End()
78
79 map2 = {}
80 while (not(map1.empty? and map2.empty?)) do
81 map1.each do |k, v|
82 obj = v
83 writer.Begin(obj)
84 reader.Begin(obj, page.GetResourceDict())
85 ProcessElements(reader, writer, map2)
86 reader.End()
87 writer.End()
88
89 map1.delete(k)
90 end
91 if (map1.empty? and not map2.empty?)
92 map1.update(map2)
93 map2.clear
94 end
95 end
96 itr.Next()
97 end
98
99 doc.Save(output_path + output_filename, SDFDoc::E_remove_unused)
100 doc.Close()
101 PDFNet.Terminate
102 puts "Done. Result saved in " + output_filename + "..."

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales