Files
PDFPatcher/App/Model/PdfPathDocument.cs
T
2024-10-07 16:17:27 +08:00

77 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using iTextSharp.text.pdf;
using PDFPatcher.Processor;
namespace PDFPatcher.Model
{
sealed class PdfPathDocument : IHierarchicalObject<DocumentObject>
{
const int pageGroupNumber = 100;
readonly PdfReader _pdf;
readonly DocumentObject[] _rootObjects;
readonly DocumentObject _trailer;
readonly DocumentObject _hiddenObjects;
readonly Dictionary<int, int> _pageMapper;
public PdfPathDocument(string pdfPath) {
_pdf = PdfHelper.OpenPdfFile(pdfPath, AppContext.LoadPartialPdfFile, false);
_pageMapper = _pdf.GetPageRefMapper();
_trailer = new DocumentObject(this, null, "Trailer", _pdf.Trailer, PdfObjectType.Trailer) {
IsKeyObject = true,
Description = "文档根节点",
FriendlyValue = System.IO.Path.GetFileNameWithoutExtension(pdfPath)
};
_hiddenObjects = new DocumentObject(this, null, "隐藏对象", null, PdfObjectType.Hidden);
var l = _pdf.NumberOfPages;
if (l > 301) {
var c = new DocumentObject[1 + (l + pageGroupNumber - 1) / pageGroupNumber + 1];
c[0] = _trailer;
for (int i = 1; i < c.Length - 1; i++) {
var a = (i - 1) * pageGroupNumber + 1;
var b = Math.Min(l, i * pageGroupNumber);
c[i] = new DocumentObject(this, null, "Pages", null, PdfObjectType.Pages) { IsKeyObject = true, ExtensiveObject = $"{a}-{b}", FriendlyValue = $"第 {a} 至 {b} 页,共 {l} 页" };
}
c[c.Length - 1] = _hiddenObjects;
_rootObjects = c;
}
else {
_rootObjects = [
_trailer,
new DocumentObject (this, null, "Pages", null, PdfObjectType.Pages) { IsKeyObject = true, FriendlyValue = "共 " + l + " 页" },
_hiddenObjects
];
}
}
public PdfReader Document => _pdf;
public DocumentObject Trailer => _trailer;
public int PageCount => _pdf.NumberOfPages;
public void Close() {
_pdf.Close();
}
public int GetPageNumber(PdfIndirectReference pdfRef) {
_pageMapper.TryGetValue(pdfRef.Number, out int page);
return page;
}
#region IHierarchicalObject<DocumentObject> 成员
bool IHierarchicalObject<DocumentObject>.HasChildren => true;
ICollection<DocumentObject> IHierarchicalObject<DocumentObject>.Children {
get {
var c = new DocumentObject[_rootObjects.Length];
Array.Copy(_rootObjects, c, _rootObjects.Length);
return c;
}
}
#endregion
}
}