使用iText设置标题行的格式 [英] set format of header row using iText

查看:114
本文介绍了使用iText设置标题行的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iText5从CSV生成PDF文件。我需要格式化文件,以便标题行(发生在每个页面的开头)应该在



更大的字体



粗体



以背景颜色突出显示



为了清楚起见,我知道如何设置字体样式/大小/颜色。我很难找到如何为标题行



我该怎么做?请帮助。



提前致谢

解决方案

您的要求已解释详情请参阅教程视频,更具体地说,请参阅 United States 示例。在此示例中,我们采用包含美国不同州的CSV文件: united_states.csv

  name; abbr; capital;人口最多的城市;人口;平方英里;时区1;时区2; dst 
ALABAMA; AL; Montgomery; Birmingham; 4,708,708; 52,423; CST(UTC-6); EST(UTC-5); YES
ALASKA; AK; Juneau; Anchorage; 698,473; 656,425; AKST( UTC-09); HST(UTC-10); YES
ARIZONA; AZ; Phoenix; Phoenix; 6,595,778; 114,006; MT(UTC-07); ; NO
ARKANSAS; AR;小石城;小石城; 2,889,450; 53,182; CST(UTC-6); ; YES
CALIFORNIA; CA; Sacramento;洛杉矶; 36,961,664; 163,707; PT(UTC-8); ; YES

我们将这些解析成带有重复标题的PDF: united_states.pdf



唯一的区别是,您希望标题中的文本为粗体。这只需要对代码进行最小的更改:

  public void createPdf(String dest)抛出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();
进程(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));
}
}

仔细看看<$ c $的价格c> process()方法已更改:它现在接受 font 参数,以便我们可以为标题定义更大,更大胆的字体。 / p>

I am generating a PDF file from a CSV using iText5. I need to format the file so that the header row (which occurs in the beginning of every page) should be in a

bigger font

bold and

highlighted by a background colour

Just to be clear, I know how to set the font style/size/colour. I'm having a tough time finding out how to do that for the header rows

How can I do that? Please help.

Thanks in advance

解决方案

Your requirement is explained in large detail in our tutorial video, more specifically in the UnitedStates example. In this example, we take a CSV file containing the different states of the US: united_states.csv

name;abbr;capital;most populous city;population;square miles;time zone 1;time zone 2;dst
ALABAMA;AL;Montgomery;Birmingham;4,708,708;52,423;CST (UTC-6);EST (UTC-5);YES
ALASKA;AK;Juneau;Anchorage;698,473;656,425;AKST (UTC-09) ;HST (UTC-10) ;YES
ARIZONA;AZ;Phoenix;Phoenix;6,595,778;114,006;MT (UTC-07); ;NO
ARKANSAS;AR;Little Rock;Little Rock;2,889,450;53,182;CST (UTC-6); ;YES
CALIFORNIA;CA;Sacramento;Los Angeles;36,961,664;163,707;PT (UTC-8); ;YES

And we parse these into a PDF with a repeating header: united_states.pdf

The only difference, is that you want the text in the header to be bold. This requires only a minimal change to the code:

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));
    }
}

Take a close look at how the process() method was changed: it now accepts a font parameter so that we can define a bigger, bolder font for the header.

这篇关于使用iText设置标题行的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