Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用iText创建具有重复标题的表_Java_Pdf_Itext - Fatal编程技术网

Java 使用iText创建具有重复标题的表

Java 使用iText创建具有重复标题的表,java,pdf,itext,Java,Pdf,Itext,我想创建一个带有重复标题的表,就像我贴在帖子上的图片一样 我想填充表左边的前三列到表的末尾,然后填充表右边的后三列 我使用了一个如页面所示的6列表。我根据日期顺序从数据库中获取数据,并希望将数据如图所示放入表中 我可以用java中的iText创建一个表,但我不知道如何用iText创建一个具有这种样式的表。如果可能的话,请帮助我 这是相对简单的 首先,我创建了一个表示单个数据项的类 static class Record{ public String firstname; publ

我想创建一个带有重复标题的表,就像我贴在帖子上的图片一样

我想填充表左边的前三列到表的末尾,然后填充表右边的后三列

我使用了一个如页面所示的6列表。我根据日期顺序从数据库中获取数据,并希望将数据如图所示放入表中

我可以用java中的iText创建一个表,但我不知道如何用iText创建一个具有这种样式的表。如果可能的话,请帮助我


这是相对简单的

首先,我创建了一个表示单个数据项的类

static class Record{
    public String firstname;
    public String lastname;
    public Record(String firstname, String lastname){
        this.firstname = firstname;
        this.lastname = lastname;
    }
    public String getFirstname(){return firstname;}
    public String getLastname(){return lastname;}
    public boolean equals(Object o){
        if(o instanceof Record){
            Record r = (Record) o;
            return r.firstname.equals(firstname) && r.lastname.equals(lastname);
        }
        return false;
    }
}
我还利用辛普森一家的角色,制作了一种随机生成此类对象的方法

private static Random RANDOM = new Random(System.currentTimeMillis());
public Record randomRecord(){
    String[] fn = {"Marge", "Homer", "Bart", "Lisa", "Nelson", "Apu", "Doris", "Seymour"};
    String[] ln = {"Bouvier", "Simpson", "Muntz", "Nahasapeemapetilan", "Skinner"};
    return new Record(fn[RANDOM.nextInt(fn.length)], ln[RANDOM.nextInt(ln.length)]);
}
生成表的代码如下所示:

    List<Record> recordList = new ArrayList<>();
    for(int i=0;i<17;i++)
        recordList.add(randomRecord());

    File outputFile = getOutputFile();
    PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outputFile));
    Document layoutDocument = new Document(pdfDocument);

    int numberOfItemsInFirstHalf = (int) Math.ceil(recordList.size() / 2.0);

    Table table = new Table(new float[]{0.16f, 0.16f, 0.16f, 0.16f, 0.16f, 0.16f});

    // header
    for(int i=0;i<2;i++) {
        table.addCell(new Cell().add(new Paragraph("Number")).setBackgroundColor(new DeviceRgb(74,189,172)));
        table.addCell(new Cell().add(new Paragraph("Firstname")).setBackgroundColor(new DeviceRgb(74,189,172)));
        table.addCell(new Cell().add(new Paragraph("Lastname")).setBackgroundColor(new DeviceRgb(74,189,172)));
    }

    // table
    for(int i=0;i<numberOfItemsInFirstHalf;i++){
        Record r0 = recordList.get(i);
        Record r1 = (i+numberOfItemsInFirstHalf) < recordList.size() ? recordList.get(i + numberOfItemsInFirstHalf) : null;

        table.addCell(new Cell().add(new Paragraph((i + 1)+"")));
        table.addCell(new Cell().add(new Paragraph(r0.getFirstname())));
        table.addCell(new Cell().add(new Paragraph(r0.getLastname())));

        if(r1 == null){
            table.addCell(new Cell().add(new Paragraph("")));
            table.addCell(new Cell().add(new Paragraph("")));
            table.addCell(new Cell().add(new Paragraph("")));
        }else{
            table.addCell(new Cell().add(new Paragraph((i + numberOfItemsInFirstHalf + 1)+"")));
            table.addCell(new Cell().add(new Paragraph(r1.getFirstname())));
            table.addCell(new Cell().add(new Paragraph(r1.getLastname())));
        }
    }

    layoutDocument.add(table);

    pdfDocument.close();
List recordList=new ArrayList();

