views:

208

answers:

2

Hello,

I've recently started using iTextSharp to generate PDF reports from data. It works quite nicely.

In one particular report, I need a section to always appear at the bottom of the page. I'm using the PdfContentByte to create a dashed line 200f from the bottom:

cb.MoveTo(0f, 200f);
cb.SetLineDash(8, 4, 0);
cb.LineTo(doc.PageSize.Width, 200f);
cb.Stroke();

Now I'd like to insert content below that line. However, (as expected) the PdfContentByte methods don't change the vertical position of the PdfWriter. New paragraphs, for example, appear earlier in the page.

// appears wherever my last content was, NOT below the dashed line
doc.Add(new Paragraph("test", _myFont));

Is there some way to instruct the pdfwriter that I'd like to advance the vertical position to below the dashed line now, and continue inserting content there? There is a GetVerticalPosition() method -- it'd be nice if there was a corresponding Setter :-).

// Gives me the vertical position, but I can't change it
var pos = writer.GetVerticalPosition(false);

So, is there any way to set the writer's position by hand? Thanks!

A: 

Alright, I guess the answer is a bit obvious, but I was looking for a specific method. There's no setter for the vertical position, but you can easily just use a combination of writer.GetVerticalPosition() and paragraph.SpacingBefore to achieve this result.

My solution:

cb.MoveTo(0f, 225f);
cb.SetLineDash(8, 4, 0);
cb.LineTo(doc.PageSize.Width, 225f);
cb.Stroke();

var pos = writer.GetVerticalPosition(false);

var p = new Paragraph("test", _myFont) { SpacingBefore = pos - 225f };
doc.add(p);
Pandincus
+1  A: 

Aside from SpacingBefore, the usual way to do it is by adding the text using the PdfContentByte instead of directly to the Document

// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap1002.pdf", FileMode.Create));
document.Open();

// we grab the ContentByte and do some stuff with it
PdfContentByte cb = writer.DirectContent;

// we tell the ContentByte we're ready to draw text
cb.beginText();

// we draw some text on a certain position
cb.setTextMatrix(100, 400);
cb.showText("Text at position 100,400.");

// we tell the contentByte, we've finished drawing text
cb.endText();
BlueRaja - Danny Pflughoeft
@BlueRaja Yeah, we can do that, but this still doesn't actually modify the writer's vertical position. So if I add a new Paragraph, Phrase, Chunk, Table, or other item, it will still appear elsewhere. I think.
Pandincus