com.lowagie.text.pdf

Class PdfCopy

public class PdfCopy extends PdfWriter

Make copies of PDF documents. Documents can be edited after reading and before writing them out.

Author: Mark Thompson

Nested Class Summary
static classPdfCopy.IndirectReferences
This class holds information about indirect references, since they are renumbered by iText.
static classPdfCopy.PageStamp
protected static classPdfCopy.RefKey
A key to allow us to hash indirect references
static classPdfCopy.StampContent
Field Summary
protected PdfIndirectReferenceacroForm
protected intcurrentObjectNum
protected PdfArrayfieldArray
protected HashMapfieldTemplates
protected HashMapindirectMap
protected HashMapindirects
protected int[]namePtr
protected PdfReaderreader
booleanrotateContents
Holds value of property rotateContents.
Constructor Summary
PdfCopy(Document document, OutputStream os)
Constructor
Method Summary
PdfIndirectReferenceadd(PdfOutline outline)
PdfIndirectReferenceadd(PdfPage page, PdfContents contents)
voidaddAnnotation(PdfAnnotation annot)
voidaddFieldResources(PdfDictionary catalog)
voidaddPage(PdfImportedPage iPage)
Add an imported page to our output
voidclose()
Signals that the Document was closed and that no other Elements will be added.
voidcopyAcroForm(PdfReader reader)
Copy the acroform for an input document.
protected PdfArraycopyArray(PdfArray in)
Translate a PRArray to a PdfArray.
protected PdfDictionarycopyDictionary(PdfDictionary in)
Translate a PRDictionary to a PdfDictionary.
protected PdfIndirectReferencecopyIndirect(PRIndirectReference in)
Translate a PRIndirectReference to a PdfIndirectReference In addition, translates the object numbers, and copies the referenced object to the output file.
protected PdfObjectcopyObject(PdfObject in)
Translate a PR-object to a Pdf-object
protected PdfStreamcopyStream(PRStream in)
Translate a PRStream to a PdfStream.
PdfCopy.PageStampcreatePageStamp(PdfImportedPage iPage)
Create a page stamp.
voidfreeReader(PdfReader reader)
protected PdfDictionarygetCatalog(PdfIndirectReference rootObj)
PdfImportedPagegetImportedPage(PdfReader reader, int pageNumber)
Grabs a page from the input document
booleanisRotateContents()
Getter for property rotateContents.
protected intsetFromIPage(PdfImportedPage iPage)
convenience method.
protected voidsetFromReader(PdfReader reader)
convenience method.
voidsetRotateContents(boolean rotateContents)
Setter for property rotateContents.

Field Detail

acroForm

protected PdfIndirectReference acroForm

currentObjectNum

protected int currentObjectNum

fieldArray

protected PdfArray fieldArray

fieldTemplates

protected HashMap fieldTemplates

indirectMap

protected HashMap indirectMap

indirects

protected HashMap indirects

namePtr

protected int[] namePtr

reader

protected PdfReader reader

rotateContents

private boolean rotateContents
Holds value of property rotateContents.

Constructor Detail

PdfCopy

public PdfCopy(Document document, OutputStream os)
Constructor

Parameters: document os outputstream

Method Detail

add

public PdfIndirectReference add(PdfOutline outline)

add

PdfIndirectReference add(PdfPage page, PdfContents contents)

addAnnotation

public void addAnnotation(PdfAnnotation annot)

addFieldResources

private void addFieldResources(PdfDictionary catalog)

addPage

public void addPage(PdfImportedPage iPage)
Add an imported page to our output

Parameters: iPage an imported page

Throws: IOException, BadPdfFormatException

close

public void close()
Signals that the Document was closed and that no other Elements will be added.

The pages-tree is built and written to the outputstream. A Catalog is constructed, as well as an Info-object, the reference table is composed and everything is written to the outputstream embedded in a Trailer.

copyAcroForm

public void copyAcroForm(PdfReader reader)
Copy the acroform for an input document. Note that you can only have one, we make no effort to merge them.

Parameters: reader The reader of the input file that is being copied

Throws: IOException, BadPdfFormatException

copyArray

protected PdfArray copyArray(PdfArray in)
Translate a PRArray to a PdfArray. Also translate all of the objects contained in it

copyDictionary

protected PdfDictionary copyDictionary(PdfDictionary in)
Translate a PRDictionary to a PdfDictionary. Also translate all of the objects contained in it.

copyIndirect

protected PdfIndirectReference copyIndirect(PRIndirectReference in)
Translate a PRIndirectReference to a PdfIndirectReference In addition, translates the object numbers, and copies the referenced object to the output file. NB: PRIndirectReferences (and PRIndirectObjects) really need to know what file they came from, because each file has its own namespace. The translation we do from their namespace to ours is *at best* heuristic, and guaranteed to fail under some circumstances.

copyObject

protected PdfObject copyObject(PdfObject in)
Translate a PR-object to a Pdf-object

copyStream

protected PdfStream copyStream(PRStream in)
Translate a PRStream to a PdfStream. The data part copies itself.

createPageStamp

public PdfCopy.PageStamp createPageStamp(PdfImportedPage iPage)
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.

The general usage to stamp something in a page is:

 PdfImportedPage page = copy.getImportedPage(reader, 1);
 PdfCopy.PageStamp ps = copy.createPageStamp(page);
 ps.addAnnotation(PdfAnnotation.createText(copy, new Rectangle(50, 180, 70, 200), "Hello", "No Thanks", true, "Comment"));
 PdfContentByte under = ps.getUnderContent();
 under.addImage(img);
 PdfContentByte over = ps.getOverContent();
 over.beginText();
 over.setFontAndSize(bf, 18);
 over.setTextMatrix(30, 30);
 over.showText("total page " + totalPage);
 over.endText();
 ps.alterContents();
 copy.addPage(page);
 

Parameters: iPage an imported page

Returns: the PageStamp

freeReader

public void freeReader(PdfReader reader)

getCatalog

protected PdfDictionary getCatalog(PdfIndirectReference rootObj)

getImportedPage

public PdfImportedPage getImportedPage(PdfReader reader, int pageNumber)
Grabs a page from the input document

Parameters: reader the reader of the document pageNumber which page to get

Returns: the page

isRotateContents

public boolean isRotateContents()
Getter for property rotateContents.

Returns: Value of property rotateContents.

setFromIPage

protected int setFromIPage(PdfImportedPage iPage)
convenience method. Given an imported page, set our "globals"

setFromReader

protected void setFromReader(PdfReader reader)
convenience method. Given a reader, set our "globals"

setRotateContents

public void setRotateContents(boolean rotateContents)
Setter for property rotateContents.

Parameters: rotateContents New value of property rotateContents.