我有一个如下所示的文件夹结构:
\- root
|- 1. Folder1
\- 2. Folder2
||- 2.1 Folder21
|| file.pdf
|\- 2.2 Folder22
|| |- 2.2.1 Folder 221
|| |- 2.2.2 Folder 222
|| otherFile.pdf
|| yetAnotherFile.pdf
||- 2.3 Folder23
|- 3. Folder3
我想创建一个包含文件夹中所有pdf的pdf,将它们合并为一个pdf,并创建一个书签结构,镜像文件系统中文件和文件夹的布局。
已经在运作的内容:
我遇到麻烦:
这是我的树数据结构:
class TreeNode : IEnumerable<TreeNode> {
private readonly List<TreeNode> children = new List<TreeNode>();
readonly List<string> files;
private string Id;
private TreeNode Parent { get; set; }
public TreeNode(string id) {
Id = id;
files = Directory.GetFiles(id, "*.pdf", SearchOption.TopDirectoryOnly).OrderBy(f => f).ToList();
}
public TreeNode Add(TreeNode item) {
item.Parent = this;
children.Add(item);
return item;
}
public IEnumerator<TreeNode> GetEnumerator() {
return children.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
public int Count => children.Count;
public int FileCount => files.Count;
}
我用这些函数构建我的树:
private static TreeNode BuildTree(string path) {
var root = new TreeNode(path);
TraverseDir(root);
return root;
}
private static void TraverseDir(TreeNode parent) {
var dirs = Directory.EnumerateDirectories(parent.Id).OrderBy(f => f);
foreach (var dir in dirs) {
var tmp = parent.Add(new TreeNode(dir));
TraverseDir(tmp);
}}
这是我第一次尝试使用iText构建层次结构:
private static List<Dictionary<string, object>> traverse(TreeNode tree) {
var outlines = new List<Dictionary<string, object>>();
var bm = new Dictionary<string, object> {
{"Title", tree.Id}
};
var kids = new List<Dictionary<string, object>>();
foreach (var child in tree.children) {
var c = new Dictionary<string, object> {
{"Title", child.Id}
};
kids.Add(c);
traverse(child);
}
bm.Add("Kids", kids);
outlines.Add(bm);
return outlines;
}
这只会创建层次结构的第一层,即
\- root
|- 1. Folder1
|- 2. Folder2
|- 3. Folder3
iText期望的大纲数据结构是List<Dictionary<string, object>>
。子节点附加在特定字典的Kids
键中。
有人能指出我正确的方向吗?