如何为 PDFPTable 中的 PDF 单元格定义不同的字体样式
How to define different font styles to PDFCells in a PDFTable
我需要你的助手应用不同的字体样式来使字体变为粗体,并使 PDFTable
中的 header 单元格和下面的单元格居中对齐,而不是粗体,但左对齐方式不同。
使用当前代码,我只能将 header 单元格设置为粗体并将 header 内容居中对齐,我需要您的帮助来修改下面的字体动态生成的单元格并将对齐方式更改为左侧。那我该怎么做呢?
当前代码为:
dfPTable table = new PdfPTable(2);
Font earningsTitlefont = new Font(Font.TIMES_ROMAN,12, Font.BOLD);
PdfPCell c1 = new PdfPCell(new Phrase("Earnings Description",earningsTitlefont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Earnings Amount",earningsTitlefont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
for (int i = 0; i < listEarnings.size(); i++) {
String temp1 = listEarnings.get(i).getEarningsDescriptionSS();
String temp2 = listEarnings.get(i).getEarningsAmountSS();
table.addCell(temp1);
table.addCell(temp2);
}
您可以将这些字体和对齐属性包装为 Phrase
构造函数的参数,然后将其传递给 .addCell( )
。第一个参数。用于对齐,第三用于字体。
table.addCell( new Phrase(Element.ALIGN_LEFT,"text",new Font(..,..,...) ));
对于左对齐,您只需将元素属性设置为 Element.ALIGN_LEFT
& 要使用非粗体字体,请使用 Font.NORMAL
所以会是:
Font plainFont= new Font(Font.FontFamily.COURIER, 14,
Font.NORMAL);
for (int i = 0; i < listEarnings.size(); i++) {
String temp1 = listEarnings.get(i).getEarningsDescriptionSS();
String temp2 = listEarnings.get(i).getEarningsAmountSS();
table.addCell( new Phrase(Element.ALIGN_LEFT,temp1,plainFont));
table.addCell( new Phrase(Element.ALIGN_LEFT,temp2,plainFont));
// table.addCell(temp1);
// table.addCell(temp2);
}
只是为了让您记住 Font
class 来自 com.itextpdf.text.Font
而不是 java.awt.Font
我需要你的助手应用不同的字体样式来使字体变为粗体,并使 PDFTable
中的 header 单元格和下面的单元格居中对齐,而不是粗体,但左对齐方式不同。
使用当前代码,我只能将 header 单元格设置为粗体并将 header 内容居中对齐,我需要您的帮助来修改下面的字体动态生成的单元格并将对齐方式更改为左侧。那我该怎么做呢?
当前代码为:
dfPTable table = new PdfPTable(2);
Font earningsTitlefont = new Font(Font.TIMES_ROMAN,12, Font.BOLD);
PdfPCell c1 = new PdfPCell(new Phrase("Earnings Description",earningsTitlefont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Earnings Amount",earningsTitlefont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
for (int i = 0; i < listEarnings.size(); i++) {
String temp1 = listEarnings.get(i).getEarningsDescriptionSS();
String temp2 = listEarnings.get(i).getEarningsAmountSS();
table.addCell(temp1);
table.addCell(temp2);
}
您可以将这些字体和对齐属性包装为 Phrase
构造函数的参数,然后将其传递给 .addCell( )
。第一个参数。用于对齐,第三用于字体。
table.addCell( new Phrase(Element.ALIGN_LEFT,"text",new Font(..,..,...) ));
对于左对齐,您只需将元素属性设置为 Element.ALIGN_LEFT
& 要使用非粗体字体,请使用 Font.NORMAL
所以会是:
Font plainFont= new Font(Font.FontFamily.COURIER, 14,
Font.NORMAL);
for (int i = 0; i < listEarnings.size(); i++) {
String temp1 = listEarnings.get(i).getEarningsDescriptionSS();
String temp2 = listEarnings.get(i).getEarningsAmountSS();
table.addCell( new Phrase(Element.ALIGN_LEFT,temp1,plainFont));
table.addCell( new Phrase(Element.ALIGN_LEFT,temp2,plainFont));
// table.addCell(temp1);
// table.addCell(temp2);
}
只是为了让您记住 Font
class 来自 com.itextpdf.text.Font
而不是 java.awt.Font