如何在 iText 中将段落与不同的块对齐
How to align paragraph with different chunks in iText
我需要帮助来证明以下段落的对齐方式:
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 文件中。
在您的代码中,您正在创建一个 Paragraph
对象 certifyParagraph
并为其定义对齐方式,但您从未将其添加到文档中。相反,您要添加三个不同的 Chunk
对象,分别命名为 certifyText
、nameText
和 workingText
.
我已经调整了您的代码,以便将这三个 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();
我需要帮助来证明以下段落的对齐方式:
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 文件中。
在您的代码中,您正在创建一个 Paragraph
对象 certifyParagraph
并为其定义对齐方式,但您从未将其添加到文档中。相反,您要添加三个不同的 Chunk
对象,分别命名为 certifyText
、nameText
和 workingText
.
我已经调整了您的代码,以便将这三个 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();