Available Now: Explore our latest release with enhanced accessibility and powerful IDP features

Apryse SDK Support for Go Language

By Apryse | 2024 Oct 21

Sanity Image
Read time

3 min

The Go Language/GoLang has been around for 15 years and is increasing in popularity. According to Tiobe, it is now the 8th most used programming language (with Rust at #13 and PHP at #15).

The Apryse SDK has supported Go for more than three years with precompiled, wrappers being available for download. That makes it easy for Go Developers to add Apryse’s leading, high-quality document creation, editing, printing, conversion (and much more) to their applications whether they are using Windows, Linux, and Mac.

For detailed guides, sample codes, and other resources, check out our Apryse SDK Go documentation.

What’s Supported with Apryse SDK for GoLang

Copied to clipboard

The Go PDF library package supports all of the Apryse SDK rich document processing functionalities, including:

Getting Started with Apryse SDK and GoLang

Copied to clipboard

The Go PDF Library is available for Windows, macOS and Linux. It means that you can start using the Apryse SDK and GoLang with just a few lines of code.

If you use the library in production then you'll need a commercial license, but for development work you can get a free trial license.

In this article we will look at using it within WSL and Ubuntu 24.04, running Go 1.22.2. The Apryse SDK was downloaded using

go get github.com/pdftron/pdftron-go/v2

and I then moved the files to /usr/lib/go-1.22/src/pdftron.

There is no need to install the library at that location, but if you place it elsewhere then you will need to update the references accordingly.

Note that the same download contains executables for multiple frameworks, so you may wish to delete the files that you do not need.

An image showing the location containing the Apryse SDK.

The location where I placed the Apryse SDK, showing the executables for a range of platforms.

Once installed, you’re good to go, with dozens of unique document processing capabilities at your fingertips. For example, you can generate PDFs from Office documents - and we will do that in a minute.

You can also create a PDF from scratch, and perform many other document processing actions to optimize and customize your document to taste.

How to Create a Linearized PDF Document with Apryse Go SDK

Copied to clipboard

As a first example of what can be done with the SDK, let's create a brand new, PDF document containing a blank page and linearize it for fast web view using the Apryse Go SDK. We will also add a page label (based on code taken from one of the shipped samples) just to demonstrate some additional functionality.

Create a file called linearized.go. In my case I placed it in a new folder called dev within my $HOME folder.

Next, copy in the following code - you will need to specify your license key.

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

func main(){
 // PDFNet must be initialized before accessing any PDFTron API
  PDFNetInitialize([Your license key]")
	doc := NewPDFDoc()
	page := doc.PageCreate() // Start a new page
	doc.PagePushBack(page)   // Add the page to document
	L1 := PageLabelCreate(doc.GetSDFDoc(), PageLabelE_roman_uppercase, "Page ", 5)
	doc.SetPageLabel(1, L1)
	doc.Save("output.pdf", uint(SDFDocE_linearized)) // Save the document as a linearized PDF
	doc.Close()
	fmt.Println("Finished")
}

Save the file then run it using go run linearized.go

In a few moments a new PDF will appear in the folder - if you open it in Acrobat or Xodo PDF Studio then you will see an empty page, but it does have a custom label - exactly as we wanted.

A view of the PDF generated using Go.

The PDF created using Go. Note how the page label says 'Page V', as specified in our code.

That's pretty awesome. Obviously, you can do much more with the SDK, so let's look at one of the conversion options.

How to Convert PDF to DOCX with Apryse Go SDK

Copied to clipboard

Let's look at converting PDF files into Office documents.

In order to do that you will need to download the Apryse Structured Output module. This is one of a number of add-on modules that the SDK can use - others include providing support for Data Extraction, and CAD.

You can extract the Structured Output archive where you wish. You will need to know where it is though, so that you can tell the SDK where to look for it.

In my case I'm copying the files to:

go-1.22/src/pdftron/Lib/Linux

When it's complete the file structure should be something like this:

Image showing the file structure after extracting the Structured Output module

Location of the Structured Output module.

Depending on its location you may also need to make it executable using chmod +x.

If I was using Windows then I would probably locate it at:

go/src/pdftron/Lib/Windows

Next, create a file called pdf2docx.go within the $HOME/dev folder. We are going to use that to convert a file called "input.pdf" into a DOCX file:

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

func main() {
  // PDFNet must be initialized before accessing any PDFTron API
  PDFNetInitialize("[Your license key]")
  PDFNetAddResourceSearchPath("/usr/lib/go-1.22/src/pdftron/Lib/")
  if !StructuredOutputModuleIsModuleAvailable() {
		fmt.Println("")
		fmt.Println("Unable to run the sample: Apryse Structured Output module not available.")
		fmt.Println("---------------------------------------------------------------")
		fmt.Println("The Structured Output module is an optional add-on, available for download")
		fmt.Println("at https://docs.apryse.com/core/info/modules/#structured-output-module")
		fmt.Println("")
		returnstructured-output-module
	}
	fmt.Println("Converting PDF to Word")
	infile := "./input.pdf"
	outfile := "./output.docx"
	ConvertToWord(infile, outfile)
	fmt.Println("Result saved in output.docx")
}

Make sure that the file input.pdf is available and enter

go run pdf2docx.go

Typical output if the PDF is successfully converted to Word.

Typical output if the PDF is successfully converted to Word.

In a few moments there will be a DOCX file in the folder. What you do with it is up to you. You could send to someone, store in a Database, or perform further document processing on it.

A view of the DOCX file created from the PDF.

The generated DOCX file. you can also see the other files in the Ubuntu folder.

The converted DOCX is superb - you still have the same layout as the PDF - but the file can now be easily edited using familiar tools, whether Office, LibreOffice or WebViewer.

Wrap Up

Copied to clipboard

Whether you are converting files or editing PDFs using GoLang, we’ve got you covered. Our Go Language package supports all of the Apryse SDK’s rich document processing features, such as converting Office documents to PDF (as well as PDF to Office!), while retaining the same style and formatting as the original document no matter your platform.

We hope you are as excited as we are about this new GoLang support. Check out the documentation and feel free to contact us on our Discord channel with any questions or suggestions!

Sanity Image

Apryse

Share this post

email
linkedIn
twitter