Itext将文本放置在特定位置,并带有arial粗体和一些颜色

时间:2016-07-26 09:32:23

标签: java itext

我需要使用粗体颜色的Arial字体并将文本放在特定位置

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);

我尝试使用段落但是使用段落我不知道如何将文本放在特定的位置,否则有更好的方法来实现。

1 个答案:

答案 0 :(得分:1)

我假设您的意思是iText版本5.5.x(特别是因为该任务在iText 7.x中变得微不足道)。

此外,我认为你的意思是在不需要换行的情况下添加一小段文字。

在这种情况下,请查看iText示例FoobarFilmFestival。关键代码:

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允许您使用给定的对齐和角度在给定位置自由定位文本行。

如果您的意思是添加更长的文本并需要换行,请查看iText示例MovieCalendar。关键代码:

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();

此代码在必要时应用换行符的矩形Phrase中绘制rect

相关问题