/
ContentReplacerTest.go
70 lines (54 loc) · 2.39 KB
/
ContentReplacerTest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//---------------------------------------------------------------------------------------
// 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"
var inputPath = "../../TestFiles/"
var outputPath = "../../TestFiles/Output/"
//-----------------------------------------------------------------------------------------
// The sample code illustrates how to use the ContentReplacer class to make using
// 'template' pdf documents easier.
//-----------------------------------------------------------------------------------------
func main(){
PDFNetInitialize(PDFTronLicense.Key)
// Example 1) Update a business card template with personalized info
doc := NewPDFDoc(inputPath + "BusinessCardTemplate.pdf")
doc.InitSecurityHandler()
// first, replace the image on the first page
replacer := NewContentReplacer()
page := doc.GetPage(1)
img := ImageCreate(doc.GetSDFDoc(), inputPath + "peppers.jpg")
replacer.AddImage(page.GetMediaBox(), img.GetSDFObj())
// next, replace the text place holders on the second page
replacer.AddString("NAME", "John Smith")
replacer.AddString("QUALIFICATIONS", "Philosophy Doctor")
replacer.AddString("JOB_TITLE", "Software Developer")
replacer.AddString("ADDRESS_LINE1", "#100 123 Software Rd")
replacer.AddString("ADDRESS_LINE2", "Vancouver, BC")
replacer.AddString("PHONE_OFFICE", "604-730-8989")
replacer.AddString("PHONE_MOBILE", "604-765-4321")
replacer.AddString("EMAIL", "info@pdftron.com")
replacer.AddString("WEBSITE_URL", "http://www.pdftron.com")
// finally, apply
replacer.Process(page)
doc.Save(outputPath + "BusinessCard.pdf", uint(SDFDocE_linearized))
doc.Close()
fmt.Println("Done. Result saved in BusinessCard.pdf")
// Example 2) Replace text in a region with new text
doc = NewPDFDoc(inputPath + "newsletter.pdf")
doc.InitSecurityHandler()
replacer = NewContentReplacer()
page = doc.GetPage(1)
replacer.AddText(page.GetMediaBox(), "hello hello hello hello hello hello hello hello hello hello")
replacer.Process(page)
doc.Save(outputPath + "ContentReplaced.pdf", uint(SDFDocE_linearized))
doc.Close()
fmt.Println("Done. Result saved in ContentReplaced.pdf")
PDFNetTerminate()
fmt.Println("Done.")
}