Skip to main content
Version: Version 1.7

Get started with other programming languages

Use the Pdftools SDK with Go, Rust, and potentially many other programming languages by binding the C API. The Pdftools SDK includes a powerful C API for high-performance applications, which can be used with a wide range of programming languages not directly supported by the Pdftools SDK. This guide illustrates the binding of the C API on a Go example.

Pdftools customers use interface bindings to implement the Pdftools SDK with the following programming languages and frameworks:
  • Delphi
  • Go
  • Lua
  • Node.js
  • Perl
  • PHP
  • R
  • Ruby
  • Rust
note

Pdftools do not officially support and cannot guarantee the stability of such interface bindings. This approach requires you to be very familiar with your chosen language and how it integrates with C.

Prerequisites

Install Go 1.6 or higher on your system, and include the Go executable files in your path.

Use the Pdftools SDK with Go

Go provides a standard library package cgo which makes it easy to bind the Pdftools SDK's C API. Learn how to use the Go programming language to convert a PDF document to an image in the following example.

note

These instructions explain the process by which you create a Go wrapper for the Pdftools SDK. For simplicity, some functionality such as error handling is not shown in the example code below.

  1. Create a project directory named pdf2img.

  2. Extract the latest Pdftools SDK for C zip file into the pdf2img directory, then rename the extracted directory to PdftoolsSDK. The project structure looks like the following:

    pdf2img
    └── PdftoolsSDK
    ├── include
    └── lib
  3. Create a new file in the top-level project directory and name it pdf2img.go, and then add the following code inside it:

    package main

    // #cgo CFLAGS: -IPdftoolsSDK/include
    // #cgo LDFLAGS: -LPdftoolsSDK/lib/win-x64 -Wl,-rpath,PdftoolsSDK/lib/win-x64 -lPdfToolsSdk
    // #include "PdfTools_Platform.h"
    // #include "PdfTools_PdfTools.h"
    // #include "PdfTools_PdfToolsSys.h"
    // #include "PdfTools_PdfToolsPdf.h"
    // #include "PdfTools_PdfToolsImage.h"
    // #include "PdfTools_PdfToolsPdf2Image.h"
    // #include "PdfTools_PdfToolsPdf2ImageProfiles.h"
    // #include <stdlib.h>
    // #include <stdio.h>
    import "C"
    import (
    "os"
    "unsafe"
    )

    func main() {

    // Initialize the SDK
    C.PdfTools_Initialize()
    demokey := C.CString("<add-your-license-key-here>")
    C.PdfTools_Sdk_Initialize(demokey, nil)

    szInPath := C.CString(os.Args[1])
    szInParms := C.CString("rb")
    szOutPath := C.CString(os.Args[2])
    szOutParms := C.CString("wb+")
    var pInStream *C.FILE
    var pOutStream *C.FILE
    var pInDoc *C.TPdfToolsPdf_Document
    var pOutDoc *C.TPdfToolsImage_Document
    var pProfile *C.TPdfToolsPdf2ImageProfiles_Profile
    var pConverter *C.TPdfToolsPdf2Image_Converter

    // Open input document
    pInStream = C.fopen(szInPath, szInParms)
    var inDesc *C.TPdfToolsSys_StreamDescriptor
    inDesc = (*C.TPdfToolsSys_StreamDescriptor)(C.malloc(C.size_t(unsafe.Sizeof(*inDesc))))
    C.PdfToolsSysCreateFILEStreamDescriptor(inDesc, pInStream, 0)
    pInDoc = C.PdfToolsPdf_Document_Open(inDesc, C.CString(""))

    // Create output stream for writing
    pOutStream = C.fopen(szOutPath, szOutParms)
    var outDesc *C.TPdfToolsSys_StreamDescriptor
    outDesc = (*C.TPdfToolsSys_StreamDescriptor)(C.malloc(C.size_t(unsafe.Sizeof(*outDesc))))
    C.PdfToolsSysCreateFILEStreamDescriptor(outDesc, pOutStream, 0)

    // Create the profile that defines the conversion parameters.
    // The Archive profile converts PDF documents to TIFF images for archiving.
    pProfile = (*C.TPdfToolsPdf2ImageProfiles_Profile)(C.PdfToolsPdf2ImageProfiles_Archive_New())

    // Convert the PDF document to an image document
    pConverter = C.PdfToolsPdf2Image_Converter_New()
    pOutDoc =
    (*C.TPdfToolsImage_Document)(C.PdfToolsPdf2Image_Converter_ConvertDocument(pConverter, pInDoc, outDesc, pProfile))

    // Clean up resources
    if pOutDoc != nil {
    C.PdfToolsImage_Document_Close(pOutDoc)
    }
    C.PdfTools_Release(unsafe.Pointer(pConverter))
    C.PdfTools_Release(unsafe.Pointer(pProfile))
    if pOutStream != nil {
    C.fclose(pOutStream)
    }
    if pInDoc != nil {
    C.PdfToolsPdf_Document_Close(pInDoc)
    }
    if pInStream != nil {
    C.fclose(pInStream)
    }
    C.free(unsafe.Pointer(szInPath))
    C.free(unsafe.Pointer(szInParms))
    C.free(unsafe.Pointer(szOutPath))
    C.free(unsafe.Pointer(szOutParms))
    C.PdfTools_Uninitialize()
    }
    tip

    The output file is watermarked by default. If you have a valid license key, replace <add-your-license-key-here> with your Pdftools SDK license key.

  4. Open a terminal in the top-level project directory (~/pdf2img) and run:

    go build
  5. An executable file named pdf2img is created in the top-level project directory. Test the executable by running the command:

    pdf2img <path to input PDF file> <path to output image file>

    For example, place a PDF file named in.pdf in the top-level project directory and run:

    pdf2img in.pdf out.tif

    An output image file named out.tif is created in the top-level project directory.

Bind the C library and header files

Bind the Pdftools SDK's C API to a different programming language using the native C library and header files. Click the download link to get the Pdftools SDK for C package.

The method for using the native C library and header files differs slightly for each language.

tip

Without a valid license key the output files are watermarked. Get in touch with the Pdftools sales team through the Contact page to get a full license.