Skip to content

Foxit PDF SDK Go Library

This section describes how to run samples and create a basic project using the Foxit PDF SDK Go library. The project demonstrates rendering the first page of a PDF document to a bitmap and saving it as a JPG image.

Prerequisites

Development environment

  • Install Go: Ensure Go version ≥ 1.18.
  • Foxit GO Library: Request a trial via the Foxit Developer Hub. Download and install the library that matches your OS and architecture.

System support

We provide detailed system support information for Linux and Mac, including operating system versions and compiler requirements. Select your platform to view the details.

LinuxMac

Configuration

Configure environment variables

After downloading the Foxit PDF SDK Go library, configure environment variables so the system can locate the dynamic libraries. Choose the method for your platform:

Linux:

  • x64 systems: Run the following command to set the library path:
    bash
    export LD_LIBRARY_PATH=/home/user/Desktop/foxitpdfsdk_xx_x_linux_go/lib/x64:$LD_LIBRARY_PATH
  • x32 systems: Run the following command to set the library path:
    bash
    export LD_LIBRARY_PATH=/home/user/Desktop/foxitpdfsdk_xx_x_linux_go/lib/x86:$LD_LIBRARY_PATH

macOS:

  • x64 systems: Run the following command to set the library path:
    bash
    export DYLD_LIBRARY_PATH=/Users/user/Desktop/foxitpdfsdk_xx_x_mac_go/lib:$DYLD_LIBRARY_PATH

NOTE

  • Replace the paths in the commands above with your actual SDK installation path.
  • Consider adding these environment variables to your shell configuration file (such as ~/.bashrc or ~/.zshrc) for persistence.
  • After setting them, reopen the terminal or run source ~/.bashrc for the changes to take effect.

Run samples

Run all samples

  • See Samples for an overview of the sample projects, dependency information for specific samples, and instructions for running samples from the command line.

Quick start

  1. Create a project directory:

    • Create a folder named test as the project directory.
  2. Prepare a PDF file:

    • Copy SamplePDF.pdf from /example/simple_demo/input_files into the test folder.
  3. Copy library files:

    • Linux x86/x64 users: Copy the lib folder from foxitpdfsdk_xx_x_linux_go into the test project folder.
    • Mac x64 users: Copy the lib folder from foxitpdfsdk_xx_x_mac_go into the test project folder.
  4. Set the library path:

  5. Create a Go file:

    • Add the following Go file test.go in the test folder.

    Note:

    • In test.go, set sn to the string after "SN=" in gsdk_sn.txt.
    • In test.go, set key to the string after "Sign=" in gsdk_key.txt.
    [test.go]
    go
    import (
        "fmt"
        . "foxit.com/fsdk"
    )
    
    // Assuming PDFDoc doc has been loaded.
    // The value of "sn" can be got from "gsdk_sn.txt" (the string after "SN=").
    // The value of "key" can be got from "gsdk_key.txt" (the string after "Sign=").
    const (
        sn = " "
        key = " "
    )
    
    func main() int {
        code := LibraryInitialize(sn, key)
        if code != E_ErrSuccess {
            return 0
        }
    
        // Load a PDF document, and parse the first page of the document.
        doc := NewPDFDoc("SamplePDF.pdf")
        defer DeletePDFDoc(doc)
        error_code := doc.Load("")
        if error_code != E_ErrSuccess {
            return 0
        }
    
        page := doc.GetPage(0)
        page.StartParse(PDFPageE_ParsePageNormal)
    
        width := int(page.GetWidth())
        height := int(page.GetHeight())
        matrix := page.GetDisplayMatrix(0, 0, width, height, page.GetRotation())
    
        // Prepare a bitmap for rendering.
        bitmap := NewBitmap(width, height, BitmapE_DIBArgb)
        bitmap.FillRect(0xFFFFFFFF)
        // Render page.
        render := Renderer(bitmap, false)
        render.StartRender(page, matrix)
    
        // Add the bitmap to image and save the image.
        img := NewImage()
        img.AddFrame(bitmap)
        img.SaveAs("testpage.jpg")
        LibraryRelease()
        return 0
    }
  6. Initialize the Go module:

    • In the root of the test project, open a terminal and run:
    bash
    go mod init test
  7. Edit the Go module:

    • For Linux x64, for example:
    bash
    go mod edit -replace foxit.com/fsdk=./lib/x64
    go mod edit -require foxit.com/[email protected]
  8. Tidy Go dependencies:

    bash
    go mod tidy
  9. Run the project:

    bash
    go run test.go
    • After a successful run, testpage.jpg is generated in the current folder.