0

I'm using iText and Java to modify pdf-document. When I try to write a string with new line characters inside, in pdf it is shown in only one line.

Here is some code I use,

static PdfContentByte cb;
cb = writer.getDirectContent();
.
.
.
cb.setTextMatrix(48, 285);
cb.showText(str);

but str appears in a single line.

How to add newline in iText?

Jongware
  • 22,200
  • 8
  • 54
  • 100
Xdeveloper
  • 57
  • 1
  • 8
  • 1
    @Xdeveloper, you are using the most low level API of iText here (`setTextMatrix`, `showText`) which directly corresponds to certain PDF operations (*Tm*, *Tj*). These operations don't know new line characters as something special, they only draw character after character. At this API level you need to split up the string and draw each line individually. – mkl Nov 30 '15 at 05:15
  • Please read the documentation on the official iText site before posting a question. In your case, read the section about [adding text at absolute positions](http://developers.itextpdf.com/faq/category/absolute-positioning-text). Currently, you are using an approach only PDF specialists should use... – Bruno Lowagie Nov 30 '15 at 06:14
  • @mkl, Bruno Lowagie, thanks for your help. I will read more before posting, you are right Bruno. – Xdeveloper Nov 30 '15 at 19:41
  • @Jongware, why did you delete my answer? I think it's good to explain to people when you change their text, so they can learn. Thanks – Xdeveloper Dec 12 '15 at 14:41
  • Answers do not belong in Questions - they belong in *Answers*. Your question has been marked as duplicate of another; if you think your answer is helpful, you can post it there. If you feel your question is *not* a duplicate, please edit your question and point out how the duplicate does not answer it. – Jongware Dec 12 '15 at 20:34
  • Ok. The "duplicated" question is about how to add text in rectangle. The answer in that question does not help me to solve the problem, thats why I think, this question is not a duplicate. I have found a solution, and that can help others. This website is like Putin-made :). I have a solution so I dont need to answer again. – Xdeveloper Dec 12 '15 at 21:59
  • But you can still add your solution to the original question! If you found the answer there does not work under all circumstances, yours may help someone with a similar problem. – Jongware Dec 13 '15 at 11:01
  • No, I can not add my solution to the original question, there is no relation between them. – Xdeveloper Dec 15 '15 at 12:26

0 Answers0