wmjordan/PDFPatcher · error · PdfException

页面缺少 MediaBox。

Error message

页面缺少 MediaBox。

What it means

PageDimensionProcessor.ResizePage throws PdfException when the page dictionary lacks a MEDIABOX array. The MEDIABOX is the mandatory page boundary in the PDF spec; without it the page geometry cannot be computed and the resize operation cannot proceed. The code checks page.GetAsArray(PdfName.MEDIABOX) and rejects null.

Source

Thrown at App/Processor/ContentProcessors/PageDimensionProcessor.cs:26

namespace PDFPatcher.Processor
{
	sealed class PageDimensionProcessor : IPageProcessor
	{
		CoordinateTranslationSettings[] _cts;
		bool _resizePages, _adjustMargins;
		PageRangeCollection _pageRanges;
		PaperSize _refPaperSize;
		public PageBoxSettings Settings { get; set; }

		internal static CoordinateTranslationSettings ResizePage(PdfDictionary page, PageBoxSettings settings, PaperSize refPaperSize) {
			var size = refPaperSize ?? settings.PaperSize.Clone();
			var hAlign = settings.HorizontalAlign;
			var vAlign = settings.VerticalAlign;
			var pb = page.GetAsArray(PdfName.CROPBOX);
			var b = pb != null ? PdfReader.GetNormalizedRectangle(pb) : null;
			pb = page.GetAsArray(PdfName.MEDIABOX);
			if (pb == null) {
				throw new PdfException("页面缺少 MediaBox。");
			}
			var mb = PdfReader.GetNormalizedRectangle(pb);
			var n = PdfHelper.GetPageRotation(page);
			if (n == 90 || n == 270) {
				size = new PaperSize(size.PaperName, size.Height, size.Width);
			}
			// 自动旋转页面适应原页面的方向
			if (settings.AutoRotation && size.SpecialSize == SpecialPaperSize.None && (size.Width > size.Height) ^ (mb.Width > mb.Height)) {
				size = new PaperSize(size.PaperName, size.Height, size.Width);
			}
			b ??= new Rectangle(mb);
			float d, z = 1, zx = 1, zy = 1;
			float dx = 0, dy = 0;
			float sw = b.Width, sh = b.Height; // resized width and height
			if (size.SpecialSize == SpecialPaperSize.FixedWidthAutoHeight || size.SpecialSize == SpecialPaperSize.AsWidestPage || size.SpecialSize == SpecialPaperSize.AsNarrowestPage) {
				size.Height = b.Height * size.Width / b.Width;
			}

View on GitHub (pinned to 4782bbd9ad)

Solutions

  1. Pre-validate that every page in the document has a resolvable MediaBox (including inherited) before running the resize processor.
  2. Use iTextSharp's PdfReader.GetPageSize(pageNumber) which resolves inherited boxes, instead of raw GetAsArray on the page node.
  3. Repair the source PDF with a tool that normalizes inherited MediaBox to each page node.
  4. Skip pages lacking a MediaBox instead of failing the whole batch.

Example fix

// before
var pb = page.GetAsArray(PdfName.MEDIABOX);
if (pb == null) throw new PdfException(...);

// after
var pb = page.GetAsArray(PdfName.MEDIABOX)
  ?? pdf.GetPageN(pageNumber).GetAsArray(PdfName.MEDIABOX);
if (pb == null) {
  Tracker.TraceMessage(Tracker.Category.Error, "页面缺少 MediaBox,跳过。");
  return CoordinateTranslationSettings.Default;
}
Defensive patterns

Strategy: validation

Validate before calling

var mb = page.GetAsArray(PdfName.MEDIABOX);
if (mb == null || mb.Size != 4) {
    // resolve inherited or skip
    return; // do not call ResizePage
}

Type guard

static bool PageHasMediaBox(PdfDictionary p) => p?.GetAsArray(PdfName.MEDIABOX)?.Size == 4;

Try / catch

try { ResizePage(page, settings, refSize); }
catch (PdfException ex) { Tracker.TraceMessage(Tracker.Category.Warning, ex.Message); }

Prevention

When it happens

Trigger: Calling ResizePage on a PdfDictionary whose MEDIABOX entry is missing or not an array — typically a corrupt, truncated, or spec-violating PDF where pages inherit MediaBox from a parent but the inheritance was not resolved, or the entry was stripped.

Common situations: Processing a damaged or hand-crafted PDF that omits the required MediaBox; a PDF where MediaBox is inherited from the page-tree parent (valid per spec) but iTextSharp's GetAsArray on the page node returns null because inheritance wasn't flattened; a partially written/truncated file.

Related errors


AI-assisted analysis of wmjordan/PDFPatcher@4782bbd9ad (2026-08-13). Data as JSON: /api/errors/5030d3d19c019d35. Report an issue: GitHub.