iText拆分表多次打印

时间:2016-09-30 05:29:31

标签: java itext

我试图在x行之后拆分一个表,所以我在页面上没有孤立的签名块(即,在页面上的签名块之前总是至少有一行表)。在我的测试中,我在表格中打印了六行。第二组打印为第二组打印为两个相同的表格,其间有三个空白行,第三组打印为三个相同的表格,它们之间有三个空白行。

如何删除重复的表格?代码是:

      Paragraph preface = new Paragraph();

      PdfPTable table = new PdfPTable(3);
      table.setWidths(new int[]{1, 3, 1});
      table.setHeaderRows(1);

      PdfPCell c1 = new PdfPCell(new Phrase("Section"));
      c1.setHorizontalAlignment(Element.ALIGN_CENTER);
      table.addCell(c1);

      c1 = new PdfPCell(new Phrase("Award"));
      c1.setHorizontalAlignment(Element.ALIGN_CENTER);
      table.addCell(c1);

      c1 = new PdfPCell(new Phrase("Date"));
      c1.setHorizontalAlignment(Element.ALIGN_CENTER);
      table.addCell(c1);
      table.setHeaderRows(1);

      String storedName = null;
      int noRows = 0;
      DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
      DateFormat df2 = new SimpleDateFormat("dd-MM-yyyy");

      for (final Transcript scoutNamesDescription : listymAwards) {
          if (noRows > 1){ // Change this to number of rows required
              noRows = 0;
              preface.add(table);
              document.add(preface);
              document.newPage();
              // We add three empty lines
              addEmptyLine(preface, 1);
              addEmptyLine(preface, 1);
              addEmptyLine(preface, 1);

              table.flushContent();
          }

          noRows++;

          if (scoutNamesDescription.getSection().equals(storedName)){
              table.addCell(" ");
          }else{
              storedName = scoutNamesDescription.getSection();
              table.addCell(scoutNamesDescription.getSection());
          }
          table.addCell(scoutNamesDescription.getAwardName());

          Date awardedDate = df1.parse(scoutNamesDescription.getAwardedDate());
          String awardedString = df2.format(awardedDate);
          table.addCell(awardedString);
      }
      preface.add(table);
      document.add(preface);

0 个答案:

没有答案