对于(inti=0;i而言,这是相对简单的

首先,我创建了一个表示单个数据项的类

static class Record{
    public String firstname;
    public String lastname;
    public Record(String firstname, String lastname){
        this.firstname = firstname;
        this.lastname = lastname;
    }
    public String getFirstname(){return firstname;}
    public String getLastname(){return lastname;}
    public boolean equals(Object o){
        if(o instanceof Record){
            Record r = (Record) o;
            return r.firstname.equals(firstname) && r.lastname.equals(lastname);
        }
        return false;
    }
}
我还利用辛普森一家的角色,制作了一种随机生成此类对象的方法

private static Random RANDOM = new Random(System.currentTimeMillis());
public Record randomRecord(){
    String[] fn = {"Marge", "Homer", "Bart", "Lisa", "Nelson", "Apu", "Doris", "Seymour"};
    String[] ln = {"Bouvier", "Simpson", "Muntz", "Nahasapeemapetilan", "Skinner"};
    return new Record(fn[RANDOM.nextInt(fn.length)], ln[RANDOM.nextInt(ln.length)]);
}
生成表的代码如下所示:

    List<Record> recordList = new ArrayList<>();
    for(int i=0;i<17;i++)
        recordList.add(randomRecord());

    File outputFile = getOutputFile();
    PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outputFile));
    Document layoutDocument = new Document(pdfDocument);

    int numberOfItemsInFirstHalf = (int) Math.ceil(recordList.size() / 2.0);

    Table table = new Table(new float[]{0.16f, 0.16f, 0.16f, 0.16f, 0.16f, 0.16f});

    // header
    for(int i=0;i<2;i++) {
        table.addCell(new Cell().add(new Paragraph("Number")).setBackgroundColor(new DeviceRgb(74,189,172)));
        table.addCell(new Cell().add(new Paragraph("Firstname")).setBackgroundColor(new DeviceRgb(74,189,172)));
        table.addCell(new Cell().add(new Paragraph("Lastname")).setBackgroundColor(new DeviceRgb(74,189,172)));
    }

    // table
    for(int i=0;i<numberOfItemsInFirstHalf;i++){
        Record r0 = recordList.get(i);
        Record r1 = (i+numberOfItemsInFirstHalf) < recordList.size() ? recordList.get(i + numberOfItemsInFirstHalf) : null;

        table.addCell(new Cell().add(new Paragraph((i + 1)+"")));
        table.addCell(new Cell().add(new Paragraph(r0.getFirstname())));
        table.addCell(new Cell().add(new Paragraph(r0.getLastname())));

        if(r1 == null){
            table.addCell(new Cell().add(new Paragraph("")));
            table.addCell(new Cell().add(new Paragraph("")));
            table.addCell(new Cell().add(new Paragraph("")));
        }else{
            table.addCell(new Cell().add(new Paragraph((i + numberOfItemsInFirstHalf + 1)+"")));
            table.addCell(new Cell().add(new Paragraph(r1.getFirstname())));
            table.addCell(new Cell().add(new Paragraph(r1.getLastname())));
        }
    }

    layoutDocument.add(table);

    pdfDocument.close();
List recordList=new ArrayList();

