使用itext生成pdf并在特定行中加粗

Lax*_*las 9 java itext

您好我能够使用iText生成包含数据的pdf表.如何在特定行中加粗特定数据?

ken*_*ohn 14

首先,您实例化具有所需详细信息的字体对象.在这里,您将指定它是否为Bold.

Font boldFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
Font normalFont = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.ITALIC);
Run Code Online (Sandbox Code Playgroud)

然后在您想要使用它的任何地方使用该字体.

为了添加具有粗体字体的表格单元格.

PdfPTable table=new PdfPTable(1);

PdfPCell pdfWordCell = new PdfPCell();
Phrase firstLine = new Phrase("text goes here", boldFont );
Phrase secondLine = new Phrase("normal text goes here", normalFont );

pdfWordCell.addElement(firstLine );
pdfWordCell.addElement(secondLine );

table.addCell(  pdfWordCell );
Run Code Online (Sandbox Code Playgroud)

使用粗体文本创建段落的示例.

Paragraph title = new Paragraph("Title of the document", boldFont );
Run Code Online (Sandbox Code Playgroud)

您可以在任何地方使用相同的实例,具体取决于API是否允许它.浏览文档以找出允许字体操作的内容.

有关更多示例,请参见此处

http://www.vogella.de/articles/JavaPDF/article.html