2

I am producing PDF file using itextsharp,

I am printing 2 strings leading1 & leading2.

The problem is when ever leading1 lenght increases, its effecting leading2 and its gets trimmed.

But I want to print leading1 & leading2 in next line, if no.of characters in leading1 is increased.

Basically leading2 is hardcoded as = YOU DIDIT. But leading1 is dynamic value.

So , I just want to know how to position and wrap long-text.

Here is my code...

Can anyone help me in doing this?

PdfContentByte cb = writer.getDirectContent();
        cb.saveState();
        cb.beginText();
        cb.setFontAndSize(baseFontMedium, 10f);

        // float x = 6.4392f * commonView.INCH;
        float x = 6.47f * commonView.INCH;
        float y = pageSize.getHeight() - (1.13f * commonView.INCH);

        cb.setCMYKColorFillF(0f, 0f, 0f, 0.77f); 
        cb.setTextMatrix(1, 0, 0, 1, x, y); 
        cb.showText(leading1);

        x += new Chunk(leading1, fontMedium10Pt).getWidthPoint();

        cb.setCMYKColorFillF(1f, 0f, 0f, 0f);
        cb.setTextMatrix(1, 0, 0, 1, x, y);
        cb.showText(leading2);

        cb.endText();
        cb.restoreState()
user1660325
  • 747
  • 4
  • 20
  • 35

1 Answers1

5

You have chosen to add text using PDF syntax at the lowest level. This means that you need to calculate the length of every single piece of text that you are adding to your document, and then distribute the text by adding it in different showText() sequences, making sure to adjust the coordinates correctly.

That is hard.

However, you have also chosen to use iText, which means that you can have iText do this work for you. For instance: if you want to add a snippet of text inside a specific rectangle, then you can define a ColumnText object, define a Rectangle, add the text as a Paragraph and go()!

PdfContentByte cb = writer.getDirectContent();
ColumnText ct = new ColumnText(cb);
ct.setSimpleColumn(new Rectangle(36, 600, 200, 800));
ct.addElement(new Paragraph("I want to add this text in a rectangle defined by the coordinates llx = 36, lly = 600, urx = 200, ury = 800"));
int status = ct.go();

Now the text "I want to add this text in a rectangle defined by the coordinates llx = 36, lly = 600, urx = 200, ury = 800" will be wrapped inside a rectangular area defined by the coordinates llx = 36, lly = 600, urx = 200, ury = 800. The status variable will give you an indication whether or not the text was fully rendered (or if it didn't fit completely).

Further reading:

If this example helps you, please help me understand what I can do to make sure that other developers do not make the same mistake you made, and explain what made you write the code you wrote instead of trying ColumnText first. Your information will help me in writing a better book.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • A useful explanation of the llx, lly, urx, and ury parameters is here: http://stackoverflow.com/questions/30748651/how-should-i-interpret-the-coordinates-of-a-rectangle-in-pdf – Elijah Lofgren Oct 03 '16 at 20:12