{"record":{"id":"5030d3d19c019d35","repo":"wmjordan/PDFPatcher","slug":"mediabox","errorCode":null,"errorMessage":"页面缺少 MediaBox。","messagePattern":"页面缺少 MediaBox。","errorType":"exception","errorClass":"PdfException","httpStatus":null,"severity":"error","filePath":"App/Processor/ContentProcessors/PageDimensionProcessor.cs","lineNumber":26,"sourceCode":"namespace PDFPatcher.Processor\r\n{\r\n\tsealed class PageDimensionProcessor : IPageProcessor\r\n\t{\r\n\t\tCoordinateTranslationSettings[] _cts;\r\n\t\tbool _resizePages, _adjustMargins;\r\n\t\tPageRangeCollection _pageRanges;\r\n\t\tPaperSize _refPaperSize;\r\n\t\tpublic PageBoxSettings Settings { get; set; }\r\n\r\n\t\tinternal static CoordinateTranslationSettings ResizePage(PdfDictionary page, PageBoxSettings settings, PaperSize refPaperSize) {\r\n\t\t\tvar size = refPaperSize ?? settings.PaperSize.Clone();\r\n\t\t\tvar hAlign = settings.HorizontalAlign;\r\n\t\t\tvar vAlign = settings.VerticalAlign;\r\n\t\t\tvar pb = page.GetAsArray(PdfName.CROPBOX);\r\n\t\t\tvar b = pb != null ? PdfReader.GetNormalizedRectangle(pb) : null;\r\n\t\t\tpb = page.GetAsArray(PdfName.MEDIABOX);\r\n\t\t\tif (pb == null) {\r\n\t\t\t\tthrow new PdfException(\"页面缺少 MediaBox。\");\r\n\t\t\t}\r\n\t\t\tvar mb = PdfReader.GetNormalizedRectangle(pb);\r\n\t\t\tvar n = PdfHelper.GetPageRotation(page);\r\n\t\t\tif (n == 90 || n == 270) {\r\n\t\t\t\tsize = new PaperSize(size.PaperName, size.Height, size.Width);\r\n\t\t\t}\r\n\t\t\t// 自动旋转页面适应原页面的方向\r\n\t\t\tif (settings.AutoRotation && size.SpecialSize == SpecialPaperSize.None && (size.Width > size.Height) ^ (mb.Width > mb.Height)) {\r\n\t\t\t\tsize = new PaperSize(size.PaperName, size.Height, size.Width);\r\n\t\t\t}\r\n\t\t\tb ??= new Rectangle(mb);\r\n\t\t\tfloat d, z = 1, zx = 1, zy = 1;\r\n\t\t\tfloat dx = 0, dy = 0;\r\n\t\t\tfloat sw = b.Width, sh = b.Height; // resized width and height\r\n\t\t\tif (size.SpecialSize == SpecialPaperSize.FixedWidthAutoHeight || size.SpecialSize == SpecialPaperSize.AsWidestPage || size.SpecialSize == SpecialPaperSize.AsNarrowestPage) {\r\n\t\t\t\tsize.Height = b.Height * size.Width / b.Width;\r\n\t\t\t}\r\n\r","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/wmjordan/PDFPatcher/blob/4782bbd9ada850b56258f29999b7a1c9076c9b1b/App/Processor/ContentProcessors/PageDimensionProcessor.cs#L8-L44","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pre-validate that every page in the document has a resolvable MediaBox (including inherited) before running the resize processor.","Use iTextSharp's PdfReader.GetPageSize(pageNumber) which resolves inherited boxes, instead of raw GetAsArray on the page node.","Repair the source PDF with a tool that normalizes inherited MediaBox to each page node.","Skip pages lacking a MediaBox instead of failing the whole batch."],"exampleFix":"// before\nvar pb = page.GetAsArray(PdfName.MEDIABOX);\nif (pb == null) throw new PdfException(...);\n\n// after\nvar pb = page.GetAsArray(PdfName.MEDIABOX)\n  ?? pdf.GetPageN(pageNumber).GetAsArray(PdfName.MEDIABOX);\nif (pb == null) {\n  Tracker.TraceMessage(Tracker.Category.Error, \"页面缺少 MediaBox，跳过。\");\n  return CoordinateTranslationSettings.Default;\n}","handlingStrategy":"validation","validationCode":"var mb = page.GetAsArray(PdfName.MEDIABOX);\nif (mb == null || mb.Size != 4) {\n    // resolve inherited or skip\n    return; // do not call ResizePage\n}","typeGuard":"static bool PageHasMediaBox(PdfDictionary p) => p?.GetAsArray(PdfName.MEDIABOX)?.Size == 4;","tryCatchPattern":"try { ResizePage(page, settings, refSize); }\ncatch (PdfException ex) { Tracker.TraceMessage(Tracker.Category.Warning, ex.Message); }","preventionTips":["Pre-scan pages for a resolvable MediaBox before batch resize.","Repair PDFs that rely solely on inherited MediaBox."],"tags":["pdf","page-dimensions","validation","media-box","corrupt-file"],"backgroundTag":null,"analyzedSha":"4782bbd9ada850b56258f29999b7a1c9076c9b1b","analyzedAt":"2026-08-13T17:44:50.812Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}