In itext I have a chunk/phrase/paragraph (I dont mind which) and I want to position some where else on the page e.g. at 300 x 200. How would I do this?
Asked
Active
Viewed 4.2k times
5 Answers
26
In the end I wrote my own method to do it.
private void PlaceChunck(String text, int x, int y) {
PdfContentByte cb = writer.DirectContent;
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.SaveState();
cb.BeginText();
cb.MoveText(x, y);
cb.SetFontAndSize(bf, 12);
cb.ShowText(text);
cb.EndText();
cb.RestoreState();
}
-
1Hi. Does it support newlines? – KarolDepka Nov 05 '09 at 04:14
-
6I won't downvote this answer, but I don't like it. The question is: how do I position a chunk, phrase, paragraph. The correct answer is: by using ColumnText. Either you can use the showTextAligned() method (for Phrase objects only), or you can use a ColumnText instance and the go() method if you need support for newlines and/or objects other than Phrase. – Bruno Lowagie Jan 30 '13 at 09:46
-
2@BrunoLowagie - how about a full answer then? It would be helpful to see an explanation of the right way. – Jason Kleban Jul 03 '13 at 00:39
-
1The full answer can be found in the documentation. For instance the book http://itextpdf.com/book and the examples http://tinyurl.com/itextsharpIIA2C03 I'm currently writing a (free e)book. There are only 24 hours in one day, you shouldn't expect people to write their book on SO. – Bruno Lowagie Jul 03 '13 at 07:35
-
6@BrunoLowagie thanks for the book, but I totally dislike your approach. If you have a better solution you can post it on SO, if you don't like the question you can downvote it. You did neither. Moreover this might not be the best possible approach but I haven't the time to read a full book and this answer provided me a viable solution to create my example. There are only 24 hours in one day, you shouldn't expect people to read books while they have to work. – Gabber Nov 25 '13 at 11:35
-
People shouldn't accept jobs they aren't qualified for. That falsifies the market place. Moreover, I have given a full answer: the link points to examples that can be readily used. And on top of that, I've answered the question elsewhere on SO. – Bruno Lowagie Nov 25 '13 at 12:44
-
I added cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE_CLIP); to make it work – ihebiheb Sep 10 '15 at 08:39
21
Here's a version with all the correct casing and try/catch block:
private static void absText(String text, int x, int y) {
try {
PdfContentByte cb = writer.getDirectContent();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.saveState();
cb.beginText();
cb.moveText(x, y);
cb.setFontAndSize(bf, 12);
cb.showText(text);
cb.endText();
cb.restoreState();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

aaronbartell
- 1,010
- 1
- 12
- 19
-
writer.getDirectContent() error if I used Document document = new Document(); Font times = null ; // step 2 PdfWriter writer; try { writer = PdfWriter.getInstance(document, new FileOutputStream(path)); then it Made New Document And I wnt same doc with edit function – Sunil Chaudhary Oct 07 '15 at 06:32
3
I did something along these lines, maybe it will help others:
ColumnText ct = new ColumnText(writer.getDirectContent());
ct.setSimpleColumn(left,bottom,right,top);
ct.setText(new Phrase("String"));
ct.go();

JstnPwll
- 8,585
- 2
- 33
- 56
0
In my case only this solution worked fine.
PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(FILE));
Add method
private void addTextData(Document document, String text) {
PdfContentByte cb = pdfWriter.getDirectContent();
cb.beginText();
try {
BaseFont f_cn = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.setFontAndSize(f_cn, 40);
float requiredX = 50;
float requiredY = 50;
Paint paint = new Paint();
paint.setTextSize(40);
Typeface typeface=Typeface.createFromAsset(getAssets(), "Helvetica.ttf");
paint.setTypeface(typeface);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL);
Rect result = new Rect();
paint.getTextBounds(text, 0, text.length(), result);
Log.i("Text dimensions", "Width: "+result.width()+"-Height: "+result.height());
float calculatedY = document.getPageSize().getHeight() - result.height() - requiredY;
cb.setTextMatrix(requiredX, calculatedY);
cb.showText(text);
cb.endText();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
This will help you for sure.

Hiren Patel
- 52,124
- 21
- 173
- 151
0
Hope this help you! Here is my code...
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, output);
document.open();
FixText("Add Your Text",400,700,writer,14);
document.close();
Add Function:
private static void FixText(String text, int x, int y,PdfWriter writer,int size) {
try {
PdfContentByte cb = writer.getDirectContent();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.saveState();
cb.beginText();
cb.moveText(x, y);
cb.setFontAndSize(bf, size);
cb.showText(text);
cb.endText();
cb.restoreState();
} catch (DocumentException | IOException e) {
e.printStackTrace();
}
}

Saurabh Gaddelpalliwar
- 1,535
- 18
- 19