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

相关推荐
hweiyu004 分钟前
C#学习教程(附电子书资料)
开发语言·学习·c#
csdn_aspnet2 小时前
C# DataGridView 选中所有复选框
c#·winform·datagridview
qq_297908015 小时前
c#车检车构客户管理系统软件车辆年审短信提醒软件
sqlserver·c#·开源软件
酷炫码神6 小时前
C#数组与集合
开发语言·c#
钢铁男儿6 小时前
C# 深入理解类(静态函数成员)
java·开发语言·c#
敲代码的小吉米14 小时前
前端上传el-upload、原生input本地文件pdf格式(纯前端预览本地文件不走后端接口)
前端·javascript·pdf·状态模式
CoderIsArt15 小时前
参数系统的基类Parameter抽象类
c#
盛夏绽放16 小时前
Python字符串常用方法详解
开发语言·python·c#
东方巴黎~Sunsiny18 小时前
EasyExcel导出excel再转PDF转图片详解
pdf·excel
Tummer836320 小时前
C#+WPF+prism+materialdesign创建工具主界面框架
开发语言·c#·wpf