suresh
suresh

Reputation: 177

Itext place text in particular position with arial bold and some color

I need to use a Arial font bold with color and place the text in Particular position,

Font name = FontFactory.getFont("file/ResumeBuilder/arial.tiff", 22, Font.BOLD,new CMYKColor(0, 0, 0, 175));
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("helloWorld.pdf"));
document.open();
Paragraph paragraph = new Paragraph("Add in PDF document", name);
document.add(paragraph);

I tried using Paragraph but by using paragraph I am not sure how to place text in particular position or else is there any better way to achieve.

Upvotes: 1

Views: 1738

Answers (1)

mkl
mkl

Reputation: 95918

I assume you mean iText version 5.5.x (in particular because the task has become trivial in iText 7.x).

Furthermore I assume you mean adding a short piece of text without the need of line breaking.

In that case look at the iText example FoobarFilmFestival. The pivotal code:

Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
document.open();
String foobar = "Foobar Film Festival";
...
Font times = new Font(bf_times, 12);
...
PdfContentByte canvas = writer.getDirectContent();
...
Phrase phrase = new Phrase(foobar, times);
ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 200, 572, 0);
ColumnText.showTextAligned(canvas, Element.ALIGN_RIGHT, phrase, 200, 536, 0);
ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, phrase, 200, 500, 0);
ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, phrase, 200, 464, 30);
ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, phrase, 200, 428, -30);
document.close();

ColumnText.showTextAligned allows you to freely position a text line at a given position with a given alignment and angle.


If instead you mean adding a longer piece of text with the need of line breaking, look at the iText example MovieCalendar. The pivotal code:

Rectangle rect = getPosition(screening);
ColumnText column = new ColumnText(directcontent);
column.setSimpleColumn(new Phrase(screening.getMovie().getMovieTitle()),
        rect.getLeft(), rect.getBottom(),
        rect.getRight(), rect.getTop(), 18, Element.ALIGN_CENTER);
column.go();

This code draws the Phrase in the rectangle rect applying line breaks where necessary.

Upvotes: 1

Related Questions