Existing Go (C Interface Binding) 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 an existing Go app to create a new PDFDoc. (Or see instructions for creating a new Go app.)

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).

Integrate Apryse SDK with Go

Start by adding these lines to the beginning of your .go file:

Go

1// #cgo CFLAGS: -Ipath/to/your/download/PDFNetCMac/Headers
2// #cgo LDFLAGS: -Lpath/to/your/download/PDFNetCMac/Lib -Wl,-rpath,PDFNetC64/Lib -lPDFNetC

Before calling other Apryse API, you must initialize PDFNet. The PDFNet initialize header is found in C/PDF/TRN_PDFNet.h and the PDFDoc header is found in C/PDF/TRN_PDFDoc.h. Include both at the beginning of your .go file:

Go

1// #include "C/PDF/TRN_PDFNet.h"
2// #include "C/PDF/TRN_PDFDoc.h"

Also import the unsafe library:

Go

1import (
2 "unsafe"
3)

Then initialize PDFNet by calling these lines:

Go

1// Declare a license key and convert it to CString
2ccp := C.CString("Insert commercial license key here after purchase");
3// Initialize PDFNet
4C.TRN_PDFNetInitialize(ccp);
5// Free the memory used for the license key
6C.free(unsafe.Pointer(ccp));

To create a new PDFDoc in Go, you must allocate memory for it and handle freeing the memory associated with it.

You can follow these steps to instantiate a PDFDoc:

Go

1// Declare a pointer for the PDFDoc.
2var doc *C.TRN_PDFDoc;
3
4// Get the size of the type using the pointer and allocate that amount of memory using malloc,
5// then typecast void pointer returned by malloc to the required type explicitly.
6doc = (*C.TRN_PDFDoc)(C.malloc(C.size_t(unsafe.Sizeof(*doc))));
7
8// Call `TRN_PDFDocCreate` on the pointer to create the PDFDoc.
9C.TRN_PDFDocCreate(doc);

To deallocate the memory used by the PDFDoc, you can use C.free:

Go

1C.free(doc);

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