Add Image to PDF - Go Sample Code

Sample code to use Apryse SDK for programmatically inserting various raster image formats (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) into a PDF document. Sample code provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby and VB.

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// This sample illustrates how to embed various raster image formats
16// (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.
17//
18// Note: On Windows platform this sample utilizes GDI+ and requires GDIPLUS.DLL to
19// be present in the system path.
20//-----------------------------------------------------------------------------------
21
22func main(){
23 PDFNetInitialize(PDFTronLicense.Key)
24 // Relative path to the folder containing test files.
25 var inputPath = "../../TestFiles/"
26 var outputPath = "../../TestFiles/Output/"
27 doc := NewPDFDoc()
28 f := NewElementBuilder() // Used to build new Element objects
29 writer := NewElementWriter() // Used to write Elements to the page
30 page := doc.PageCreate() // Start a new page
31 writer.Begin(page) // Begin writing to this page
32 // ----------------------------------------------------------
33 // Add JPEG image to the output file
34 img := ImageCreate(doc.GetSDFDoc(), inputPath + "peppers.jpg")
35 element := f.CreateImage(img, 50.0, 500.0, float64(img.GetImageWidth()/2), float64(img.GetImageHeight()/2))
36 writer.WritePlacedElement(element)
37
38 // ----------------------------------------------------------
39 // Add a PNG image to the output file
40 img = ImageCreate(doc.GetSDFDoc(), inputPath + "butterfly.png")
41 element = f.CreateImage(img, NewMatrix2D(100.0, 0.0, 0.0, 100.0, 300.0, 500.0))
42 writer.WritePlacedElement(element)
43
44 //----------------------------------------------------------
45 // Add a GIF image to the output file
46 img = ImageCreate(doc.GetSDFDoc(), inputPath + "pdfnet.gif")
47 element = f.CreateImage(img, NewMatrix2D(float64(img.GetImageWidth()), 0.0, 0.0, float64(img.GetImageHeight()), 50.0, 350.0))
48 writer.WritePlacedElement(element)
49
50 // ----------------------------------------------------------
51 // Add a TIFF image to the output file
52
53 img = ImageCreate(doc.GetSDFDoc(), (inputPath + "grayscale.tif"))
54 element = f.CreateImage(img, NewMatrix2D(float64(img.GetImageWidth()), 0.0, 0.0, float64(img.GetImageHeight()), 10.0, 50.0))
55 writer.WritePlacedElement(element)
56
57 writer.End() // Save the page
58 doc.PagePushBack(page) // Add the page to the document page sequence
59
60 // ----------------------------------------------------------
61 // Embed a monochrome TIFF. Compress the image using lossy JBIG2 filter.
62 page = doc.PageCreate(NewRect(0.0, 0.0, 612.0, 794.0))
63 writer.Begin(page) // begin writing to this page
64
65 // Note: encoder hints can be used to select between different compression methods.
66 // For example to instruct PDFNet to compress a monochrome image using JBIG2 compression.
67 hintSet := NewObjSet();
68 enc := hintSet.CreateArray(); // Initilaize encoder 'hint' parameter
69 enc.PushBackName("JBIG2");
70 enc.PushBackName("Lossy");
71
72 img = ImageCreate(doc.GetSDFDoc(), inputPath + "multipage.tif");
73 element = f.CreateImage(img, NewMatrix2D(612.0, 0.0, 0.0, 794.0, 0.0, 0.0));
74 writer.WritePlacedElement(element);
75
76 writer.End() // Save the page
77 doc.PagePushBack(page) // Add the page to the document page sequence
78
79 // ----------------------------------------------------------
80 // Add a JPEG2000 (JP2) image to the output file
81
82 // Create a new page
83 page = doc.PageCreate()
84 writer.Begin(page) // Begin writing to the page
85
86 // Embed the image
87 img = ImageCreate(doc.GetSDFDoc(), inputPath + "palm.jp2")
88
89 // Position the image on the page
90 element = f.CreateImage(img, NewMatrix2D(float64(img.GetImageWidth()), 0.0, 0.0, float64(img.GetImageHeight()), 96.0, 80.0))
91 writer.WritePlacedElement(element)
92
93 // Write 'JPEG2000 Sample' text string under the image
94 writer.WriteElement(f.CreateTextBegin(FontCreate(doc.GetSDFDoc(), FontE_times_roman), 32.0))
95 element = f.CreateTextRun("JPEG2000 Sample")
96 element.SetTextMatrix(1.0, 0.0, 0.0, 1.0, 190.0, 30.0)
97 writer.WriteElement(element)
98 writer.WriteElement(f.CreateTextEnd())
99
100 writer.End()
101 doc.PagePushBack(page)
102
103 doc.Save((outputPath + "addimage.pdf"), uint(SDFDocE_linearized));
104 doc.Close()
105
106 PDFNetTerminate()
107 fmt.Println("Done. Result saved in addimage.pdf...")
108}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales