In iTextSharp, can I set the vertical position of a pdfwriter file?

I recently started using iTextSharp to create PDF reports from data. It works very well.

In one specific report, I need the section to always appear at the bottom of the page. I use PdfContentByte to create a dashed line 200f from below:

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

Now I would like to add content below this line. However (as expected), the PdfContentByte methods do not change the vertical position of the PdfWriter. For example, new paragraphs appear on the page earlier.

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

Is there a way to instruct the pdfwriter that I would like to advance the vertical position below the dashed line and continue to paste the content there? There is a GetVerticalPosition () method - it would 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 a way to set the writer position manually? Thanks!

+4
source share
2 answers

Well, I think the answer is a little obvious, but I was looking for a specific method. There is no setter for upright position, but you can easily use a combination of writer.GetVerticalPosition () and paragraph.SpacingBefore to achieve this result.

My decision:

 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); 
+4
source

Beyond SpacingBefore, the usual way to do this is to add text using PdfContentByte and not 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(); 
+1
source

Source: https://habr.com/ru/post/1310503/


All Articles