Skip to main content
Version: Version 1.3

Using other languages with the Pdftools SDK

Use the Pdftools SDK with Go, Python, and 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.

Bind the C library and header files

info

To bind the Pdftools SDK's C API to a different programming language, you require a compiled C library and header files. Download these files in the Pdftools SDK for C package.

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

Use the Pdftools SDK with Go

Learn how to use the Go programming language with the Pdftools SDK's C API 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.

The instructions assume that Go 1.6+ is installed on your system, and that the Go executable files are included in your path.

  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.

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

    go build
  2. 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.

Use the Pdftools SDK with Python

tip

Download sample projects that show you how to use the Pdftools SDK with Python. See our Code samples page. The samples include: