c# - 带有 itextsharp 的简单目录
问题描述
我有一个网站,我在 Telerik radwindow 中使用 itextsharp 显示 pdf。我成功地在 Telerik radwindow 弹出窗口上显示 pdf。现在,我有页面名称及其顺序,需要重新排列以使用 itextsharp 显示 TOC,如下所示
解决方案
您可以通过使用 a 创建一个块分隔符(参见此答案)tabPosition
(以使页码左对齐,如您在图像中显示的那样)。
using (FileStream output = new FileStream(@"simpleToc.pdf", FileMode.Create, FileAccess.Write))
using (Document document = new Document(PageSize.A4))
{
PdfWriter writer = PdfWriter.GetInstance(document, output);
document.Open();
Chunk leader = new Chunk(new DottedLineSeparator(), 400);
Paragraph p = new Paragraph("Terms and Conditions");
p.Add(leader);
p.Add("4");
document.Add(p);
p = new Paragraph("Dental");
p.Add(leader);
p.Add("6");
document.Add(p);
p = new Paragraph("Vision");
p.Add(leader);
p.Add("7");
document.Add(p);
p = new Paragraph("Neighborhood Pharmacy");
p.Add(leader);
p.Add("8");
document.Add(p);
p = new Paragraph("Teladoc");
p.Add(leader);
p.Add("9");
document.Add(p);
p = new Paragraph("Retail Health Clinics");
p.Add(leader);
p.Add("11");
document.Add(p);
p = new Paragraph("Counseling Services");
p.Add(leader);
p.Add("12");
document.Add(p);
p = new Paragraph("Medical Bill Saver\u2122");
p.Add(leader);
p.Add("13");
document.Add(p);
p = new Paragraph("Vitamins");
p.Add(leader);
p.Add("14");
document.Add(p);
}
结果看起来像这样
当您将库称为“iTextSharp”并且仅使用[iText]标签时,我假设您使用的是 iText 5.x,而不是 7.x。上面的代码已经使用当前的 5.5.14-SNAPSHOT 开发版本进行了测试。
上面使用的Chunk
构造函数被标记为Obsolete:
/**
* Creates a tab Chunk.
* Note that separator chunks can't be used in combination with tab chunks!
* @param separator the drawInterface to use to draw the tab.
* @param tabPosition an X coordinate that will be used as start position for the next Chunk.
* @since 2.1.2
*/
[Obsolete]
public Chunk(IDrawInterface separator, float tabPosition) : this(separator, tabPosition, false)
但是由于旧的 iText 5.x 版本整体处于维护模式,因此无需担心在进一步开发过程中会从程序集中删除。
推荐阅读
- karate - 在空手道场景大纲中经过一定次数的迭代后引入延迟
- go - 项目结构和导入包
- ionic-framework - 离子:routerLink 不再工作,因为使用选项卡实现导航
- python - 为什么我的线程进程仍然冻结?
- powershell - 为什么powershell不打印带有换行符的txt?
- r - R:如何分隔和添加由“;”分隔的数值 (内存溢出问题)
- gitlab - Terraform - 从子模块访问根模块脚本
- javascript - 如何从 React Native 中的 Firestore 获取集合中的所有文档
- sql - 需要帮助在 SQL JOIN 中设置列值
- python - 为什么 logger.get_logger(__name__) 总是有效?