wmjordan/PDFPatcher · error · FormatException

简易书签文件内容不足。

Error message

简易书签文件内容不足。

What it means

Thrown as FormatException by DetectEncoding when the selected bookmark file is shorter than 20 bytes. DetectEncoding reads the first 20 bytes (new byte[20]) to probe for the version marker #版本 / #版本 across the candidate encodings in Constants.Encoding.Encodings; with fewer than 20 bytes the probe is meaningless so it refuses to guess. The threshold is the fixed buffer length b.Length, not a content heuristic.

Source

Thrown at App/Processor/OutlineManager.cs:197

				for (int i = 0; i < indent; i++) {
					writer.Write(indentChar);
				}
				writer.Write(item.Title);
				writer.Write("\t\t");
				writer.Write(item.Page.ToText());
				writer.WriteLine();
				WriteSimpleBookmark(writer, item, indent + 1, indentChar);
			}
		}

		private static Encoding DetectEncoding(string path) {
			const string VersionString = "#版本";
			const string VersionString2 = "#版本";

			var b = new byte[20];
			using (var r = new FileStream(path, FileMode.Open)) {
				if (r.Length < b.Length) {
					throw new FormatException("简易书签文件内容不足。");
				}
				r.Read(b, 0, b.Length);
			}
			foreach (var item in Constants.Encoding.Encodings) {
				if (item == null) {
					continue;
				}
				var s = item.GetString(b);
				if (s.HasPrefix(VersionString) || s.HasPrefix(VersionString2)) {
					return item;
				}
			}
			return Encoding.Default;
		}

	}
}

View on GitHub (pinned to 4782bbd9ad)

Solutions

  1. Check the file size before import: if new FileInfo(path).Length < 20, reject it with a clear message.
  2. Re-export the bookmarks from the source document, or re-download/recreate the file and confirm it contains the #版本 header.
  3. Verify the selected path points to the actual bookmark text file and not a shortcut or 0-byte stub.

Example fix

// before
OutlineManager.ImportSimpleBookmarks(path, doc);

// after
if (!File.Exists(path) || new FileInfo(path).Length < 20) {
    FormHelper.ErrorBox("简易书签文件为空或内容不足,请选择有效的书签文件。");
    return;
}
OutlineManager.ImportSimpleBookmarks(path, doc);
Defensive patterns

Strategy: validation

Validate before calling

if (!File.Exists(path) || new FileInfo(path).Length < 20) {
    FormHelper.ErrorBox("简易书签文件为空或内容不足,请选择有效的书签文件。");
    return;
}

Try / catch

try {
    OutlineManager.ImportSimpleBookmarks(path, doc);
}
catch (FormatException ex) when (ex.Message.Contains("内容不足")) {
    FormHelper.ErrorBox("所选书签文件内容不足 20 字节,无法识别编码。");
}

Prevention

When it happens

Trigger: ImportSimpleBookmarks(path,...) -> DetectEncoding(path) where the FileStream reports r.Length < 20. Caused by an empty file, a file containing only a few characters, a Windows shortcut/partial download, or selecting a non-bookmark text file by mistake.

Common situations: User picks an empty or near-empty file in the bookmark-import dialog; a file that failed to finish writing/syncing; selecting a .lnk or tiny placeholder instead of the real bookmark file; a bookmark export that wrote zero bytes due to an earlier error.

Related errors


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