C#使用iText7给PDF文档添加书签

上一篇文章将SqlSugar官网文档中每个链接对应的网页生成独立PDF文档再合并为单个PDF文档,但是没有书签,八百多页的内容查找和跳转都不方便,本文学习和使用iText7给PDF文档添加多级书签。
  添加多级书签分为两大步骤:1)获取书签分类;2)创建多级书签。如下图所示,SqlSugar官网文档页面右侧的帮助链接都有所属版块,如版本记录属于产品详情版块,入门必看属于从零开始版块,本文第一步先分析如何获取链接所属的版块信息。

  查看网页源代码,分析其中的结构,可以看到版块名称和帮助链接都位于class值为layui-tree的列表内,且版块名称和帮助链接属于同一级别,先出现版块li,其class值为clear,后面跟随的li都属于其版块内容,class值均包含site-float site-tree-noicon。

  根据上述分析,采用HtmlAgilityPack抓取并获取版块及其所属帮助链接信息,主要代码及程序运行效果如下所示:

csharp 复制代码
HtmlWeb web = new HtmlWeb();
HtmlAgilityPack.HtmlDocument docu = web.Load(txtUrl.Text);
HtmlNode node = docu.DocumentNode.SelectSingleNode(@"//ul[@class='layui-tree']");

HtmlNode tmpNode;
string curSection = string.Empty;

foreach (HtmlNode subNode in node.ChildNodes)
{
    string className = subNode.GetAttributeValue<string>("class", string.Empty);

    if (string.IsNullOrEmpty(className))
    {
        continue;
    }

    if (className == "clear")
    {
        curSection = subNode.InnerText;
    }

    if (className.Contains("site-float site-tree-noicon"))
    {
        tmpNode = subNode.SelectSingleNode(".//a");
        
        DataGridViewRow dgvr = new DataGridViewRow();        
        dgvr.CreateCells(dataGridView1);
        dgvr.Cells[0].Value = dataGridView1.Rows.Count + 1;
        dgvr.Cells[1].Value = curSection ;
        dgvr.Cells[2].Value = tmpNode.InnerText;
        dgvr.Cells[3].Value = @"https://www.donet5.com" + tmpNode.Attributes["href"].Value;

        dataGridView1.Rows.Add(dgvr);
    }                
}


  生成的独立文档如下所示,为便于获取文档所属板块,将板块名称作为文件名的第一部分。

  接下来是添加多级书签,主要就是两级,第一级为版块,第二级为版块包含的帮助文档。使用PdfOutline创建书签及跳转链接。在PDF文档合并之后,调用PdfDocument.GetOutlines函数获取书签根节点,然后调用PdfOutline.AddOutline函数创建下级书签(类似于树控件中的节点,可以不停地创建),关键代码如下所示。

csharp 复制代码
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(txtFileName.Text));
...
...
PdfOutline rootOutline = pdfDoc.GetOutlines(false);
PdfOutline tmpOutline = null;
PdfOutline tmpSubOutline = null;
int curPageIndex = 1;
int underlineIndex = -1;
string tmpModule = "XXXXXX";

foreach (PdfFileInfo doc in pdfFiles)
{
    string fileName = doc.FileName;

    if (!fileName.StartsWith(tmpModule))
    {
        underlineIndex = fileName.IndexOf('_');

		//添加第一级版块书签及跳转链接
        tmpModule = fileName.Substring(0, underlineIndex);
        tmpOutline = rootOutline.AddOutline(tmpModule);
        tmpOutline.AddDestination(PdfExplicitDestination.CreateFit(pdfDoc.GetPage(curPageIndex)));
    }

	//添加第二级书签及跳转链接
    tmpSubOutline = tmpOutline.AddOutline(fileName.Substring(underlineIndex + 1));
    tmpSubOutline.AddDestination(PdfExplicitDestination.CreateFit(pdfDoc.GetPage(curPageIndex)));
    curPageIndex += doc.docu.GetNumberOfPages();
}

程序的运行效果及最终的PDF文档效果如下图所示。SqlSugar官网帮助文档的合并PDF文件已上传到资源中,待审核后即可下载使用,有需要的可以自行下载。

参考文献:

1\]https://itextpdf.com/ \[2\]https://github.com/itext/itext-dotnet \[3\]https://blog.csdn.net/qq_38628970/article/details/135478244 \[4\]https://github.com/itext/itext-publications-samples-dotnet/blob/master/itext/itext.samples/itext/samples/sandbox/merge/MergeWithOutlines.cs \[5\]https://blog.csdn.net/rebecca_cao/article/details/135185043

相关推荐
SEO-狼术41 分钟前
Infragistics now supports .NET 10
pdf·.net
南無忘码至尊1 小时前
Unity学习90天-第2天-认识键盘 / 鼠标输入(PC)并实现WASD 移动,鼠标控制物体转向
学习·unity·c#·游戏开发
William_cl2 小时前
C# ASP.NET 分层架构实战:BLL (Service) 业务层从入门到封神(规范 + 避坑)
架构·c#·asp.net
琪伦的工具库2 小时前
批量PDF合并工具使用说明:批量合并与直接合并两种模式,拖拽排序/页面范围/遍历子目录/重名自动处理
数据结构·pdf·排序算法
琪伦的工具库3 小时前
批量PDF多页面合并工具使用说明:多页合并N合1/多文件合并,支持页码范围/矢量模式/DPI与布局边距
pdf
qq_454245033 小时前
图数据标准化与智能去重框架:设计与实现解析
数据结构·架构·c#·图论
CSharp精选营3 小时前
C# 如何减少代码运行时间:7 个实战技巧
性能优化·c#·.net·技术干货·实战技巧
hhh3u3u3u16 小时前
Visual C++ 6.0中文版安装包下载教程及win11安装教程
java·c语言·开发语言·c++·python·c#·vc-1
加号316 小时前
【C#】实现沃德普线光控制器通信控制(附完整源码)
开发语言·c#
lzhdim17 小时前
SharpCompress:跨平台的 C# 压缩与解压库
开发语言·c#