Some test text!

Loading...
Guides

Go PDF editor (programmatic)

More languages

More languages
JavaScript
Java (Android)
C++
C#
C# (.NET Core)
Go
Java
Kotlin
Obj-C
JS (Node.js)
PHP
Python
Ruby
Swift
C# (UWP)
VB
C# (Xamarin)

Sample Go code for using PDFTron 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 PDFTron SDK's other functions for programmatically editing PDFs include the Cos/SDF low-level API, page manipulation, and more. Learn more about our Go PDF Library and PDF Editing & Manipulation Library.

Get Started Samples Download

To run this sample, get started with a free trial of PDFTron SDK.

//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
// Consult LICENSE.txt regarding license information.
//---------------------------------------------------------------------------------------

package main
import (
	"fmt"
	. "pdftron"
)

import  "pdftron/Samples/LicenseKey/GO"

//---------------------------------------------------------------------------------------
// 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, changes path fill color to red, and changes text color to blue. 
//---------------------------------------------------------------------------------------

func ProcessElements(reader ElementReader, writer ElementWriter, omap map[uint]Obj){
    element := reader.Next()     // Read page contents
    for element.GetMp_elem().Swigcptr() != 0{
        etype := element.GetType()
        if etype == ElementE_image{
            // remove all images by skipping them
        }else if etype == ElementE_inline_image{            
            // remove all images by skipping them
        }else if etype == ElementE_path{
            // Set all paths to red color.
            gs := element.GetGState()
            gs.SetFillColorSpace(ColorSpaceCreateDeviceRGB())
            gs.SetFillColor(NewColorPt(1.0, 0.0, 0.0))
            writer.WriteElement(element)
        }else if etype == ElementE_text{    // Process text strings...
            // Set all text to blue color.
            gs := element.GetGState()
            gs.SetFillColorSpace(ColorSpaceCreateDeviceRGB())
            cp := NewColorPt(0.0, 0.0, 1.0)
            gs.SetFillColor(cp)
            writer.WriteElement(element)
        }else if etype == ElementE_form{    // Recursively process form XObjects
            o := element.GetXObject()
            omap[o.GetObjNum()] = o
            writer.WriteElement(element)
        }else{
            writer.WriteElement(element)
		}
        element = reader.Next()
	}
}

func main(){
    PDFNetInitialize(PDFTronLicense.Key)
    
    // Relative path to the folder containing the test files.
    inputPath := "../../TestFiles/"
    outputPath := "../../TestFiles/Output/"
    inputFilename := "newsletter.pdf"
    outputFilename := "newsletter_edited.pdf"
    
    
    // Open the test file
    fmt.Println("Opening the input file...")
    doc := NewPDFDoc(inputPath + inputFilename)
    doc.InitSecurityHandler()
    
    writer := NewElementWriter()
    reader := NewElementReader()
    
    itr := doc.GetPageIterator()
    
    for itr.HasNext(){
        page := itr.Current()
        reader.Begin(page)
        writer.Begin(page, ElementWriterE_replacement, false)
        var map1 = make(map[uint]Obj)
        ProcessElements(reader, writer, map1)
        writer.End()
        reader.End()
		
        var map2 = make(map[uint]Obj)
        for !(len(map1) == 0 && len(map2) == 0){
            for k, v := range map1{
                obj := v
                writer.Begin(obj)
                reader.Begin(obj, page.GetResourceDict())
                ProcessElements(reader, writer, map2)
                reader.End()
                writer.End()
                delete(map1, k)
			}
            if (len(map1) == 0 && len(map2) != 0){
                //map1.update(map2)
				for key, value := range map2{         
					map1[key] = value 
				}
				//map2.clear()
				for k := range map2 {
					delete(map2, k)
				}
			}
		}
        itr.Next()
    }
	
    doc.Save(outputPath + outputFilename, uint(SDFDocE_remove_unused))
    doc.Close()
    PDFNetTerminate()
    fmt.Println("Done. Result saved in " + outputFilename +"...")
}