Exemplo n.º 1
0
  /**
   * This will overlay two documents onto each other. The overlay document is repeatedly overlayed
   * onto the destination document for every page in the destination.
   *
   * @param overlay The document to copy onto the destination
   * @param destination The file that the overlay should be placed on.
   * @return The destination pdf, same as argument passed in.
   * @throws IOException If there is an error accessing data.
   */
  public PDDocument overlay(PDDocument overlay, PDDocument destination) throws IOException {
    pdfOverlay = overlay;
    pdfDocument = destination;

    PDDocumentCatalog overlayCatalog = pdfOverlay.getDocumentCatalog();
    collectLayoutPages(overlayCatalog.getAllPages());

    COSDictionary saveGraphicsStateDic = new COSDictionary();
    saveGraphicsStateStream =
        new COSStream(saveGraphicsStateDic, pdfDocument.getDocument().getScratchFile());
    OutputStream saveStream = saveGraphicsStateStream.createUnfilteredStream();
    saveStream.write(" q\n".getBytes("ISO-8859-1"));
    saveStream.flush();

    restoreGraphicsStateStream =
        new COSStream(saveGraphicsStateDic, pdfDocument.getDocument().getScratchFile());
    OutputStream restoreStream = restoreGraphicsStateStream.createUnfilteredStream();
    restoreStream.write(" Q\n".getBytes("ISO-8859-1"));
    restoreStream.flush();

    PDDocumentCatalog pdfCatalog = pdfDocument.getDocumentCatalog();
    processPages(pdfCatalog.getAllPages());

    return pdfDocument;
  }