Edit PDF� - Go 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-2021 by PDFTron Systems Inc. All Rights Reserved.
3// Consult LICENSE.txt regarding license information.
4//---------------------------------------------------------------------------------------
5
6package main
7import (
8 "fmt"
9 . "pdftron"
10)
11
12import "pdftron/Samples/LicenseKey/GO"
13
14//---------------------------------------------------------------------------------------
15// The sample code shows how to edit the page display list and how to modify graphics state
16// attributes on existing Elements. In particular the sample program strips all images from
17// the page, changes path fill color to red, and changes text color to blue.
18//---------------------------------------------------------------------------------------
19
20func ProcessElements(reader ElementReader, writer ElementWriter, omap map[uint]Obj){
21 element := reader.Next() // Read page contents
22 for element.GetMp_elem().Swigcptr() != 0{
23 etype := element.GetType()
24 if etype == ElementE_image{
25 // remove all images by skipping them
26 }else if etype == ElementE_inline_image{
27 // remove all images by skipping them
28 }else if etype == ElementE_path{
29 // Set all paths to red color.
30 gs := element.GetGState()
31 gs.SetFillColorSpace(ColorSpaceCreateDeviceRGB())
32 gs.SetFillColor(NewColorPt(1.0, 0.0, 0.0))
33 writer.WriteElement(element)
34 }else if etype == ElementE_text{ // Process text strings...
35 // Set all text to blue color.
36 gs := element.GetGState()
37 gs.SetFillColorSpace(ColorSpaceCreateDeviceRGB())
38 cp := NewColorPt(0.0, 0.0, 1.0)
39 gs.SetFillColor(cp)
40 writer.WriteElement(element)
41 }else if etype == ElementE_form{ // Recursively process form XObjects
42 o := element.GetXObject()
43 omap[o.GetObjNum()] = o
44 writer.WriteElement(element)
45 }else{
46 writer.WriteElement(element)
47 }
48 element = reader.Next()
49 }
50}
51
52func main(){
53 PDFNetInitialize(PDFTronLicense.Key)
54
55 // Relative path to the folder containing the test files.
56 inputPath := "../../TestFiles/"
57 outputPath := "../../TestFiles/Output/"
58 inputFilename := "newsletter.pdf"
59 outputFilename := "newsletter_edited.pdf"
60
61
62 // Open the test file
63 fmt.Println("Opening the input file...")
64 doc := NewPDFDoc(inputPath + inputFilename)
65 doc.InitSecurityHandler()
66
67 writer := NewElementWriter()
68 reader := NewElementReader()
69
70 itr := doc.GetPageIterator()
71
72 for itr.HasNext(){
73 page := itr.Current()
74 reader.Begin(page)
75 writer.Begin(page, ElementWriterE_replacement, false)
76 var map1 = make(map[uint]Obj)
77 ProcessElements(reader, writer, map1)
78 writer.End()
79 reader.End()
80
81 var map2 = make(map[uint]Obj)
82 for !(len(map1) == 0 && len(map2) == 0){
83 for k, v := range map1{
84 obj := v
85 writer.Begin(obj)
86 reader.Begin(obj, page.GetResourceDict())
87 ProcessElements(reader, writer, map2)
88 reader.End()
89 writer.End()
90 delete(map1, k)
91 }
92 if (len(map1) == 0 && len(map2) != 0){
93 //map1.update(map2)
94 for key, value := range map2{
95 map1[key] = value
96 }
97 //map2.clear()
98 for k := range map2 {
99 delete(map2, k)
100 }
101 }
102 }
103 itr.Next()
104 }
105
106 doc.Save(outputPath + outputFilename, uint(SDFDocE_remove_unused))
107 doc.Close()
108 PDFNetTerminate()
109 fmt.Println("Done. Result saved in " + outputFilename +"...")
110}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales