Itext表行,表头,表格单元格

时间:2017-06-29 20:24:07

标签: itext

我需要使用java中的itext生成PDF

需要生成包含3行和2列的表,能够创建表。 国家Langugage 美国英语 加拿大法语

当我试图阅读pdf VIA NVDA桌面应用程序时,表格没有正确读取单元格,比如当我想把指针集中在加拿大时,应该是国家 - 加拿大

以下是代码

公共课测试{

public static final String CSS = "tr { text-align: center; } th { background-color: lightgreen; padding: 3px; } td {background-color: lightblue;  padding: 3px; }";

public static void main(String sunil[])
{
    String pFileName = "Test.pdf";
    String pFilePath = "C:/local/home/dna/pushlive/" + pFileName;
    try {

        FileOutputStream baosPDF = new FileOutputStream(pFilePath);
        com.itextpdf.text.Rectangle pageSize = new com.itextpdf.text.Rectangle(1836, 2376);
        com.itextpdf.text.Document document =  new  com.itextpdf.text.Document(pageSize, 36, 36, 36, 36);
        PdfWriter writer = PdfWriter.getInstance(document, baosPDF);
        writer.setTagged(); 

        document.open();

        PdfPTable bannerTable = new PdfPTable(4);


        document.add(createFirstTable());

        document.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
}

public static PdfPTable createFirstTable() {
    // a table with three columns
    PdfPTable table = new PdfPTable(2);

    // the cell object
    PdfPCell cell;
    // we add a cell with colspan 3
    //create header cell for first row
    // now we add a cell with rowspan 2


    //add metadata for each cell in the body.


    // we add the four remaining cells with addCell()
    cell = new PdfPCell(new Phrase("Country"));
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("Language"));
    table.addCell(cell);

    cell = new PdfPCell(new Phrase("USA")); 
    table.addCell(cell);


    cell = new PdfPCell(new Phrase("English")); 
    table.addCell(cell);

    cell = new PdfPCell(new Phrase("Canada"));  
    table.addCell(cell);

    cell = new PdfPCell(new Phrase("French"));  
    table.addCell(cell);


    //set header row for the table
    table.setHeaderRows(1);

    return table;
}

0 个答案:

没有答案