Exemple #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;
  }
Exemple #2
0
  private COSStream makeUniqObjectNames(Map objectNameMap, COSStream stream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(10240);

    byte[] buf = new byte[10240];
    int read;
    InputStream is = stream.getUnfilteredStream();
    while ((read = is.read(buf)) > -1) {
      baos.write(buf, 0, read);
    }

    buf = baos.toByteArray();
    baos = new ByteArrayOutputStream(buf.length + 100);
    StringBuffer sbObjectName = new StringBuffer(10);
    boolean bInObjectIdent = false;
    boolean bInText = false;
    boolean bInEscape = false;
    for (int i = 0; i < buf.length; i++) {
      byte b = buf[i];

      if (!bInEscape) {
        if (!bInText && b == '(') {
          bInText = true;
        }
        if (bInText && b == ')') {
          bInText = false;
        }
        if (b == '\\') {
          bInEscape = true;
        }

        if (!bInText && !bInEscape) {
          if (b == '/') {
            bInObjectIdent = true;
          } else if (bInObjectIdent && Character.isWhitespace((char) b)) {
            bInObjectIdent = false;

            // System.err.println(sbObjectName);
            // String object = sbObjectName.toString();

            String objectName = sbObjectName.toString().substring(1);
            String newObjectName = objectName + "overlay";
            baos.write('/');
            baos.write(newObjectName.getBytes("ISO-8859-1"));

            objectNameMap.put(objectName, COSName.getPDFName(newObjectName));

            sbObjectName.delete(0, sbObjectName.length());
          }
        }

        if (bInObjectIdent) {
          sbObjectName.append((char) b);
          continue;
        }
      } else {
        bInEscape = false;
      }

      baos.write(b);
    }

    COSDictionary streamDict = new COSDictionary();
    streamDict.setInt(COSName.LENGTH, baos.size());
    COSStream output = new COSStream(streamDict, pdfDocument.getDocument().getScratchFile());
    output.setFilters(stream.getFilters());
    OutputStream os = output.createUnfilteredStream();
    baos.writeTo(os);
    os.close();

    return output;
  }