views:

190

answers:

1

Hi,

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:

=============================

Content table

Chapter 1 ----- 3

Chapter 2 ----- 17

=============================

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?

A: 

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()
Markive
Hi Markive,The code helps me understand itextsharp better. But I still don't know how to do it. I don't have a original file. I rewrite the question and hope it shows my problem better. Thank you.
nnn
Markive
You want to use paragraphs so that you can get all your text from your database and get iTextSharp to do the formatting for you.Try this sort of thing: http://www.mikesdotnetting.com/Article/82/iTextSharp-Adding-Text-with-Chunks-Phrases-and-Paragraphs
Markive
It is much easier to use Paragraph to display the texts in content table. But the page number has to be displayed using PdfTemplate which is absolutely positioned. So I guess this is the problem I have. I think it is also possible to position everything in table content absolutely but I need to position every texts which needs lots of work.
nnn
Do the bulk of it using paragraphs then you can chuck chunks of text at it, for your absolutely positioned elements use my code above.. It wasn't too hard to absolutely position everything. All I did was get my pdf template into Adobe Illustrator and work out the x,y value. I wrote up an xml config file that feeds all the arguments needed like the text, x and y, color, align etc.. My routine just spins through each xml node and spits the PDF out perfectly each time..
Markive
It seems I have no choice. I will try to position the content table absolutely. Thanks for the help.
nnn