How to get PDF page size
PdfPage.Size() returns the width and height in points, as a viewer would show the
page.
pdfReader, f, err := model.NewPdfReaderFromFile("input.pdf", nil)
if err != nil {
return err
}
defer f.Close()
page, err := pdfReader.GetPage(1)
if err != nil {
return err
}
width, height, err := page.Size()
if err != nil {
return err
}
fmt.Printf("%.0f x %.0f points\n", width, height)Points, not pixels: 612 by 792 is US Letter, 595 by 842 is A4. Divide by 72 for inches.
Rotation is already applied. A page with /Rotate 90 reports its dimensions swapped,
which is why Size() is the right call rather than reading the media box yourself.
Pages are also allowed to inherit the media box from a parent node in the page tree
instead of carrying one, and GetMediaBox walks up to find it, so Size() still
answers for pages that define nothing themselves. A file where no ancestor defines one
is malformed and returns “media box not defined”.
Size can differ per page, so do not read page one and assume the rest match.
The media box is the full sheet. If a page also has a crop box, that smaller rectangle
is what a viewer displays and what rendering produces, while Size() keeps reporting
the media box. page.CropBox is nil when there is none, and Width() and Height()
on the rectangle give its dimensions.