ITextSharp and placing rich text at a specific position
I have created a PDF document using Adobe LiveCycle. The problem I'm running into is that I need to add some formatted text at a specific position in the document.
How can i do this?
overContent.BeginText();
overContent.SetTextMatrix(10, 400);
overContent.ShowText("test");
This will add the body text at the specified position and I really need the line, bullets, etc. in the document.
+1
source to share
1 answer
I am using iText in Java and also faced a similar situation. I was just trying to insert a simple line break with unformatted text. The newline character (\ n) does not fly. The solution I came across (and it's pretty pretty):
// 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();
- I am splitting my strings into an array / stack / list
- i loop a trhu that contains the list and setText one line at a time
There should be a better way, but it worked for my situation (so far).
+2
source to share