我需要使用 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);
我尝试使用段落,但通过使用段落,我不确定如何将文本放置在特定位置,否则是否有更好的方法来实现。
最佳答案
我假设您指的是 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();
此代码在矩形 rect
中绘制 Phrase
,并在必要时应用换行符。
关于java - Itext 将文本放置在特定位置,并带有粗体和一些颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38586148/