以下为原代码,文件路径修改可测试:
List list = new List {
@“C:\Users\wgewf_zomh3mr\Desktop\324\试剂领用流程.pdf”,
@“C:\Users\wgewf_zomh3mr\Desktop\324\CHINET中国细菌耐药监测网技术方案(2022年更新版)2022-1-18.pdf”
};
Document document = new Document();
List bookmarkPageIndices = new List(); //存放书签链接页面
List bookmarkItems = new List();
int currentPage = 1;
foreach (var item in list)
{
Document tempDoc = new Document(item);
for (int i = 1; i <= tempDoc.Pages.Count; i++)
{
document.Pages.Add(tempDoc.Pages[i]);
}
string bookmarkTitle = Path.GetFileNameWithoutExtension(item);
OutlineItemCollection bookmark = new OutlineItemCollection(document.Outlines)
{
Title = bookmarkTitle,
Action = new GoToAction(document.Pages[currentPage])
};
document.Outlines.Add(bookmark);
bookmarkPageIndices.Add(currentPage);
bookmarkItems.Add(Path.GetFileNameWithoutExtension(item));
currentPage += tempDoc.Pages.Count;
}
Aspose.Pdf.Page tocPage = document.Pages.Insert(1);
TocInfo tocInfo = new TocInfo();
TextFragment title = new TextFragment(“目录”)
{
TextState = { FontSize = 20, FontStyle = FontStyles.Bold, Font = FontRepository.FindFont(“SimSun”) }
};
title.TextState.Font.IsEmbedded = true;
tocInfo.Title = title;
tocPage.TocInfo = tocInfo;
for (int i = 0; i < list.Count; i++)
{
// 创建目录项
var heading = new Aspose.Pdf.Heading(1)
{
TocPage = tocPage,
DestinationPage = document.Pages[bookmarkPageIndices[i] + 1],
Top = document.Pages[bookmarkPageIndices[i]].Rect.Height + 20
};
var segment = new TextSegment
{
Text = Path.GetFileNameWithoutExtension(list[i]),
TextState = { Font = FontRepository.FindFont(“SimSun”), FontSize = 14 }
};
segment.TextState.Font.IsEmbedded = true; heading.Segments.Add(segment);
tocPage.Paragraphs.Add(heading);
}
document.Save(@“C:\Users\wgewf_zomh3mr\Desktop\324\112.pdf”);
CHINET中国细菌耐药监测网技术方案(2022年更新版)2022-1-18.pdf (581.3 KB)
试剂领用流程.pdf (34.4 KB)