如何使用Java中的iText逐行填充每列的动态数据

时间:2016-08-08 04:43:26

标签: java itext

我想通过动态数据生成包含3列的PDF。我试过我的代码,

public class Clazz {

public static final String RESULT = "result.pdf";
private    String[] data = {"1","2","3","4","5","6","7","8"};

private void go() throws Exception {

    Document doc = new Document();
    PdfWriter.getInstance(doc, new FileOutputStream(RESULT));
    doc.open();

    PdfPTable mainTable = new PdfPTable(3);
    PdfPCell cell;

    for (int i = 0; i < data.length; i+=2) {
        cell = new PdfPCell(new Phrase(data[i]));
        PdfPTable table = new PdfPTable(1);
        table.addCell(cell);
        if (i+1 <= data.length -1) {
           cell = new PdfPCell(new Phrase(data[i + 1]));
           table.addCell(cell);
        } else {
            cell = new PdfPCell(new Phrase(""));
            table.addCell(cell);
        }
        mainTable.addCell(table);
    }

    doc.add(mainTable);
    doc.close();

}
}

我想在我的PDF中打印出这样的内容:

  

在第一页中,它将打印1 2 3,然后在下一页中将打印   4 5 6然后在下一页中它将打印7 8和空白单元格。

2 个答案:

答案 0 :(得分:0)

要转到下一页,您必须使用文档类中的 newPage()方法。您可以看到newPage here的示例。

这是一个可能的解决方案。它肯定可以改进,它只是你所要求的东西:

private static void go() throws Exception {
    Document doc = new Document();
    PdfWriter.getInstance(doc, new FileOutputStream(RESULT));
    doc.open();

    PdfPTable mainTable = new PdfPTable(3);
    PdfPCell cell;

    //went to <= data.length to cover the case of empty cell after 7,8
    //a better approach could be more suitable here
    for (int i = 0; i <= data.length; i++) {
        PdfPTable table = new PdfPTable(1);
        if (i < data.length) {
           cell = new PdfPCell(new Phrase(data[i]));
           table.addCell(cell);
        } else {
            cell = new PdfPCell(new Phrase(""));
            table.addCell(cell);
        }

        //go to next page after completing each row
        if(i != 0 && i%3 == 0)
        {
          doc.add(mainTable);
          doc.newPage();
          mainTable =  new PdfPTable(3);
        }

        mainTable.addCell(table);
    }

    doc.add(mainTable);
    doc.close();
}

答案 1 :(得分:0)

从纯粹的学术角度来看,这是代码。它是否优化,没有。我刚刚做了3件事 a)需要一个包含3列的表格 b)每行后需要分页符 c)空白单元格需要填充“”

import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class CreateTable {

    /** The resulting PDF file. */
    public static final String RESULT = "first_table.pdf";

    /**
     * Main method.
     * 
     * @param args
     *            no arguments needed
     * @throws DocumentException
     * @throws IOException
     */
    public static void main(String[] args) throws IOException, DocumentException {
        new CreateTable().createPdf(RESULT);
    }

    /**
     * Creates a PDF with information about the movies
     * 
     * @param filename
     *            the name of the PDF file that will be created.
     * @throws DocumentException
     * @throws IOException
     */
    public void createPdf(String filename) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        createCustomTables(document);
        // step 5
        document.close();
    }

    public static void createCustomTables(Document document) throws DocumentException {
        String[] data = { "1", "2", "3", "4", "5", "6", "7", "8" };
        PdfPTable table = null;
        int arrayLength = data.length;
        int mod3 = data.length % 3;
        int iterCounter = (mod3 == 0) ? arrayLength : arrayLength + (3 - mod3);

        System.out.println(iterCounter);

        for (int i = 0; i <= iterCounter; i++) {
            String string = (arrayLength > i) ? data[i] : "";
            if (i % 3 == 0) {
                table = new PdfPTable(3);
                document.newPage();
            }
            PdfPCell cell = new PdfPCell(new Phrase(string));
            table.addCell(cell);
            document.add(table);
        }
        return;
    }
}