Example #1
0
 /** convenience method. Given an imported page, set our "globals" */
 protected int setFromIPage(PdfImportedPage iPage) {
   int pageNum = iPage.getPageNumber();
   PdfReaderInstance inst = currentPdfReaderInstance = iPage.getPdfReaderInstance();
   reader = inst.getReader();
   setFromReader(reader);
   return pageNum;
 }
 /**
  * Gets the content stream of a page as a PdfStream object.
  *
  * @param pageNumber the page of which you want the stream
  * @param compressionLevel the compression level you want to apply to the stream
  * @return a PdfStream object
  * @since 2.1.3 (the method already existed without param compressionLevel)
  */
 PdfStream getFormXObject(int pageNumber, int compressionLevel) throws IOException {
   PdfDictionary page = reader.getPageNRelease(pageNumber);
   PdfObject contents = PdfReader.getPdfObjectRelease(page.get(PdfName.CONTENTS));
   PdfDictionary dic = new PdfDictionary();
   byte bout[] = null;
   if (contents != null) {
     if (contents.isStream()) dic.putAll((PRStream) contents);
     else bout = reader.getPageContent(pageNumber, file);
   } else bout = new byte[0];
   dic.put(PdfName.RESOURCES, PdfReader.getPdfObjectRelease(page.get(PdfName.RESOURCES)));
   dic.put(PdfName.TYPE, PdfName.XOBJECT);
   dic.put(PdfName.SUBTYPE, PdfName.FORM);
   PdfImportedPage impPage = (PdfImportedPage) importedPages.get(new Integer(pageNumber));
   dic.put(PdfName.BBOX, new PdfRectangle(impPage.getBoundingBox()));
   PdfArray matrix = impPage.getMatrix();
   if (matrix == null) dic.put(PdfName.MATRIX, IDENTITYMATRIX);
   else dic.put(PdfName.MATRIX, matrix);
   dic.put(PdfName.FORMTYPE, ONE);
   PRStream stream;
   if (bout == null) {
     stream = new PRStream((PRStream) contents, dic);
   } else {
     stream = new PRStream(reader, bout, compressionLevel);
     stream.putAll(dic);
   }
   return stream;
 }
 void writeAllPages() throws IOException {
   try {
     file.reOpen();
     for (Iterator it = importedPages.values().iterator(); it.hasNext(); ) {
       PdfImportedPage ip = (PdfImportedPage) it.next();
       writer.addToBody(
           ip.getFormXObject(writer.getCompressionLevel()), ip.getIndirectReference());
     }
     writeAllVisited();
   } finally {
     try {
       reader.close();
       file.close();
     } catch (Exception e) {
       // Empty on purpose
     }
   }
 }
Example #4
0
  /**
   * Add an imported page to our output
   *
   * @param iPage an imported page
   * @throws IOException, BadPdfFormatException
   */
  public void addPage(PdfImportedPage iPage) throws IOException, BadPdfFormatException {
    int pageNum = setFromIPage(iPage);

    PdfDictionary thePage = reader.getPageN(pageNum);
    PRIndirectReference origRef = reader.getPageOrigRef(pageNum);
    reader.releasePage(pageNum);
    RefKey key = new RefKey(origRef);
    PdfIndirectReference pageRef;
    IndirectReferences iRef = indirects.get(key);
    if (iRef != null && !iRef.getCopied()) {
      pageReferences.add(iRef.getRef());
      iRef.setCopied();
    }
    pageRef = getCurrentPage();
    if (iRef == null) {
      iRef = new IndirectReferences(pageRef);
      indirects.put(key, iRef);
    }
    iRef.setCopied();
    PdfDictionary newPage = copyDictionary(thePage);
    root.addPage(newPage);
    iPage.setCopied();
    ++currentPageNumber;
  }
Example #5
0
 /**
  * Create a page stamp. New content and annotations, including new fields, are allowed. The fields
  * added cannot have parents in another pages. This method modifies the PdfReader instance.
  *
  * <p>The general usage to stamp something in a page is:
  *
  * <p>
  *
  * <pre>
  * PdfImportedPage page = copy.getImportedPage(reader, 1);
  * PdfCopy.PageStamp ps = copy.createPageStamp(page);
  * ps.addAnnotation(PdfAnnotation.createText(copy, new Rectangle(50, 180, 70, 200), &quot;Hello&quot;, &quot;No Thanks&quot;, true, &quot;Comment&quot;));
  * PdfContentByte under = ps.getUnderContent();
  * under.addImage(img);
  * PdfContentByte over = ps.getOverContent();
  * over.beginText();
  * over.setFontAndSize(bf, 18);
  * over.setTextMatrix(30, 30);
  * over.showText(&quot;total page &quot; + totalPage);
  * over.endText();
  * ps.alterContents();
  * copy.addPage(page);
  * </pre>
  *
  * @param iPage an imported page
  * @return the <CODE>PageStamp</CODE>
  */
 public PageStamp createPageStamp(PdfImportedPage iPage) {
   int pageNum = iPage.getPageNumber();
   PdfReader reader = iPage.getPdfReaderInstance().getReader();
   PdfDictionary pageN = reader.getPageN(pageNum);
   return new PageStamp(reader, pageN, this);
 }