Pages

Page and pages

A page is a simple structure:

type Page struct {
	Dict Dict // Additional dictionary entries such as "/Trimbox"
	Annotations []Annotation
	Faces       []*Face
	Images      []*Imagefile
	// Patterns maps a per-page-unique resource name (without the leading
	// slash) to the indirect Pattern object returned by
	// PDF.WriteShadingPattern. Entries land in /Resources/Pattern; the
	// renderer (e.g. svgreader) refers to them as "/<name> scn" inside the
	// content stream.
	Patterns map[Name]*Object
	// ExtGStates maps a resource name (without the leading slash) to the
	// indirect graphics state parameter dictionary returned by
	// PDF.WriteExtGState. Entries land in /Resources/ExtGState; content
	// streams activate them with "/<name> gs".
	ExtGStates map[Name]*Object
	Objnum     Objectnumber // The "/Page" object
	Width      float64
	Height     float64
	OffsetX    float64
	OffsetY    float64
}

Pages are split into two parts in the PDF, a dictionary and a content stream. The Objnum points to the dictionary.

A page is created and added to the PDF with

func (pw *PDF) AddPage(content *Object, page Objectnumber) *Page

When you create a page with AddPage(), you can pass pdf.Objectnumber(0) for the second argument. AddPage() creates the dictionary object itself.

You need to register all used Annotations, Faces and Images to the page object in order to get the contents structure correct.

Permuting pages

After all pages have been collected, you can re-arrange the pages in the PDF file by changing the Pages slice in the Pages object which the main PDF object has a reference to.

type Pages struct {
	Pages []*Page
}

For example you can do something like this:

pw.Pages.Pages[0], pw.Pages.Pages[1] = pw.Pages.Pages[1], pw.Pages.Pages[0]

to swap the first two pages. pw in this example is the main PDF object.

Writing pages to the PDF

The PDF backend takes care of writing all the Page objects and the Pages object to the PDF. No user action is required.