首页 > 解决方案 > 带有 itextsharp 的简单目录

问题描述

我有一个网站,我在 Telerik radwindow 中使用 itextsharp 显示 pdf。我成功地在 Telerik radwindow 弹出窗口上显示 pdf。现在,我有页面名称及其顺序,需要重新排列以使用 itextsharp 显示 TOC,如下所示

在此处输入图像描述

标签: c#itext

解决方案


您可以通过使用 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 版本整体处于维护模式,因此无需担心在进一步开发过程中会从程序集中删除。


推荐阅读