【问题标题】:How to fill the dynamic data with each column row by row Using iText in Java如何在Java中使用iText逐行填充每一列的动态数据
【发布时间】:2016-08-08 04:43:26
【问题描述】:

我想通过动态数据生成一个包含 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 和空白单元格。

【问题讨论】:

  • 你能澄清一下你正在尝试做什么以及到目前为止你已经尝试过什么吗?
  • 我想在我的 pdf 中这样打印:在第一页它将打印 1 2 3 然后在下一页它将打印 4 5 6 然后在下一页它将打印 7 8 和空白细胞。

标签: java itext


【解决方案1】:

为了转到下一页,您必须使用 Document 类中的 newPage() 方法。可以看一下newPagehere的例子。

这是一个可能的解决方案。它肯定可以改进,它只是按照您的要求做的事情:

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

【讨论】:

    【解决方案2】:

    从纯学术的角度来看,这里是代码。优化了吗,没有。我刚拿了 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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-09-28
      • 2013-01-26
      • 1970-01-01
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多