如何为Itext PDF创建子文档

时间:2016-10-11 16:03:05

标签: itext

我的任务是创建一个工作单应用程序 输出为PDF,用于从浏览器打印。输出必须是一个PDF 包含一个或多个“工单”的文档。由于内容长度可变,每个工单可能不止一个打印页面。每个工单必须有自己的“页面”编号。我正在使用Spring MVC和Itext 5.

所以想象我们有三个工单。第一个适合打印一个 页面,第二页需要两个打印页面,第三页需要一个打印页面。我们总共有四个打印页面。

如何制作: 工单1的页码为“1” 工单2的页码为“1,2” 工单3将页码“1”

所以基本上PDF文档将是多个内部的容器 彼此独立的文件。

我尝试重置页码并使用页脚输出,但这似乎不起作用。我想知道是否有某种方法来设置我可以控制的自定义页面计数器。

public class PDFBuilder extends AbstractITextPdfView
{

  @Override
  protected void buildPdfDocument(Map<String, Object> model, Document doc,
      PdfWriter writer, HttpServletRequest request, HttpServletResponse response)
      throws Exception
  {

    @SuppressWarnings("unchecked") // TODO replace this with some generic goodness
    List<WorkOrderDto> workOrders = (List<WorkOrderDto>) model.get("workOrders");
    Integer pageCounter = 1;

    // define table header cell
    PdfPCell cell = new PdfPCell();
    cell.setBackgroundColor(BaseColor.BLUE);
    cell.setPadding(5);

    // define font for table header row
    Font font = FontFactory.getFont(FontFactory.HELVETICA);
    font.setColor(BaseColor.WHITE);
    writer.setPageEvent(new Footer(pageCounter));

    for (WorkOrderDto dto : workOrders)
    {
      PdfPTable woHeader = new PdfPTable(4);
      woHeader.setWidthPercentage(100.0f);
      woHeader.setSpacingBefore(10);

      if (dto.getNumber() != null)
      {
        woHeader.addCell("Number: " + dto.getNumber());
      }
      else
      {
        woHeader.addCell("Number: ");
      }

      if (dto.getOwnerNumber() != null)
      {
        woHeader.addCell("Owner: " + dto.getOwnerNumber());
      }
      else
      {
        woHeader.addCell("Owner: ");
      }

      if (dto.getTypeNumber() != null)
      {
        woHeader.addCell("Type: " + dto.getTypeNumber());
      }
      else
      {
        woHeader.addCell("Type: ");
      }

      if (dto.getScheduleDate() != null)
      {
        woHeader.addCell("Open Date: " + dto.getScheduleDate().toString());
      }
      else
      {
        woHeader.addCell("Open Date: ");
      }
      doc.add(woHeader);

      PdfPTable servicesTable = new PdfPTable(4);
      for (ServiceDto serviceDto : dto.getCurrentServices())
      {
        servicesTable.addCell("Number: " + serviceDto.getNumber());
        servicesTable.addCell("Name: " + serviceDto.getName());
        servicesTable.addCell("Description: " + serviceDto.getDescription());
        servicesTable.addCell("Status: " + serviceDto.getStatus());
      }
      doc.add(servicesTable);

      doc.newPage();
      pageCounter++;
    }

  }
}




public class Footer implements PdfPageEvent
{
  Font    ffont = new Font(Font.FontFamily.UNDEFINED, 10, Font.ITALIC);
  Integer pageCounter;

  public Footer(Integer pageCounter)
  {
    this.pageCounter = pageCounter;
  }

  @Override
  public void onOpenDocument(PdfWriter writer, Document document)
  {
  }

  @Override
  public void onStartPage(PdfWriter writer, Document document)
  {
  }

  @Override
  public void onEndPage(PdfWriter writer, Document document)
  {
    PdfContentByte cb = writer.getDirectContent();
    Phrase footer = new Phrase(pageCounter + "", ffont);

    ColumnText.showTextAligned(cb, Element.ALIGN_CENTER,
        footer,
        (document.right() - document.left()) / 2 + document.leftMargin(),
        document.bottom() - 10, 0);
  }

  @Override
  public void onCloseDocument(PdfWriter writer, Document document)
  {
  }

  @Override
  public void onParagraph(PdfWriter writer, Document document, float paragraphPosition)
  {
  }

  @Override
  public void onParagraphEnd(PdfWriter writer, Document document, float paragraphPosition)
  {
  }

  @Override
  public void onChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title)
  {
  }

  @Override
  public void onChapterEnd(PdfWriter writer, Document document, float paragraphPosition)
  {
  }

  @Override
  public void onSection(PdfWriter writer, Document document, float paragraphPosition, int depth, Paragraph title)
  {
  }

  @Override
  public void onSectionEnd(PdfWriter writer, Document document, float paragraphPosition)
  {
  }

  @Override
  public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text)
  {
  }

}

