Go (C Interface Binding) new project with Apryse SDK PDF library integration

C interface binding example

This guide is a demo for using our C interface binding and is not a production version of the Go wrapper. Please visit our Apryse SDK for Go documentation for more information on using our production version of Go.

This guide will help you integrate a free trial of the Apryse SDK with Go using Apryse Headers on macOS, Linux or Windows in a new Go app. (Or see instructions for incorporation with an existing project.) Your free trial includes unlimited trial usage and support from solution engineers.

Prerequisites

Make sure Go is installed at /usr/local/go and /usr/local/go/bin.

  • Apryse SDK for macOS

  • Get your Apryse trial key.

License Key

Apryse collects some data regarding your usage of the SDK for product improvement.

If you wish to continue without data collection, contact us and we will email you a no-tracking trial key for you to get started.

License Key Required

The trial of Apryse SDK requires a trial license key, which is provided in the box titled "License Key". A commercial license key is required for use in a production environment. Please contact sales to purchase a commercial key or if you need any other license key assistance.

Keep your license keys confidential.

License keys are uniquely generated. Please make sure that it is not publicly available (e.g. in your public GitHub).

Setup

  1. Create a project directory called myApp.
  2. Extract the PDFNetCMac package as a folder into your project directory.

sh

1~/myApp
2~/myApp/PDFNetCMac
3~/myApp/PDFNetCMac/Headers
4~/myApp/PDFNetCMac/Lib
5...

Integrate Apryse SDK with Go

  1. In your project directory, create a new file called myApp.goAdd the following code inside:Save and close the file.Your folder should look similar to:

Go

1package main
2// #cgo CFLAGS: -IPDFNetCMac/Headers
3// #cgo LDFLAGS: -LPDFNetCMac/Lib -Wl,-rpath,PDFNetCMac/Lib -lPDFNetC
4// #include "C/PDF/TRN_PDFNet.h"
5// #include "C/SDF/TRN_SDFDoc.h"
6// #include "C/PDF/TRN_PDFDoc.h"
7// #include "C/PDF/TRN_Page.h"
8// #include "C/Common/TRN_UString.h"
9// #include "C/Common/TRN_Types.h"
10// #include <stdlib.h>
11// #include <stdio.h>
12import "C"
13import (
14 "fmt";
15 "unsafe"
16)
17func main() {
18 // PDFNet must be initialized before calling other Apryse API
19 ccp := C.CString("YOUR_DEMO_KEY")
20 C.TRN_PDFNetInitialize(ccp)
21
22 // Create a new PDFDoc
23 var doc *C.TRN_PDFDoc
24 doc = (*C.TRN_PDFDoc)(C.malloc(C.size_t(unsafe.Sizeof(*doc))))
25 C.TRN_PDFDocCreate(doc)
26
27 // Allocate memory for a blank page
28 var page *C.TRN_Page
29 page = (*C.TRN_Page)(C.malloc(C.size_t(unsafe.Sizeof(*page))))
30
31 // Allocate memory for a default rectangle
32 var box *C.TRN_Rect
33 box = (*C.TRN_Rect)(C.malloc(C.size_t(unsafe.Sizeof(*box))))
34
35 // Create the page and add it to the document
36 C.TRN_PDFDocPageCreate(*doc, box, page)
37 C.TRN_PDFDocPagePushBack(*doc, *page)
38
39 // Save the document as a linearized PDF
40 var filepath C.TRN_UString
41 C.TRN_UStringCreateFromCharString(C.CString("output.pdf"), C.int(10),
42 C.e_UString_utf8, &filepath)
43 C.TRN_PDFDocSave(*doc, filepath, 0);
44 fmt.Println("Hello World!");
45}

sh

1~/myApp
2~/myApp/PDFNetCMac
3~/myApp/PDFNetCMac/Headers
4~/myApp/PDFNetCMac/Lib
5...
6~/myApp/myApp.go
  1. In the project folder, run:This will produce a myApp executable file.

sh

1go build
  1. Run the executable file. The executable will produce a pdf with a blank page in a linearized format.

Want to generate your own bindings?

You can generate bindings for Apryse SDK APIs using various methods. It is recommended to use SWIG to generate these bindings. The header files to include can be found in PDFNET_BASE\Headers\ and the library can be found in PDFNET_BASE\Lib\.

Next step

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales