1

I am currently using ITextSharp, and following along with IText in Action Second edition, specifically page 74 working with the ColumnText object.

I am loading a PDF file using the PDFReader and PDFStamper classes, and trying to add multiple Elements in the PDF document.

 PdfStamper pdfStamper = new PdfStamper(pdfReader, fileStream);
 PdfContentByte canvas = pdfStamper.GetOverContent(1);
 Rectangle size = pdfReader.GetPageSizeWithRotation(1);


 ColumnText ct = new ColumnText(canvas);
 //I have multiple calls  like below, with different x,y coords
 ct.SetSimpleColumn(10, 634, 100, 642);
 ct.AddText(new Chunk("1234567890 999 9 9 999000 0 877"));
 ct.Go();

What I am noticing is the text will stop and then start writing over itself (the same column?). I basically need it to just stop when it gets to the end of the boundary. I do not need the extra text to show in another column.

I have read about the ColumnText.HasMoreText check, and also what the Go() returns. In this case, it will return NO_MORE_COLUMNS which I think is 2.

I am not understanding how to get the truncating to work correctly without the text writing on top of itself.

The text displays as "019 298397 4795 69 7989990000" which you can see it starts to overwrite on itself.

If anyone can assist me with this Id appreciate it. If i need clarification, let me know too.

abbottmw
  • 752
  • 1
  • 5
  • 19
  • I've tried reproducing the problem with the most recent version of iText and I can't reproduce the problem. Maybe you're using an older version and experiencing a bug that has been fixed. – Bruno Lowagie Nov 28 '13 at 08:35
  • Okay I updated the ITextSharp to 5.4.4, and I am still getting the overlapping. Could you possibly show me your test code so I can see if it does it for me? – abbottmw Nov 28 '13 at 15:17
  • I added a [Gist](https://gist.github.com/abbottmw/7705800) to some sample code that is doing what I am talking about. If you run that code and look at Test2.pdf you will see the text overlapping. Again, if I am missing something, let me know. – abbottmw Nov 29 '13 at 13:42

1 Answers1

4

OK, I've finally found some time to look at your example.

First this: you define a Font, but you don't use it anywhere. The default font is used for the Phrase (Helvetica 12pt, with a leading of 18pt).

A leading of 18pt, means that you'll never be able to fit the text between y = 634 and y = 642. Let's make this y = 634 and y = 652.

Finally, you're using text mode. Most of the times it's better to use composite mode. When you change your code like this, it will work:

PdfReader reader = new PdfReader("resources/hello.pdf");
using (FileStream fileStream = new FileStream("PDF/Test2.pdf", FileMode.Create, FileAccess.Write)) {
    PdfStamper stamper = new PdfStamper(pdfReader, fileStream);
    ColumnText ct = new ColumnText(stamper.GetOverContent(1));
    ct.SetSimpleColumn(10, 634, 100, 652, 0, Element.ALIGN_LEFT);
    ct.AddElement(new Phrase("1234567890 999 9 9 999000 0 877"));
    ct.Go();
    stamper.Close();
}
reader.Close();
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thank you for your help. I got this to work with your help. I do have a silly question though. How do you figure out if the text will fit between the Y's? Is it just leading that I have to look for? What is the "formula" to figure that out? Does font size play a part too? – abbottmw Dec 02 '13 at 19:29
  • 1
    The height is determined by different factors: *font size* is an average height of the glyphs. *Leading* is the distance between the base lines. By default, iText uses 1.5 times the font size. So for a font size of 12, iText takes a leading of 18 by default (you can, of course, change this value). If you want the exact height, please take a look at http://itextpdf.com/examples/iia.php?id=61 The difference between the return value of `getAscentPoint()` and `getDescentPoint()` is the actual height. – Bruno Lowagie Dec 03 '13 at 08:25