{"record":{"id":"276fac9fe2549ddf","repo":"iOfficeAI/OfficeCLI","slug":"page-count-unavailable","errorCode":"page_count_unavailable","errorMessage":"--page-count: failed to get page count (Word backend and HTML fallback both unavailable).","messagePattern":"--page-count: failed to get page count \\(Word backend and HTML fallback both unavailable\\)\\.","errorType":"exception","errorClass":"CliException","httpStatus":null,"severity":"error","filePath":"src/officecli/CommandBuilder.View.cs","lineNumber":614,"sourceCode":"            int? withPagesValue = null;\n            if (withPages && (mode.ToLowerInvariant() is \"stats\" or \"s\") && handler is OfficeCli.Handlers.WordHandler wordHandlerForCount)\n            {\n                if (OperatingSystem.IsWindows())\n                {\n                    try { withPagesValue = OfficeCli.Core.WordPdfBackend.GetPageCount(file.FullName); } catch { withPagesValue = null; }\n                }\n                if (withPagesValue == null)\n                {\n                    var tmpHtml = Path.Combine(Path.GetTempPath(), $\"officecli_pc_{Path.GetFileNameWithoutExtension(file.Name)}_{Guid.NewGuid():N}.html\");\n                    try\n                    {\n                        File.WriteAllText(tmpHtml, RenderViaRegistry(wordHandlerForCount, \"docx\", new OfficeCli.Core.Rendering.RenderOptions())!);\n                        withPagesValue = OfficeCli.Core.HtmlScreenshot.GetPageCountFromDom(tmpHtml);\n                    }\n                    finally { try { File.Delete(tmpHtml); } catch { } }\n                }\n                if (withPagesValue == null)\n                    throw new OfficeCli.Core.CliException(\"--page-count: failed to get page count (Word backend and HTML fallback both unavailable).\")\n                    { Code = \"page_count_unavailable\" };\n            }\n\n            if (json)\n            {\n                // Structured JSON output — no Content string wrapping\n                var modeKey = mode.ToLowerInvariant();\n                if (modeKey is \"stats\" or \"s\")\n                {\n                    var statsJson = handler.ViewAsStatsJson();\n                    if (withPagesValue.HasValue) statsJson[\"pages\"] = withPagesValue.Value;\n                    Console.WriteLine(OutputFormatter.WrapEnvelope(statsJson.ToJsonString(OutputFormatter.PublicJsonOptions)));\n                }\n                else if (modeKey is \"outline\" or \"o\")\n                    Console.WriteLine(OutputFormatter.WrapEnvelope(handler.ViewAsOutlineJson().ToJsonString(OutputFormatter.PublicJsonOptions)));\n                else if (modeKey is \"text\" or \"t\")\n                    Console.WriteLine(OutputFormatter.WrapEnvelope(handler.ViewAsTextJson(start, end, maxLines, cols, clipArg).ToJsonString(OutputFormatter.PublicJsonOptions)));\n                else if (modeKey is \"annotated\" or \"a\")","sourceCodeStart":596,"sourceCodeEnd":632,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/CommandBuilder.View.cs#L596-L632","documentation":"The --page-count (withPages) stat for Word tries two sources: first WordPdfBackend.GetPageCount on Windows COM (line 601), then an HTML DOM fallback that renders the doc to HTML and reads GetPageCountFromDom (lines 605-610). If both return null (non-Windows without a working HTML render, or the HTML DOM parse yielding no page count), the command cannot determine pages and throws page_count_unavailable.","triggerScenarios":"`view --mode stats --page-count doc.docx` on a host where Word COM is unavailable AND the HTML/DOM page-count extraction fails (no headless browser needed for DOM count, but the HTML render itself can fail, or the DOM lacks the PAGES:N marker). Windows without Word + a doc whose HTML preview doesn't paginate; or the temp HTML write/render threw.","commonSituations":"Linux/macOS where Word COM never runs and the HTML fallback is the only hope; a doc whose HTML preview omits the <title>PAGES:N> pagination marker; RenderViaRegistry returning null HTML (no renderer); permission errors writing temp HTML.","solutions":["Run on Windows with Word installed so WordPdfBackend.GetPageCount works","Drop --page-count and use --mode stats without it (page count is optional)","Ensure the docx HTML preview renders (check `view --mode html doc.docx` succeeds) and emits the PAGES marker","Install a headless browser / verify renderer registration so the HTML fallback path is healthy"],"exampleFix":"// before\nofficecli view --mode stats --page-count doc.docx   // fails on Linux\n// after\nofficecli view --mode stats doc.docx   // omit --page-count","handlingStrategy":"fallback","validationCode":"// Only request --page-count where it can be satisfied (Windows+Word or healthy HTML fallback).\nbool pageCountLikely = OperatingSystem.IsWindows() || HtmlPathHealthy();\nbool usePageCount = pageCountLikely;\n// otherwise omit --page-count to avoid page_count_unavailable","typeGuard":null,"tryCatchPattern":"try\n{\n    // invoke view --mode stats --page-count doc.docx\n}\ncatch (OfficeCli.Core.CliException ex) when (ex.Code == \"page_count_unavailable\")\n{\n    // retry without --page-count (pages are optional in stats)\n    // invoke view --mode stats doc.docx\n}","preventionTips":["Treat --page-count as optional; degrade gracefully to stats without it","On Windows+Word the COM path is reliable; off-Windows, verify the HTML preview paginates first","Run `view --mode html doc.docx` to confirm the PAGES:N marker is present before relying on the fallback"],"tags":["page-count","word","stats","environment","html"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}