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

相关推荐
VinciYan22 分钟前
编译Open Cascade(OCC)并使用C#进行开发
c#·开源软件·cad·occ·open cascade·三维cad
该醒醒了~25 分钟前
yolov5实例分割跑通以及C#读取yolov5_Seg实例分割转换onnx进行检测部署
python·yolo·c#
柠檬味的薄荷心34 分钟前
【Unity2D 2022:Particle System】添加拾取粒子特效
笔记·unity·c#·游戏引擎
emplace_back1 小时前
C# MathNet
开发语言·机器学习·c#
danielli2 小时前
C# 开发Winform DataGridView的增删改查实战
开发语言·oracle·c#
不爱敲代码的阿玲2 小时前
C# —— 日期对象
开发语言·c#
Dm_dotnet3 小时前
大语言模型的应用探索AI Agent初探!
c#
hlyling4 小时前
一键高效处理,批量缩放PNG图片,按比例轻松调整,高效工作从此开始!
javascript·python·c#·objective-c·batch·symfony
不爱学习的啊Biao5 小时前
C# 下sendmessage和postmessage的区别详解与示例
开发语言·c#
yizhou20105 小时前
C# Winform项目中简单使用Sqlite并在DataGridview中显示
sqlite·c#