对于(int i=0;i我不太明白您希望在拆分时如何处理表,并假设您正在搜索两列布局。如果是这样,则有一个预定义的
ColumnDocumentRenderer
类。如果在文档实例上设置,则允许将文档内容布局到列中

因此,我定义了列:

Rectangle pageSize=pdfDocument.getDefaultPageSize();
矩形[]区域=新矩形[2];
区域[0]=新矩形(36,36,((浮点)pageSize.getWidth()/2)-35.75f,pageSize.getHeight()-72);
区域[1]=新矩形((浮动)pageSize.getWidth()/2-0.25f,36,((浮动)pageSize.getWidth()/2)-35.75f,pageSize.getHeight()-72);

然后我在文档实例上应用了渲染器:
layoutDocument.setRenderer(新的ColumnDocumentRenderer(layoutDocument,areas));

拆分的结果如下所示:

守则全文如下:

    @Test
public void tableTest02() throws Exception {
    String outFileName = destinationFolder + "tableTest02.pdf";
    String cmpFileName = sourceFolder + "cmp_tableTest02.pdf";

    PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
    Document layoutDocument = new Document(pdfDocument);


    Table table = new Table(3);
    table.setWidth(UnitValue.createPercentValue(100));

    Rectangle pageSize = pdfDocument.getDefaultPageSize();
    Rectangle[] areas = new Rectangle[2];
    areas[0] = new Rectangle(36, 36, ((float) pageSize.getWidth() / 2) - 35.75f, pageSize.getHeight() - 72);
    areas[1] = new Rectangle((float) pageSize.getWidth() / 2 - 0.25f, 36, ((float) pageSize.getWidth() / 2) - 35.75f , pageSize.getHeight() - 72);

    layoutDocument.setRenderer(new ColumnDocumentRenderer(layoutDocument, areas));
    // header
    for(int i=0;i<1;i++) {
        table.addHeaderCell(new Cell().add(new Paragraph("Number")).setBackgroundColor(new DeviceRgb(74,189,172)));
        table.addHeaderCell(new Cell().add(new Paragraph("Firstname")).setBackgroundColor(new DeviceRgb(74,189,172)));
        table.addHeaderCell(new Cell().add(new Paragraph("Lastname")).setBackgroundColor(new DeviceRgb(74,189,172)));
    }

    // table
    for(int i=0;i<500;i++){

        table.addCell(new Cell().add(new Paragraph((i + 1)+"")));
        table.addCell(new Cell().add(new Paragraph("Hello")));
        table.addCell(new Cell().add(new Paragraph("World")));

    }

    layoutDocument.add(table);

    pdfDocument.close();

    Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff"));
}
@测试
public void tableTest02()引发异常{
字符串outFileName=destinationFolder+“tableTest02.pdf”;
字符串cmpFileName=sourceFolder+“cmp_tableTest02.pdf”;
PdfDocument PdfDocument=新的PdfDocument(新的PdfWriter(outFileName));
文档布局文档=新文档(PDF文档);
表=新表(3);
表.setWidth(UnitValue.createPercentValue(100));
矩形pageSize=pdfDocument.getDefaultPageSize();
矩形[]区域=新矩形[2];
区域[0]=新矩形(36,36,((浮点)pageSize.getWidth()/2)-35.75f,pageSize.getHeight()-72);
区域[1]=新矩形((浮点)pageSize.getWidth()/2-0.25f,36,((浮点)pageSize.getWidth()/2)-35.75f,pageSize.getHeight()-72);
setRenderer(新的ColumnDocumentRenderer(layoutDocument,areas));
//标题

对于(int i=0;i我不太明白您希望在拆分时如何处理表,并假设您正在搜索两列布局。如果是这样,则有一个预定义的
ColumnDocumentRenderer
类。如果在文档实例上设置,则允许将文档内容布局到列中

因此,我定义了列:

Rectangle pageSize=pdfDocument.getDefaultPageSize();
矩形[]区域=新矩形[2];
区域[0]=新矩形(36,36,((浮点)pageSize.getWidth()/2)-35.75f,pageSize.getHeight()-72);
区域[1]=新矩形((浮动)pageSize.getWidth()/2-0.25f,36,((浮动)pageSize.getWidth()/2)-35.75f,pageSize.getHeight()-72);

然后我在文档实例上应用了渲染器:
layoutDocument.setRenderer(新的ColumnDocumentRenderer(layoutDocument,areas));

拆分的结果如下所示:

守则全文如下:

    @Test
public void tableTest02() throws Exception {
    String outFileName = destinationFolder + "tableTest02.pdf";
    String cmpFileName = sourceFolder + "cmp_tableTest02.pdf";

    PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
    Document layoutDocument = new Document(pdfDocument);


    Table table = new Table(3);
    table.setWidth(UnitValue.createPercentValue(100));

    Rectangle pageSize = pdfDocument.getDefaultPageSize();
    Rectangle[] areas = new Rectangle[2];
    areas[0] = new Rectangle(36, 36, ((float) pageSize.getWidth() / 2) - 35.75f, pageSize.getHeight() - 72);
    areas[1] = new Rectangle((float) pageSize.getWidth() / 2 - 0.25f, 36, ((float) pageSize.getWidth() / 2) - 35.75f , pageSize.getHeight() - 72);

    layoutDocument.setRenderer(new ColumnDocumentRenderer(layoutDocument, areas));
    // header
    for(int i=0;i<1;i++) {
        table.addHeaderCell(new Cell().add(new Paragraph("Number")).setBackgroundColor(new DeviceRgb(74,189,172)));
        table.addHeaderCell(new Cell().add(new Paragraph("Firstname")).setBackgroundColor(new DeviceRgb(74,189,172)));
        table.addHeaderCell(new Cell().add(new Paragraph("Lastname")).setBackgroundColor(new DeviceRgb(74,189,172)));
    }

    // table
    for(int i=0;i<500;i++){

        table.addCell(new Cell().add(new Paragraph((i + 1)+"")));
        table.addCell(new Cell().add(new Paragraph("Hello")));
        table.addCell(new Cell().add(new Paragraph("World")));

    }

    layoutDocument.add(table);

    pdfDocument.close();

    Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff"));
}
@测试
public void tableTest02()引发异常{
字符串outFileName=destinationFolder+“tableTest02.pdf”;
字符串cmpFileName=sourceFolder+“cmp_tableTest02.pdf”;
PdfDocument PdfDocument=新的PdfDocument(新的PdfWriter(outFileName));
文档布局文档=新文档(PDF文档);
表=新表(3);
表.setWidth(UnitValue.createPercentValue(100));
矩形pageSize=pdfDocument.getDefaultPageSize();
矩形[]区域=新矩形[2];
区域[0]=新矩形(36,36,((浮点)pageSize.getWidth()/2)-35.75f,pageSize.getHeight()-72);
区域[1]=新矩形((浮点)pageSize.getWidth()/2-0.25f,36,((浮点)pageSize.getWidth()/2)-35.75f,pageSize.getHeight()-72);
setRenderer(新的ColumnDocumentRenderer(layoutDocument,areas));
//标题

对于(int i=0;我非常感谢您的回复。我测试了您的代码,但结果与我的要求不匹配。例如,如果我有250条记录,则记录分为两页,数字放在彼此下方而不是旁边。在第一页,最后一个数字是53,第二页的第一个数字是54,这是错误的。我想要54th记录在第一页的右边第二个三列53处。是的,这段代码显然没有考虑分页。如果你想这样做,你需要跟踪表的拆分位置。但是基本原理是一样的。谢谢。有什么方法可以找出表的拆分位置吗?添加时对于文档的IELENT,您可以要求呈现程序提供其子级。如果表被拆分,您可能会看到SplitRenderer。我搜索了intenet,但找不到任何使用SplitRenderer的示例。我是一名新手,到目前为止,我使用的是iText的简单结构,因此我对SplitRendere没有任何概念