PDF Processing API for Go

Generate, modify, secure, and convert PDF documents to various formats seamlessly within Go applications.

Overview

Aspose.PDF is a PDF document generation library for Go that makes creating complex, multi-page, printable documents easy.

API is designed to be simple, so generating complex documents has been as simple as a few function calls. As the name suggests, the library is developed based on program code in C++. This allowed us to make it as fast as possible and also made it portable to different operating systems.

Aspose.PDF for Go via C++ features

How to Manipulate PDF Files on the Backend with Aspose.PDF for Go via C++

Aspose.PDF for Go via C++ enables backend manipulation of PDF files. With Aspose.PDF for Go via C++, you can optimize PDF files, fix corrupted PDFs, add or extract text, and convert PDFs to popular formats.

Extract text from PDF

  • Using Aspose.PDF for Go via C++ you can extract text from PDF with formatting or as a raw data.

Convert document from PDF to other formats

The library allows to store PDF as

  • Microsoft Office documents (DOCX/DOC, XLSX, PPTX),
  • Images in BMP, PNG, JPEG, TIFF and SVG formats,
  • Various documents (XPS, EPUB, TeX/LaTex).

Organize PDF

Aspose.PDF for Go via C++ allows you to perform the following document organization functions:

  • Optimize PDF-document content,
  • Convert PDF document using grey shades,
  • Rotate whole pages in PDF document,
  • Set PDF document background color,
  • Repair PDF-document.

Page manipulation

Aspose.PDF for Go via C++ allows you to

  • Add new pages
  • Remove pages.

Convert PDF to JPG and Other Image Formats

Aspose.PDF for Go via C++ allows you to render and transform each page of a PDF file into conventional image formats such as BMP, JPG, and PNG with the highest possible fidelity. In addition to images, PDF files can easily be saved to DOC and DOCX formats.

Save PDF pages as JPEG images in Go

Load an existing PDF document. Convert PDF pages to images and save each page in JPG format.

Convert PDF to JPG using Go

package main

import (
    "fmt"
    "log"

    asposepdf "github.com/aspose-pdf/aspose-pdf-go-cpp"
)

const (
    inputPDFFile      = "sample.pdf"
    outputImagePrefix = "sample_page"
    imageQuality      = 300
)

func main() {
    if err := convertPDFToImages(inputPDFFile); err != nil {
        log.Fatal(err)
    }
}

func convertPDFToImages(pdfFile string) error {
    pdfDocument, err := asposepdf.Open(pdfFile)
    if err != nil {
        return fmt.Errorf("failed to open PDF file: %w", err)
    }
    defer pdfDocument.Close()

    pageCount, err := pdfDocument.PageCount()
    if err != nil {
        return fmt.Errorf("failed to get page count: %w", err)
    }

    for pageIndex := int32(1); pageIndex <= pageCount; pageIndex++ {
        if err := savePageAsImage(pdfDocument, pageIndex); err != nil {
            return err
        }
    }
    return nil
}

func savePageAsImage(pdfDocument *asposepdf.Document, pageIndex int32) error {
    imageFileName := fmt.Sprintf("%s%d.jpg", outputImagePrefix, pageIndex)
    if err := pdfDocument.PageToJpg(pageIndex, imageQuality, imageFileName); err != nil {
        return fmt.Errorf("failed to save page %d as image: %w", pageIndex, err)
    }
    return nil
}