您的要求在我们的教学视频中进行了详细说明,更具体地说是在United
States示例中。在此示例中,我们获取一个包含美国不同州的CSV文件:united_states.csv
name;abbr;capital;most populous city;population;square miles;time zone 1;time zone 2;dstALABAMA;AL;Montgomery;Birmingham;4,708,708;52,423;CST (UTC-6);EST (UTC-5);YESALASKA;AK;Juneau;Anchorage;698,473;656,425;AKST (UTC-09) ;HST (UTC-10) ;YESARIZONA;AZ;Phoenix;Phoenix;6,595,778;114,006;MT (UTC-07); ;NOARKANSAS;AR;Little Rock;Little Rock;2,889,450;53,182;CST (UTC-6); ;YESCALIFORNIA;CA;Sacramento;Los Angeles;36,961,664;163,707;PT (UTC-8); ;YES
然后将它们解析为带有重复标头的PDF:united_states.pdf
唯一的区别是,您希望标题中的文本为粗体。这只需要对代码进行最小的更改:
public void createPdf(String dest) throws IOException, documentException { document document = new document(PageSize.A4.rotate()); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); PdfPTable table = new PdfPTable(9); table.setWidthPercentage(100); table.setWidths(new int[]{4, 1, 3, 4, 3, 3, 3, 3, 1}); BufferedReader br = new BufferedReader(new FileReader(DATA)); String line = br.readLine(); process(table, line, new Font(FontFamily.HELVETICA, 16, Font.BOLD)); table.setHeaderRows(1); while ((line = br.readLine()) != null) { process(table, line, new Font(FontFamily.HELVETICA, 12)); } br.close(); document.add(table); document.close();}public void process(PdfPTable table, String line, Font font) { StringTokenizer tokenizer = new StringTokenizer(line, ";"); while (tokenizer.hasMoreTokens()) { table.addCell(new Phrase(tokenizer.nextToken(), font)); }}
仔细看一下
process()方法的更改方式:它现在接受一个
font参数,以便我们可以为标题定义更大,更粗体的字体。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)