Я использую iText в Java, а также столкнулся с подобной ситуацией. Я просто пытался вставить простой разрыв строки с неформатированным текстом. Символ новой строки (\ n) не летает. Решение, которое я придумал (и оно не очень), было:
// read in the sourcefile
reader = new PdfReader(path + sourcefile);
// create a stamper instance with the result file for output
stamper = new PdfStamper(reader, new FileOutputStream(path + resultfile));
// get under content (page)
cb = stamper.getUnderContent(page);
BaseFont bf = BaseFont.createFont();
// createTemplate returns a PdfTemplate (subclass of PdfContentByte)
// (width, height)
template = cb.createTemplate(500, 350);
Stack linelist = new Stack();
linelist.push("line 1 r");
linelist.push("line 2 r");
linelist.push("line 3 r");
int lineheight = 15;
int top = linelist.size() * lineheight;
for (int i = 0; i < linelist.size(); i++) {
String line = (String) linelist.get(i);
template.beginText();
template.setFontAndSize(bf, 12);
template.setTextMatrix(0, top - (lineheight * i));
template.showText(line);
template.endText();
}
cb.addTemplate(template, posx, (posy-top));
stamper.close();
- Я разбиваю свои строки на массив / стек / список, что угодно
- Я зацикливаю этот список и устанавливаю текст по одной строке за раз
Должен быть лучший путь, но это сработало для моей ситуации (пока).