根据itext官方回复http://developers.itextpdf.com/question/how-format-string-resulting-two-column-display 实施;
方法声明为:public ByteBuffer outputPDF(File file,String hints,String[] title,List<String[]> contents,int[] widths)
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese_italic = new Font(bfChinese, 13, Font.ITALIC, BaseColor.LIGHT_GRAY);
Font fontChinese_title = new Font(bfChinese, 14, Font.BOLD, BaseColor.BLACK);
Font fontChinese_content = new Font(bfChinese, 14, Font.NORMAL, BaseColor.BLACK);
Paragraph hintT = createParagraphWithTab(fontChinese_title,title);
// Paragraph hintT = createParagraphWithSpaces(fontChinese_title,title);
// document.add(hintT);
//
// int sz = contents.size();
// for(int j=0;j<sz;j++)
// {
Paragraph hintC = createParagraphWithTab(fontChinese_content,contents.get(j));
// Paragraph hintC = createParagraphWithSpaces(fontChinese_content, contents.get(j));
// document.add(hintC);
// }
PdfPTable table = new PdfPTable(7);//7为列,列要写,行可以不写
table.setWidthPercentage(100);
table.setHorizontalAlignment(Element.ALIGN_LEFT);
table.setWidths({2,1,1,1,2,1,4});//这7列,每列的相对宽度,本身宽度与总和商是列宽比例,列宽数组大小要与之前构造函数中的列数严格一致
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
for(String tt:title)
{
table.addCell(new Phrase(10,tt,fontChinese_title));
}
for(String[] ss:contents)
{
for(String s:ss)
{
table.addCell(new Phrase(10,s,fontChinese_content));
}
}
document.add(table);
private Paragraph createParagraphWithTab(Font font, String[] content) {
Paragraph p = new Paragraph();
p.setFont(font);
// p.set
for(String s:content)
{
p.add(s);
p.add(Chunk.TAB);
}
return p;
}
private Paragraph createParagraphWithSpaces(Font font, String[] content) {
Paragraph p = new Paragraph();
p.setFont(font);
for(String s:content)
{
p.add(String.format("%-10s", s));
}
return p;
}