使用itextsharp生成内容表 [英] Generate table of content using itextsharp
问题描述
我正在做的是从数据库生成pdf小册子。我需要生成一个包含页码的内容表。例如,有两章页码如:
What I am doing is to generate a pdf booklet from database. I need go generate a content table with page numbers. E.g there are two chapters with page number like:
========================= ====
=============================
内容表
第1章----- 3
Chapter 1 ----- 3
第2章----- 17
Chapter 2 ----- 17
================== ===========
=============================
第1章-----是正常段落。但页码3必须使用PdfTemplate生成,因为它只能在以后知道。但pdfTemplate绝对定位。我怎么知道PdfTemplate的位置?我对吗?我怎么能搞清楚这一点还是应该使用其他方法?
The text "Chapter 1 ----- " is normal paragraph. But the page number "3" has to be produced using PdfTemplate because it can only be known later. But the pdfTemplate is absolutely positioned. How can I know where to position the PdfTemplate? Am I right on this ? How could I figure this out or should I use other methods?
推荐答案
我已经提取了一些代码来帮助你你的方式..这段代码允许你使用x和y在页面的任何地方放置文本。您可能实际上想要使用iTextSharp的内置段落和余量支持,但这将非常有用,只需要转换为C#
I've extracted a bit of code to get you on your way.. This code allows you to place text anywhere on a page using an x and y. You may actually want to use iTextSharp's built in paragraph and margin support, but this will be useful, just needs converting to C#
Dim stamper As PdfStamper
Dim templateReader As PdfReader = New PdfReader(yourFileName)
Dim currentPage As PdfImportedPage = stamper.GetImportedPage(templateReader, 1)
stamper.InsertPage(1, PageSize.A4)
Dim cb As PdfContentByte = stamper.GetOverContent(1)
cb.AddTemplate(currentPage, 0, 0)
使用每个元素查看下一位你想要添加..
Look this next bit with each element you want to add..
cb.BeginText()
cb.SetFontAndSize(bf, 12)
cb.SetColorFill(color) 'create a color object to represent the colour you want
cb.ShowTextAligned(1, "Content Table", x, y, 0) 'pass in the x & y of the element
cb.EndText()
这篇关于使用itextsharp生成内容表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!