当前位置: 首页 > 知识库问答 >
问题:

如何使用itextshap添加带有页码的索引?

狄侯林
2023-03-14
Rectangle rec2 = new Rectangle(PageSize.A4);
using (FileStream fs = new FileStream("D:/Sample1.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
using (Document doc = new Document(PageSize.A4, 10f, 10f, 10f, 10f))
using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
{
       PdfPageEventHelper pageEventHelper = new PdfPageEventHelper();
       writer.PageEvent = pageEventHelper;
       doc.SetMargins(20, 20, 50, 50);
       doc.NewPage();
       doc.Open();
       var Content = writer.DirectContent;
       var pageBorderRect = new Rectangle(doc.PageSize);
       ………..
       ………………..
       ………………..
       ………..
       doc.Close();
}
public void AddPageNumber(string BidNumber)
        {
            byte[] bytes = System.IO.File.ReadAllBytes(@"D:\Sample1.pdf");
            Font blackFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, BaseColor.BLACK);
            Font boldfont = FontFactory.GetFont("Arial", 14, Font.BOLD, BaseColor.BLACK);
            using (MemoryStream stream = new MemoryStream())
            {
                PdfReader reader = new PdfReader(bytes);
                using (PdfStamper stamper = new PdfStamper(reader, stream))
                {
                    int pages = reader.NumberOfPages;
                   for (int j = 2; j <= pages; j++)
                    {
                        ColumnText.ShowTextAligned(stamper.GetUnderContent(j), Element.ALIGN_RIGHT, new Phrase((j - 1).ToString(), blackFont), 568f, 15f, 0);
                    }
                }
                bytes = stream.ToArray();
            }
            System.IO.File.WriteAllBytes(@"D:\newPDF.pdf", bytes);
        }
int chapTemplateCounter = 0;
    public override void OnChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title)
    {
        base.OnChapter(writer, document, paragraphPosition, title);

        BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);

        tableOfContentsTemplateList[chapTemplateCounter].BeginText();
        tableOfContentsTemplateList[chapTemplateCounter].SetFontAndSize(bfTimes, 12);
        tableOfContentsTemplateList[chapTemplateCounter].SetTextMatrix(0, 0);
        tableOfContentsTemplateList[chapTemplateCounter].ShowText("" + writer.PageNumber);
        tableOfContentsTemplateList[chapTemplateCounter].EndText();

        chapTemplateCounter++;
    }

共有1个答案

桓深
2023-03-14

请看一下IndexWords示例。

在本例中,我们创建IndexEvents类的一个实例,并将该实例用作页面事件:

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
IndexEvents index = new IndexEvents();
writer.setPageEvent(index);

现在,当我们将内容添加到文档中时,我们将像下面这样标记我们想要索引的单词:

Paragraph p = new Paragraph("Quick brown fox ");
p.add(index.create("jumps", "Jump"));
p.add(" over the lazy dog.");
document.add(p);
p = new Paragraph();
p.add(index.create("Quick brown fox", "Fox", "quick, brown"));
p.add(new Chunk(" jumps over "));
p.add(index.create("the lazy dog.", "Dog", "lazy"));
p.add(index.create(" ", "Jumping"));
document.add(p);
p = new Paragraph();
p.add(new Chunk("The fox is "));
p.add(index.create("brown", "Color", "brown"));
p.add(index.create(" ", "Brown", "color", "see Color; brown"));
p.add(Chunk.NEWLINE);
document.add(p);

我们将“brown”标记为“color”,并指定“brown”。我们还添加了一个引用。当人们搜索或索引单词“brown”时,我们会引用“color”。

一旦我们添加了所需的所有内容,我们就会转到一个新页面,并创建索引:

document.newPage();
// we add the index
document.add(new Paragraph("Index:"));
List<Entry> list = index.getSortedEntries();
for (Entry entry : list) {
    Paragraph in = new Paragraph();
    in.add(new Chunk(entry.getIn1()));
    if (entry.getIn2().length() > 0) {
        in.add(new Chunk("; " + entry.getIn2()));
    }
    if (entry.getIn3().length() > 0) {
        in.add(new Chunk(" (" + entry.getIn3() + ")"));
    }
    List<Integer> pages = entry.getPagenumbers();
    List<String> tags = entry.getTags();
    in.add(": ");
    for (int i = 0, x = pages.size(); i < x; i++) {
        Chunk pagenr = new Chunk(" p" + pages.get(i));
        pagenr.setLocalGoto((String) tags.get(i));
        in.add(pagenr);
    }
    document.add(in);
}

您可以在这里检查结果:index_words.pdf

 类似资料:
  • 我使用iReport创建了多页pdf报表,如下所示。 应该是这样的: 请建议。

  • 我当前正试图添加一个链接到pdf文档的页脚页眉,但是库给出了以下错误System.IndexoutOfrangeException:“请求的页码0已超出范围。”当使用IText7库将链接添加到标头时。 我在IText7中找不到任何关于这个问题的在线代码示例,ITextSharp中的解决方案不再适用。 我的问题是我如何添加一个链接到一个外部网站到PDF的标题?当前的行为是库中的bug还是有意的? 包

  • 我需要在iText Pdf的每一页添加一个条形码,所以我认为一个事件可能是我需要的,但是我下面的代码只把条形码放在最后一页。 我还有别的办法吗。谢谢 罗恩

  • 我是hazelcast的新手,我到处都读到添加索引将提高hazelcast中的查询性能。我将数据存储在hazelcast地图中,键作为字符串,值作为便携式。如果我假设我的价值是这样的: 用于添加索引的golang文档说,在将条目添加到此映射之前,应确保添加索引 所以我对此没有什么疑问: 在给地图赋值之前,是否每次都需要添加索引?还是在应用程序生命周期中只有一次

  • 问题内容: 如何使用iText在每个页面上添加总页数? 问题答案: 使用伪页面计数将输出从a 处理为第一个。 从中创建一个,调用以获取实际的页数。 重新创建PDF输出,知道页数是多少,并相应地更改页脚。 这很麻烦,但是如果没有两遍方法,就没有简单的方法来知道页数。有关处理PDF的详细信息,请参见示例代码。

  • 使用iTextSharp,您可以通过将事件附加到PDF来向PDF添加页眉/页脚,如本SO答案中所述:https://stackoverflow.com/a/19004392 我怎样才能用 iText 7 做同样的事情? 这个链接有Java代码示例,但看起来不像它使用的页面事件。