如何在iText中将段落与不同的块对齐

时间:2015-07-22 13:00:32

标签: itext text-alignment

我需要你的帮助来证明以下段落的对齐方式:

Font certifyFont = new Font(Font.TIMES_ROMAN, 16);
Paragraph certifyParagraph = new Paragraph();
certifyParagraph.setAlignment(Element.ALIGN_JUSTIFIED_ALL);

Chunk certifyText = new Chunk("This is to certify that  ", certifyFont);
certifyParagraph.add(certifyText);

Font nameFont = new Font(Font.TIMES_ROMAN, 16, Font.BOLD);
Chunk nameText = new Chunk("NAME ", nameFont);

Font workingFont = new Font(Font.TIMES_ROMAN, 16);
Chunk workingText = new Chunk("has been working in the company since the ", workingFont);

doc.open();
doc.add(certifyText);
doc.add(nameText);
doc.add(workingText);
doc.close();

我尝试将段落对齐为对齐,居中和右对齐,但对齐似乎没有反映在PDF文件中。所以请帮忙。

1 个答案:

答案 0 :(得分:0)

在您的代码中,您正在创建一个Paragraph对象certifyParagraph,并且您正在为其定义一个对齐方式,但您永远不会将其添加到文档中。相反,您要添加三个不同的Chunk对象,名为certifyTextnameTextworkingText

我已经调整了您的代码,以便将这三个Chunk对象添加到Paragraph对象中,以便将Paragraph对象添加到文档而不是单独的(对齐)无块):

Paragraph certifyParagraph = new Paragraph();
certifyParagraph.setAlignment(Element.ALIGN_JUSTIFIED_ALL);

Font certifyFont = new Font(Font.TIMES_ROMAN, 16);
Chunk certifyText = new Chunk("This is to certify that  ", certifyFont);
certifyParagraph.add(certifyText);

Font nameFont = new Font(Font.TIMES_ROMAN, 16, Font.BOLD);
Chunk nameText = new Chunk("NAME ", nameFont);
certifyParagraph.add(nameText);

Font workingFont = new Font(Font.TIMES_ROMAN, 16);
Chunk workingText = new Chunk(" has been working in the company since the ", workingFont);
certifyParagraph.add(workingText);

doc.open();
doc.add(certifyParagraph);
doc.close();