wmjordan/PDFPatcher · error · FormatException

在简易书签第 {lineNum} 行的缩进格式不正确。 说明:下级书签最多只能比上级书签多一个缩进标记。

Error message

在简易书签第 {lineNum} 行的缩进格式不正确。

说明:下级书签最多只能比上级书签多一个缩进标记。

What it means

Thrown as FormatException by ImportSimpleBookmarks when a parsed bookmark line's indentation increases by more than one level relative to currentIndent. The simple-bookmark format encodes hierarchy purely through repeated indent markers (tab by default, configurable via the 缩进标记 command); a child may be exactly one level deeper than its parent, never two or more. The message names the offending 1-based line number (lineNum) so the user can locate it.

Source

Thrown at App/Processor/OutlineManager.cs:132

					pageNum = 0;
				}
				else {
					if (pnText.IndexOfAny(__fullWidthNumbers) != -1) {
						digits = Array.ConvertAll(m.Groups[2].Value.ToCharArray(), d => ValueHelper.MapValue(d, __fullWidthNumbers, __halfWidthNumbers, d));
						pnText = new string(digits, 0, digits.Length);
					}
					if (pnText.TryParse(out pageNum)) {
						pageNum += pageOffset;
					}
				}
				bookmark = target.CreateBookmark();
				if (indent == currentIndent) {
					currentBookmark.ParentNode.AppendChild(bookmark);
				}
				else if (indent > currentIndent) {
					currentBookmark.AppendChild(bookmark);
					if (indent - currentIndent > 1) {
						throw new FormatException($"在简易书签第 {lineNum} 行的缩进格式不正确。\n\n说明:下级书签最多只能比上级书签多一个缩进标记。");
					}
					currentIndent++;
				}
				else /* indent < currentIndent */ {
					while (currentIndent > indent && currentBookmark.ParentNode != root) {
						currentBookmark = currentBookmark.ParentNode as BookmarkContainer;
						currentIndent--;
					}
					currentBookmark.ParentNode.AppendChild(bookmark);
				}
				bookmark.Title = title;
				if (!isOpen) {
					bookmark.IsOpen = false;
				}
				if (pageNum > 0) {
					bookmark.Page = pageNum;
				}
				currentBookmark = bookmark;

View on GitHub (pinned to 4782bbd9ad)

Solutions

  1. Open the file at the reported line number and reduce its leading indent to exactly one marker beyond its intended parent.
  2. Normalize the whole file to a single indent character (tabs or spaces) and ensure each nesting level adds exactly one marker.
  3. If the indent string differs from tabs, add a single #缩进标记=<marker> directive at the top BEFORE any bookmarks and verify it applies to the whole file.
  4. Pre-validate by scanning lines and asserting each indent step is <= 1 before calling ImportSimpleBookmarks.

Example fix

// before (jumps from level 0 to level 2)
第一章	1
		1.1 节	2

// after (single-step indent)
第一章	1
	1.1 节	2
Defensive patterns

Strategy: validation

Validate before calling

static void ValidateSimpleBookmarkIndents(string path, string indentString) {
    int current = -1;
    int lineNum = 0;
    foreach (var raw in File.ReadLines(path)) {
        lineNum++;
        if (string.IsNullOrWhiteSpace(raw)) continue;
        if (raw[0] == '#' || raw[0] == '#') continue;
        int p = 0, ind = 0;
        while (raw.IndexOf(indentString, p) == p) { p += indentString.Length; ind++; }
        if (current >= 0 && ind - current > 1)
            throw new FormatException($"第 {lineNum} 行缩进跳跃过大:{ind} > {current}+1");
        if (ind > current) current = ind;
    }
}

Try / catch

try {
    OutlineManager.ImportSimpleBookmarks(path, doc);
}
catch (FormatException ex) when (ex.Message.Contains("缩进格式不正确")) {
    FormHelper.ErrorBox(ex.Message + "\n请检查对应行的缩进标记。");
}

Prevention

When it happens

Trigger: A line whose leading indentString count minus currentIndent is > 1. Example: currentIndent==0 and the next bookmark line begins with two or more tabs/spaces. Also triggered when the indent string was redefined mid-file with 缩进标记 and earlier lines were parsed under the old string, or when tabs and spaces are mixed so IndexOf(indentString) miscounts.

Common situations: Hand-edited or pasted bookmark files with inconsistent nesting; files indented with spaces while the parser expects tabs (or vice-versa); deeply-nested bookmarks where a parent was deleted but children kept their old indent; a 缩进标记 change that makes previously-valid lines over-indented relative to the new marker.

Related errors


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