1 个答案:

答案 0 :(得分:1)

我提出了自己的解决方案,结果非常简单。我创建了一个名为PageCounter的类,其中包含一些属性来表示各种计数器。我将其传递给页脚,然后在构建页面时操纵属性。

public class PageCounter
{
  private Integer currentPage = 1;
  private Integer totalPages  = 1;

  public Integer getCurrentPage()
  {
    return currentPage;
  }

  public void setCurrentPage(Integer currentPage)
  {
    this.currentPage = currentPage;
  }

  public Integer getTotalPages()
  {
    return totalPages;
  }

  public void setTotalPages(Integer totalPages)
  {
    this.totalPages = totalPages;
  }

  public void increment()
  {
    this.currentPage++;
  }

  public void reset()
  {
    this.currentPage = 1;
  }

}


public class PDFBuilder extends AbstractITextPdfView
{

  @Override
  protected void buildPdfDocument(Map<String, Object> model, Document doc,
      PdfWriter writer, HttpServletRequest request, HttpServletResponse response)
      throws Exception
  {

    @SuppressWarnings("unchecked") // TODO replace this with some generic goodness
    List<WorkOrderDto> workOrders = (List<WorkOrderDto>) model.get("workOrders");
    PageCounter pageCounter = new PageCounter();

    // define table header cell
    PdfPCell cell = new PdfPCell();
    cell.setBackgroundColor(BaseColor.BLUE);
    cell.setPadding(5);

    // define font for table header row
    Font font = FontFactory.getFont(FontFactory.HELVETICA);
    font.setColor(BaseColor.WHITE);
    writer.setPageEvent(new Footer(pageCounter));

    for (WorkOrderDto dto : workOrders)
    {
      PdfPTable woHeader = new PdfPTable(4);
      woHeader.setWidthPercentage(100.0f);
      woHeader.setSpacingBefore(10);

      if (dto.getNumber() != null)
      {
        woHeader.addCell("Number: " + dto.getNumber());
      }
      else
      {
        woHeader.addCell("Number: ");
      }

      if (dto.getOwnerNumber() != null)
      {
        woHeader.addCell("Owner: " + dto.getOwnerNumber());
      }
      else
      {
        woHeader.addCell("Owner: ");
      }

      if (dto.getTypeNumber() != null)
      {
        woHeader.addCell("Type: " + dto.getTypeNumber());
      }
      else
      {
        woHeader.addCell("Type: ");
      }

      if (dto.getScheduleDate() != null)
      {
        woHeader.addCell("Open Date: " + dto.getScheduleDate().toString());
      }
      else
      {
        woHeader.addCell("Open Date: ");
      }
      doc.add(woHeader);

      PdfPTable servicesTable = new PdfPTable(4);
      for (ServiceDto serviceDto : dto.getCurrentServices())
      {
        servicesTable.addCell("Number: " + serviceDto.getNumber());
        servicesTable.addCell("Name: " + serviceDto.getName());
        servicesTable.addCell("Description: " + serviceDto.getDescription());
        servicesTable.addCell("Status: " + serviceDto.getStatus());
      }
      doc.add(servicesTable);

      doc.newPage();
      pageCounter.reset();
    }



public class Footer implements PdfPageEvent
{
  Font        ffont = new Font(Font.FontFamily.UNDEFINED, 10, Font.ITALIC);
  PageCounter pageCounter;

  public Footer(PageCounter pageCounter)
  {
    this.pageCounter = pageCounter;
  }

  @Override
  public void onOpenDocument(PdfWriter writer, Document document)
  {
  }

  @Override
  public void onStartPage(PdfWriter writer, Document document)
  {
  }

  @Override
  public void onEndPage(PdfWriter writer, Document document)
  {
    PdfContentByte cb = writer.getDirectContent();
    Phrase footer = new Phrase(pageCounter.getCurrentPage() + "", ffont);
    pageCounter.increment();

    ColumnText.showTextAligned(cb, Element.ALIGN_CENTER,
        footer,
        (document.right() - document.left()) / 2 + document.leftMargin(),
        document.bottom() - 10, 0);
  }

  @Override
  public void onCloseDocument(PdfWriter writer, Document document)
  {
  }

  @Override
  public void onParagraph(PdfWriter writer, Document document, float paragraphPosition)
  {
  }

  @Override
  public void onParagraphEnd(PdfWriter writer, Document document, float paragraphPosition)
  {
  }

  @Override
  public void onChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title)
  {
  }

  @Override
  public void onChapterEnd(PdfWriter writer, Document document, float paragraphPosition)
  {
  }

  @Override
  public void onSection(PdfWriter writer, Document document, float paragraphPosition, int depth, Paragraph title)
  {
  }

  @Override
  public void onSectionEnd(PdfWriter writer, Document document, float paragraphPosition)
  {
  }

  @Override
  public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text)
  {
  }

}