com.lowagie.text.pdf
Class PdfCopy

java.lang.Object
  extended by com.lowagie.text.DocWriter
      extended by com.lowagie.text.pdf.PdfWriter
          extended by com.lowagie.text.pdf.PdfCopy
All Implemented Interfaces:
DocListener, ElementListener, EventListener

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
(package private) static class PdfCopy.IndirectReferences
          This class holds information about indirect references, since they are renumbered by iText.
protected static class PdfCopy.RefKey
          A key to allow us to hash indirect references
 
Nested classes/interfaces inherited from class com.lowagie.text.pdf.PdfWriter
PdfWriter.PdfBody, PdfWriter.PdfTrailer
 
Field Summary
protected  PdfIndirectReference acroForm
           
protected  int currentObjectNum
           
protected  HashMap indirectMap
           
protected  HashMap indirects
           
protected  List newBookmarks
           
protected  ArrayList pageNumbersToRefs
           
protected  PdfReader reader
           
protected  PdfIndirectReference topPageParent
           
 
Fields inherited from class com.lowagie.text.pdf.PdfWriter
AllowAssembly, AllowCopy, AllowDegradedPrinting, AllowFillIn, AllowModifyAnnotations, AllowModifyContents, AllowPrinting, AllowScreenReaders, body, CenterWindow, colorNumber, crypto, currentPageNumber, currentPdfReaderInstance, defaultColorspace, DID_PRINT, DID_SAVE, directContent, directContentUnder, DirectionL2R, DirectionR2L, DisplayDocTitle, DOCUMENT_CLOSE, documentColors, documentExtGState, documentFonts, documentOCG, documentOCGorder, documentPatterns, documentProperties, documentShadingPatterns, documentShadings, documentSpotPatterns, FitWindow, fontNumber, formXObjects, formXObjectsCounter, fullCompression, group, HEADER, HideMenubar, HideToolbar, HideWindowUI, imageDictionary, importedPages, NO_SPACE_CHAR_RATIO, NonFullScreenPageModeUseNone, NonFullScreenPageModeUseOC, NonFullScreenPageModeUseOutlines, NonFullScreenPageModeUseThumbs, OCGRadioGroup, OCProperties, PAGE_CLOSE, PAGE_OPEN, PageLayoutOneColumn, PageLayoutSinglePage, PageLayoutTwoColumnLeft, PageLayoutTwoColumnRight, PageLayoutTwoPageLeft, PageLayoutTwoPageRight, PageModeFullScreen, PageModeUseAttachments, PageModeUseNone, PageModeUseOC, PageModeUseOutlines, PageModeUseThumbs, pageReferences, patternColorspaceCMYK, patternColorspaceGRAY, patternColorspaceRGB, patternNumber, pdf, PDFX1A2001, PDFX32002, PDFXKEY_CMYK, PDFXKEY_COLOR, PDFXKEY_FONT, PDFXKEY_GSTATE, PDFXKEY_IMAGE, PDFXKEY_LAYER, PDFXKEY_RGB, PDFXNONE, prevxref, PrintScalingNone, root, RUN_DIRECTION_DEFAULT, RUN_DIRECTION_LTR, RUN_DIRECTION_NO_BIDI, RUN_DIRECTION_RTL, runDirection, SIGNATURE_APPEND_ONLY, SIGNATURE_EXISTS, SPACE_CHAR_RATIO_DEFAULT, STRENGTH128BITS, STRENGTH40BITS, structureTreeRoot, tagged, userunit, VERSION_1_2, VERSION_1_3, VERSION_1_4, VERSION_1_5, VERSION_1_6, ViewerPreferencesMask, WILL_PRINT, WILL_SAVE, xmpMetadata
 
Fields inherited from class com.lowagie.text.DocWriter
closeStream, document, EQUALS, FORWARD, GT, LT, NEWLINE, open, os, pageSize, pause, QUOTE, SPACE, TAB
 
Constructor Summary
PdfCopy(Document document, OutputStream os)
          Constructor
 
Method Summary
(package private)  PdfIndirectReference add(PdfImage pdfImage)
          Writes a PdfImage to the outputstream.
 PdfIndirectReference add(PdfOutline outline)
           
