我使用PdfPTable使用com.itextpdf.text.pdf.PdfPTable
将表格数据转换为pdf文件。表正在显示,但表数据和标题是相同的样式。为了有所作为,我必须将标题字体样式设置为粗体。有人可以帮助我吗?我在这里附上了我的代码。
提前致谢。
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import javax.faces.model.ListDataModel;
import com.mypackage.core.filter.domainobject.FilterResultDO;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPTable;
public class PDFGenerator {
//This method will generate PDF for Filter Result Screen (only DataTable level)
@SuppressWarnings("unchecked")
public static PdfPTable generatePDF(PdfPTable table,List<FilterResultDO> filterResultDOList ,List<ColumnHeader> filterResultHeaderList )
{
//Initialize the table with number of columns required for the Datatable header
int numberOfFilterLabelCols = filterResultHeaderList.size();
//PDF Table Frame
table = new PdfPTable(numberOfFilterLabelCols);
//Getting Filter Detail Table Heading
for(int i = 0 ; i < numberOfFilterLabelCols; i++)
{
ColumnHeader commandHeaderObj = filterResultHeaderList.get(i);
table.addCell(commandHeaderObj.getLabel());
}
//Getting Filter Detail Data (Rows X Cols)
FilterResultDO filterResultDOObj = filterResultDOList.get(0);
List <List> filterResultDataList = filterResultDOObj.getFilterResultLst();
int numberOfFilterDataRows = filterResultDataList.size();
//each row iteration
for(int row = 0; row < numberOfFilterDataRows; row++)
{
List filterResultCols = filterResultDataList.get(row);
int numberOfFilterDataCols = filterResultCols.size();
//columns iteration of each row
for(int col = 0; col < numberOfFilterDataCols ; col++)
{
String filterColumnsValues = (String) filterResultCols.get(col);
table.addCell(filterColumnsValues);
}
}
return table;
}//generatePDF
}
答案 0 :(得分:1)
我认为commandHeaderObj.getLabel()方法返回一个字符串。 你只需要定义一个字体(在方法中,或作为实例属性或静态最终类属性......)
Font tableHeader =
FontFactory.getFont(FontFactory.HELVETICA, 10, Font.BOLD);
然后将其应用于单元格
for(int i = 0 ; i < numberOfFilterLabelCols; i++)
{
ColumnHeader commandHeaderObj = filterResultHeaderList.get(i);
Paragraph header = new Paragraph();
header.setFont(tableHeader);
header.add(commandHeaderObj.getLabel());
table.addCell(header);
}
希望这个帮助
纪尧姆