com.lowagie.text.pdf
Class PdfStamper

java.lang.Object
  |
  +--com.lowagie.text.pdf.PdfStamper

public class PdfStamper
extends Object

Applies extra content to the pages of a PDF document. This extra content can be all the objects allowed in PdfContentByte including pages from other Pdfs. The original PDF will keep all the interactive elements including bookmarks, links and form fields.

It is also possible to change the field values and to flatten them.

Author:
Paulo Soares (psoares@consiste.pt)

Field Summary
private  HashMap moreInfo
           
protected  PdfStamperImp stamper
           
 
Constructor Summary
PdfStamper(PdfReader reader, OutputStream os)
          Starts the process of adding extra content to an existing PDF document.
PdfStamper(PdfReader reader, OutputStream os, char pdfVersion)
          Starts the process of adding extra content to an existing PDF document.
 
Method Summary
 void close()
          Closes the document.
 AcroFields getAcroFields()
          Gets the AcroFields object that allows to get and set field values and to merge FDF forms.
 PdfImportedPage getImportedPage(PdfReader reader, int pageNumber)
          Gets a page from other PDF document.
 HashMap getMoreInfo()
          Gets the optional String map to add or change values in the info dictionary.
 PdfContentByte getOverContent(int pageNum)
          Gets a PdfContentByte to write over the page of the original document.
 PdfContentByte getUnderContent(int pageNum)
          Gets a PdfContentByte to write under the page of the original document.
 PdfWriter getWriter()
          Gets the underlying PdfWriter.
 boolean isRotateContents()
          Checks if the content is automatically adjusted to compensate the original page rotation.
 void setEncryption(boolean strength, String userPassword, String ownerPassword, int permissions)
          Sets the encryption options for this document.
 void setEncryption(byte[] userPassword, byte[] ownerPassword, int permissions, boolean strength128Bits)
          Sets the encryption options for this document.
 void setFormFlattening(boolean flat)
          Determines if the fields are flattened on close.
 void setMoreInfo(HashMap moreInfo)
          An optional String map to add or change values in the info dictionary.
 void setRotateContents(boolean rotateContents)
          Flags the content to be automatically adjusted to compensate the original page rotation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

stamper

protected PdfStamperImp stamper

moreInfo

private HashMap moreInfo
Constructor Detail

PdfStamper

public PdfStamper(PdfReader reader,
                  OutputStream os)
           throws DocumentException,
                  IOException
Starts the process of adding extra content to an existing PDF document.

Parameters:
reader - the original document. It cannot be reused
os - the output stream
Throws:
DocumentException - on error
IOException - on error

PdfStamper

public PdfStamper(PdfReader reader,
                  OutputStream os,
                  char pdfVersion)
           throws DocumentException,
                  IOException
Starts the process of adding extra content to an existing PDF document.

Parameters:
reader - the original document. It cannot be reused
os - the output stream
pdfVersion - the new pdf version or '\0' to keep the same version as the original document
Throws:
DocumentException - on error
IOException - on error
Method Detail

getMoreInfo

public HashMap getMoreInfo()
Gets the optional String map to add or change values in the info dictionary.

Returns:
the map or CODE>null

setMoreInfo

public void setMoreInfo(HashMap moreInfo)
An optional String map to add or change values in the info dictionary. Entries with null values delete the key in the original info dictionary

Parameters:
moreInfo - additional entries to the info dictionary

close

public void close()
           throws DocumentException,
                  IOException
Closes the document. No more content can be written after the document is closed.

Throws:
DocumentException - on error
IOException - on error

getUnderContent

public PdfContentByte getUnderContent(int pageNum)
Gets a PdfContentByte to write under the page of the original document.

Parameters:
pageNum - the page number where the extra content is written
Returns:
a PdfContentByte to write under the page of the original document

getOverContent

public PdfContentByte getOverContent(int pageNum)
Gets a PdfContentByte to write over the page of the original document.

Parameters:
pageNum - the page number where the extra content is written
Returns:
a PdfContentByte to write over the page of the original document

isRotateContents

public boolean isRotateContents()
Checks if the content is automatically adjusted to compensate the original page rotation.

Returns:
the auto-rotation status

setRotateContents

public void setRotateContents(boolean rotateContents)
Flags the content to be automatically adjusted to compensate the original page rotation. The default is true.

Parameters:
rotateContents - true to set auto-rotation, false otherwise

setEncryption

public void setEncryption(byte[] userPassword,
                          byte[] ownerPassword,
                          int permissions,
                          boolean strength128Bits)
                   throws DocumentException
Sets the encryption options for this document. The userPassword and the ownerPassword can be null or have zero length. In this case the ownerPassword is replaced by a random string. The open permissions for the document can be AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations, AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting. The permissions can be combined by ORing them.

Parameters:
userPassword - the user password. Can be null or empty
ownerPassword - the owner password. Can be null or empty
permissions - the user permissions
strength128Bits - true for 128 bit key length. false for 40 bit key length
Throws:
DocumentException - if anything was already written to the output

setEncryption

public void setEncryption(boolean strength,
                          String userPassword,
                          String ownerPassword,
                          int permissions)
                   throws DocumentException
Sets the encryption options for this document. The userPassword and the ownerPassword can be null or have zero length. In this case the ownerPassword is replaced by a random string. The open permissions for the document can be AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations, AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting. The permissions can be combined by ORing them.

Parameters:
strength - true for 128 bit key length. false for 40 bit key length
userPassword - the user password. Can be null or empty
ownerPassword - the owner password. Can be null or empty
permissions - the user permissions
Throws:
DocumentException - if anything was already written to the output

getImportedPage

public PdfImportedPage getImportedPage(PdfReader reader,
                                       int pageNumber)
Gets a page from other PDF document. Note that calling this method more than once with the same parameters will retrieve the same object.

Parameters:
reader - the PDF document where the page is
pageNumber - the page number. The first page is 1
Returns:
the template representing the imported page

getWriter

public PdfWriter getWriter()
Gets the underlying PdfWriter.

Returns:
the underlying PdfWriter

getAcroFields

public AcroFields getAcroFields()
Gets the AcroFields object that allows to get and set field values and to merge FDF forms.

Returns:
the AcroFields object

setFormFlattening

public void setFormFlattening(boolean flat)
Determines if the fields are flattened on close.

Parameters:
flat - true to flatten the fields, false to keep the fields