(package private)  PdfIndirectReference add(PdfPage page, PdfContents contents)
          Adds some PdfContents to this Writer.
 void addAnnotation(PdfAnnotation annot)
          Adds a PdfAnnotation or a PdfFormField to the document.
 void addPage(PdfImportedPage iPage)
          Add an imported page to our output
 void close()
          Signals that the Document was closed and that no other Elements will be added.
 void copyAcroForm(PdfReader reader)
          Copy the acroform for an input document.
protected  PdfArray copyArray(PdfArray in)
          Translate a PRArray to a PdfArray.
protected  PdfDictionary copyDictionary(PdfDictionary in)
          Translate a PRDictionary to a PdfDictionary.
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.
protected  PdfObject copyObject(PdfObject in)
          Translate a PR-object to a Pdf-object
protected  PdfStream copyStream(PRStream in)
          Translate a PRStream to a PdfStream.
protected  PdfDictionary getCatalog(PdfIndirectReference rootObj)
           
 PdfImportedPage getImportedPage(PdfReader reader, int pageNumber)
          Grabs a page from the input document
 PdfIndirectReference getPageReference(int page)
          Gets a reference to a page existing or not.
 void open()
          Signals that the Document has been opened and that Elements can be added.
protected  int setFromIPage(PdfImportedPage iPage)
          convenience method.
protected  void setFromReader(PdfReader reader)
          convenience method.
 void setOutlines(List outlines)
          Sets the bookmarks.
 
Methods inherited from class com.lowagie.text.pdf.PdfWriter
add, addAnnotation, addCalculationOrder, addDirectImageSimple, addDirectTemplateSimple, addJavaScript, addJavaScript, addJavaScript, addLocalDestinations, addOCGRadioGroup, addSharedObjectsToBody, addSimple, addSimple, addSimpleExtGState, addSimplePattern, addSimplePatternColorspace, addSimpleProperty, addSimpleShading, addSimpleShadingPattern, addToBody, addToBody, addToBody, addToBody, addToBody, addToBody, breakTableIfDoesntFit, checkPDFXConformance, createXmpMetadata, eliminateFontSubset, fitsPage, fitsPage, fitsPage, fitsPage, freeReader, getAcroForm, getBoxSize, getColorspaceName, getCurrentDocumentSize, getCurrentPage, getCurrentPageNumber, getDefaultColorspace, getDirectContent, getDirectContentUnder, getEncryption, getExtraCatalog, getGroup, getImageReference, getIndirectReferenceNumber, getInfo, getInstance, getInstance, getNewObjectNumber, getOCProperties, getOs, getPageEvent, getPageNumber, getPageSize, getPdfDocument, getPdfIndirectReference, getPdfTable, getPDFXConformance, getReaderFile, getRootOutline, getRunDirection, getSpaceCharRatio, getStructureTreeRoot, getTableBottom, getUserunit, getVerticalPosition, isFullCompression, isPaused, isStrictImageSequence, isTagged, propertyExists, registerLayer, releaseTemplate, reorderPages, resetContent, setAdditionalAction, setBoxSize, setCropBoxSize, setDefaultColorspace, setDuration, setEncryption, setEncryption, setFullCompression, setGroup, setLinearPageMode, setOpenAction, setOpenAction, setOutputIntents, setOutputIntents, setPageAction, setPageEmpty, setPageEvent, setPageLabels, setPdfVersion, setPDFXConformance, setRunDirection, setSigFlags, setSpaceCharRatio, setStrictImageSequence, setTagged, setThumbnail, setTransition, setUserunit, setViewerPreferences, setXmpMetadata
 
Methods inherited from class com.lowagie.text.DocWriter
add, add, addTabs, clearTextWrap, flush, getISOBytes, hasMarkupAttributes, isCloseStream, newPage, pause, removeWatermark, resetFooter, resetHeader, resetPageCount, resume, setCloseStream, setFooter, setHeader, setMarginMirroring, setMargins, setPageCount, setPageSize, write, write, writeEnd, writeEnd, writeMarkupAttributes, writeStart
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

indirects

