Skip to content

Correct MediaBox and CropBox sizes and fix #1330#1340

Merged
BobLd merged 1 commit into
UglyToad:masterfrom
BobLd:issues/1330
Jun 22, 2026
Merged

Correct MediaBox and CropBox sizes and fix #1330#1340
BobLd merged 1 commit into
UglyToad:masterfrom
BobLd:issues/1330

Conversation

@BobLd

@BobLd BobLd commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

Tidy up and correct MediaBox and CropBox sizes and fix #1330


Breaking change:

Page.MediaBox / Page.CropBox now use document (unrotated) coordinates

Page.MediaBox.Bounds and Page.CropBox.Bounds now return the rectangles exactly as defined in the PDF, in unrotated default user space (ISO 32000‑2 §14.11.2), instead of rectangles that were pre‑rotated and shifted to the origin. The crop box is also now clipped to the media box.

Unchanged: Page.Width, Page.Height, Page.Size, and Page.Letters behave exactly as before.

This only affects rotated or cropped/offset pages. For a normal page (crop box = media box, at the origin, no rotation) the values are identical, so most code needs no change.

How to adapt

  • You read page.Width / page.Height → nothing to do; they still return the visible (rotation‑applied) size in points.
  • You need the page's on‑screen size but were reading the boxes → switch to Width/Height:
// before
var w = page.CropBox.Bounds.Width;
var h = page.CropBox.Bounds.Height;
// after
var w = page.Width;
var h = page.Height;
  • You relied on the old "as‑displayed" crop box rectangle (origin at 0,0, rotation applied) → use the new helper:
// before: page.CropBox.Bounds was already rotated/normalised
var displayed = page.CropBox.Bounds;
// after: ask for the visible bounds explicitly
var displayed = page.CropBox.GetVisibleBounds(page.Rotation);

The old clipping was initialMatrix.Transform(cropBox.Bounds).Normalise(). Since the initial matrix is built from this same crop box, it maps the crop box to the origin and applies the rotation, yielding (0, 0, displayedWidth, displayedHeight), precisely what GetVisibleBounds() returns directly. So the clipping path is identical for all rotations and crop offsets (verified by the rotated/cropped visual-verification and clipping tests).

@BobLd
BobLd merged commit 7359b58 into UglyToad:master Jun 22, 2026
2 checks passed
@BobLd
BobLd deleted the issues/1330 branch June 22, 2026 07:35
This was referenced Jun 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CropBox und MediaBox reporting wrong values when CropBox is set 0.1.14 + 0.1.15-*

1 participant