Examples

Starting from an example can speed up code development significantly.

pdf_annotate_add_ellipse.go
            /*
 * Annotate/mark up pages of a PDF file.
 * Add a circle/ellipse annotation to a specified location on a page.
 *
 * Run as: go run pdf_annotate_add_ellipse.go input.pdf <page> <x> <y> <width> <height> output.pdf
 * The x, y, width and height coordinates are in the PDF coordinate's system, where 0,0 is in the lower left corner.
 */

package main

import (
	"fmt"
	"os"

	"strconv"

	unicommon "github.com/unidoc/unidoc/common"
	"github.com/unidoc/unidoc/pdf/annotator"
	pdf "github.com/unidoc/unidoc/pdf/model"
)

func main() {
	// Debug log mode.
	//unicommon.SetLogger(unicommon.NewConsoleLogger(unicommon.LogLevelDebug))

	if len(os.Args) < 8 {
		fmt.Printf("go run pdf_annotate_add_ellipse.go input.pdf <page> <x> <y> <xRad> <yRad> output.pdf\n")
		os.Exit(1)
	}

	inputPath := os.Args[1]

	pageNum, err := strconv.ParseInt(os.Args[2], 10, 64)
	if err != nil {
		fmt.Printf("Error: %v\n", err)
		os.Exit(1)
	}

	x, err := strconv.ParseFloat(os.Args[3], 64)
	if err != nil {
		fmt.Printf("Error: %v\n", err)
		os.Exit(1)
	}

	y, err := strconv.ParseFloat(os.Args[4], 64)
	if err != nil {
		fmt.Printf("Error: %v\n", err)
		os.Exit(1)
	}

	width, err := strconv.ParseFloat(os.Args[5], 64)
	if err != nil {
		fmt.Printf("Error: %v\n", err)
		os.Exit(1)
	}

	height, err := strconv.ParseFloat(os.Args[6], 64)
	if err != nil {
		fmt.Printf("Error: %v\n", err)
		os.Exit(1)
	}

	outputPath := os.Args[7]

	err = annotatePdfAddEllipseAnnotation(inputPath, pageNum, outputPath, x, y, width, height)
	if err != nil {
		fmt.Printf("Error: %v\n", err)
		os.Exit(1)
	}

	fmt.Printf("Complete, see output file: %s\n", outputPath)
}

// Annotate pdf file.
func annotatePdfAddEllipseAnnotation(inputPath string, pageNum int64, outputPath string, x, y, width, height float64) error {
	unicommon.Log.Debug("Input PDF: %v", inputPath)

	pdfWriter := pdf.NewPdfWriter()

	// Read the input pdf file.
	f, err := os.Open(inputPath)
	if err != nil {
		return err
	}
	defer f.Close()

	pdfReader, err := pdf.NewPdfReader(f)
	if err != nil {
		return err
	}

	numPages, err := pdfReader.GetNumPages()
	if err != nil {
		return err
	}

	for i := 0; i < numPages; i++ {
		// Read the page.
		page, err := pdfReader.GetPage(i + 1)
		if err != nil {
			return err
		}

		// Add only to the specific page.
		if int64(i+1) == pageNum {
			// Define a semi-transparent yellow rectangle with black borders at the specified location.

			circDef := annotator.CircleAnnotationDef{}
			circDef.X = x
			circDef.Y = y
			circDef.Width = width
			circDef.Height = height
			circDef.Opacity = 0.5 // Semi transparent.
			circDef.FillEnabled = true
			circDef.FillColor = pdf.NewPdfColorDeviceRGB(1, 1, 0) // Yellow fill.
			circDef.BorderEnabled = true
			circDef.BorderWidth = 15
			circDef.BorderColor = pdf.NewPdfColorDeviceRGB(0, 0, 0) // Black border.

			circAnnotation, err := annotator.CreateCircleAnnotation(circDef)
			if err != nil {
				return err
			}

			// Add to the page annotations.
			page.Annotations = append(page.Annotations, circAnnotation)
		}

		err = pdfWriter.AddPage(page)
		if err != nil {
			unicommon.Log.Error("Failed to add page: %s", err)
			return err
		}
	}

	fWrite, err := os.Create(outputPath)
	if err != nil {
		return err
	}

	defer fWrite.Close()

	err = pdfWriter.Write(fWrite)
	if err != nil {
		return err
	}

	return nil
}

        

View on GitHub: https://github.com/unidoc/unidoc-examples/blob/master/pdf/annotations/pdf_annotate_add_ellipse.go