protected HashMap indirects

indirectMap

protected HashMap indirectMap

currentObjectNum

protected int currentObjectNum

reader

protected PdfReader reader

acroForm

protected PdfIndirectReference acroForm

topPageParent

protected PdfIndirectReference topPageParent

pageNumbersToRefs

protected ArrayList pageNumbersToRefs

newBookmarks

protected List newBookmarks
Constructor Detail

PdfCopy

public PdfCopy(Document document,
               OutputStream os)
        throws DocumentException
Constructor

Parameters:
document -
os - outputstream
Throws:
DocumentException
Method Detail

open

public void open()
Description copied from class: PdfWriter
Signals that the Document has been opened and that Elements can be added.

When this method is called, the PDF-document header is written to the outputstream.

Specified by:
open in interface DocListener
Overrides:
open in class PdfWriter

getImportedPage

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

Overrides:
getImportedPage in class PdfWriter
Parameters:
reader - the reader of the document
pageNumber - which page to get
Returns:
the page

copyIndirect

protected PdfIndirectReference copyIndirect(PRIndirectReference in)
                                     throws IOException,
                                            BadPdfFormatException
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.

Throws:
IOException
BadPdfFormatException

copyDictionary

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

Throws:
IOException
BadPdfFormatException

copyStream

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

Throws:
IOException
BadPdfFormatException

copyArray

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

Throws:
IOException
BadPdfFormatException

copyObject

protected PdfObject copyObject(PdfObject in)
                        throws IOException,
                               BadPdfFormatException
Translate a PR-object to a Pdf-object

Throws:
IOException
BadPdfFormatException

setFromIPage

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


setFromReader

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


addPage

public void addPage(PdfImportedPage iPage)
             throws IOException,
                    BadPdfFormatException
Add an imported page to our output

Parameters:
iPage - an imported page
Throws:
IOException, - BadPdfFormatException
IOException
BadPdfFormatException

getPageReference

public PdfIndirectReference getPageReference(int page)
Description copied from class: PdfWriter
Gets a reference to a page existing or not. If the page does not exist yet the reference will be created in advance. If on closing the document, a page number greater than the total number of pages was requested, an exception is thrown.

Overrides:
getPageReference in class PdfWriter
Parameters:
page - the page number. The first page is 1
Returns:
the reference to the page

copyAcroForm

public void copyAcroForm(PdfReader reader)
                  throws IOException,
                         BadPdfFormatException
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
IOException
BadPdfFormatException

getCatalog

protected PdfDictionary getCatalog(PdfIndirectReference rootObj)
Overrides:
getCatalog in class PdfWriter

setOutlines

public void setOutlines(List outlines)
Sets the bookmarks. The list structure is defined in SimpleBookmark#.

Parameters:
outlines - the bookmarks or null to remove any

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 referencetable is composed and everything is written to the outputstream embedded in a Trailer.

Specified by:
close in interface DocListener
Overrides:
close in class PdfWriter

add

PdfIndirectReference add(PdfImage pdfImage)
                   throws PdfException
Description copied from class: PdfWriter
Writes a PdfImage to the outputstream.

Overrides:
add in class PdfWriter
Parameters:
pdfImage - the image to be added
Returns:
a PdfIndirectReference to the encapsulated image
Throws:
PdfException - when a document isn't open yet, or has been closed

add

public PdfIndirectReference add(PdfOutline outline)

addAnnotation

public void addAnnotation(PdfAnnotation annot)
Description copied from class: PdfWriter
Adds a PdfAnnotation or a PdfFormField to the document. Only the top parent of a PdfFormField needs to be added.

Overrides:
addAnnotation in class PdfWriter
Parameters:
annot - the PdfAnnotation or the PdfFormField to add

add

PdfIndirectReference add(PdfPage page,
                         PdfContents contents)
                   throws PdfException
Description copied from class: PdfWriter
Adds some PdfContents to this Writer.

The document has to be open before you can begin to add content to the body of the document.

Overrides:
add in class PdfWriter
Parameters:
page - the PdfPage to add
contents - the PdfContents of the page
Returns:
a PdfIndirectReference
Throws:
PdfException - on error