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.
LinuxMacConfiguration
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
~/.bashrcor~/.zshrc) for persistence. - After setting them, reopen the terminal or run
source ~/.bashrcfor 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
Create a project directory:
- Create a folder named
testas the project directory.
- Create a folder named
Prepare a PDF file:
- Copy
SamplePDF.pdffrom/example/simple_demo/input_filesinto thetestfolder.
- Copy
Copy library files:
- Linux x86/x64 users: Copy the
libfolder fromfoxitpdfsdk_xx_x_linux_gointo thetestproject folder. - Mac x64 users: Copy the
libfolder fromfoxitpdfsdk_xx_x_mac_gointo thetestproject folder.
- Linux x86/x64 users: Copy the
Set the library path:
- Refer to Configure environment variables and set the library path in your environment variables, replacing only the library path.
Create a Go file:
- Add the following Go file
test.goin thetestfolder.
Note:
- In
test.go, setsnto the string after "SN=" ingsdk_sn.txt. - In
test.go, setkeyto the string after "Sign=" ingsdk_key.txt.
[test.go]
goimport ( "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 }- Add the following Go file
Initialize the Go module:
- In the root of the
testproject, open a terminal and run:
bashgo mod init test- In the root of the
Edit the Go module:
- For Linux x64, for example:
bashgo mod edit -replace foxit.com/fsdk=./lib/x64 go mod edit -require foxit.com/[email protected]Tidy Go dependencies:
bashgo mod tidyRun the project:
bashgo run test.go- After a successful run,
testpage.jpgis generated in the current folder.
- After a successful run,