Package com.itextpdf.text.pdf
Class PdfCopy
- java.lang.Object
-
- com.itextpdf.text.DocWriter
-
- com.itextpdf.text.pdf.PdfWriter
-
- com.itextpdf.text.pdf.PdfCopy
-
- All Implemented Interfaces:
DocListener
,ElementListener
,PdfAnnotations
,PdfDocumentActions
,PdfEncryptionSettings
,PdfPageActions
,PdfRunDirection
,PdfVersion
,PdfViewerPreferences
,EventListener
- Direct Known Subclasses:
PdfACopy
,PdfSmartCopy
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
Nested Classes Modifier and Type Class Description protected static class
PdfCopy.ImportedPage
static class
PdfCopy.PageStamp
static class
PdfCopy.StampContent
-
Nested classes/interfaces inherited from class com.itextpdf.text.pdf.PdfWriter
PdfWriter.PdfBody, PdfWriter.PdfTrailer
-
-
Field Summary
Fields Modifier and Type Field Description protected static Counter
COUNTER
protected HashSet<PdfObject>
disableIndirects
protected PdfArray
fieldArray
protected static HashSet<PdfName>
fieldKeys
protected ArrayList<AcroFields>
fields
protected HashSet<PdfTemplate>
fieldTemplates
protected ArrayList<PdfCopy.ImportedPage>
importedPages
protected HashMap<PdfReader,HashMap<RefKey,com.itextpdf.text.pdf.PdfCopy.IndirectReferences>>
indirectMap
protected LinkedHashMap<RefKey,PdfIndirectObject>
indirectObjects
protected HashMap<RefKey,com.itextpdf.text.pdf.PdfCopy.IndirectReferences>
indirects
protected boolean
mergeFields
protected int[]
namePtr
protected HashMap<PdfObject,PdfObject>
parentObjects
protected PdfReader
reader
protected ArrayList<PdfIndirectObject>
savedObjects
protected PRIndirectReference
structTreeRootReference
protected boolean
updateRootKids
protected static HashSet<PdfName>
widgetKeys
-
Fields inherited from class com.itextpdf.text.pdf.PdfWriter
ALLOW_ASSEMBLY, ALLOW_COPY, ALLOW_DEGRADED_PRINTING, ALLOW_FILL_IN, ALLOW_MODIFY_ANNOTATIONS, ALLOW_MODIFY_CONTENTS, ALLOW_PRINTING, ALLOW_SCREENREADERS, AllowAssembly, AllowCopy, AllowDegradedPrinting, AllowFillIn, AllowModifyAnnotations, AllowModifyContents, AllowPrinting, AllowScreenReaders, body, CenterWindow, colorNumber, colorProfile, compressionLevel, crypto, currentPageNumber, currentPdfReaderInstance, defaultColorspace, DID_PRINT, DID_SAVE, directContent, directContentUnder, DirectionL2R, DirectionR2L, DisplayDocTitle, DO_NOT_ENCRYPT_METADATA, DOCUMENT_CLOSE, documentColors, documentExtGState, documentFonts, documentOCG, documentOCGorder, documentPatterns, documentProperties, documentShadingPatterns, documentShadings, documentSpotPatterns, EMBEDDED_FILES_ONLY, ENCRYPTION_AES_128, ENCRYPTION_AES_256, extraCatalog, FitWindow, fontNumber, formXObjects, formXObjectsCounter, fullCompression, GENERATION_MAX, group, HideMenubar, HideToolbar, HideWindowUI, imageDictionary, JBIG2Globals, markAll, markInlineElementsOnly, newBookmarks, NO_SPACE_CHAR_RATIO, NonFullScreenPageModeUseNone, NonFullScreenPageModeUseOC, NonFullScreenPageModeUseOutlines, NonFullScreenPageModeUseThumbs, OCGLocked, OCGRadioGroup, OCProperties, originalFileID, PAGE_CLOSE, PAGE_OPEN, pageDictEntries, PageLayoutOneColumn, PageLayoutSinglePage, PageLayoutTwoColumnLeft, PageLayoutTwoColumnRight, PageLayoutTwoPageLeft, PageLayoutTwoPageRight, PageModeFullScreen, PageModeUseAttachments, PageModeUseNone, PageModeUseOC, PageModeUseOutlines, PageModeUseThumbs, pageReferences, patternColorspaceCMYK, patternColorspaceGRAY, patternColorspaceRGB, patternNumber, pdf, pdf_version, PDF_VERSION_1_2, PDF_VERSION_1_3, PDF_VERSION_1_4, PDF_VERSION_1_5, PDF_VERSION_1_6, PDF_VERSION_1_7, pdfIsoConformance, PDFX1A2001, PDFX32002, PDFXNONE, prevxref, PrintScalingNone, readerInstances, root, RUN_DIRECTION_DEFAULT, RUN_DIRECTION_LTR, RUN_DIRECTION_NO_BIDI, RUN_DIRECTION_RTL, runDirection, SIGNATURE_APPEND_ONLY, SIGNATURE_EXISTS, SPACE_CHAR_RATIO_DEFAULT, STANDARD_ENCRYPTION_128, STANDARD_ENCRYPTION_40, STRENGTH128BITS, STRENGTH40BITS, structureTreeRoot, tabs, tagged, taggingMode, ttfUnicodeWriter, VERSION_1_2, VERSION_1_3, VERSION_1_4, VERSION_1_5, VERSION_1_6, VERSION_1_7, WILL_PRINT, WILL_SAVE, xmpMetadata, xmpWriter
-
-
Constructor Summary
Constructors Constructor Description PdfCopy(Document document, OutputStream os)
Constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description PdfIndirectReference
add(PdfOutline outline)
void
addAnnotation(PdfAnnotation annot)
Use this methods to add aPdfAnnotation
or aPdfFormField
to the document.void
addDocument(PdfReader reader)
void
addDocument(PdfReader reader, List<Integer> pagesToKeep)
void
addPage(PdfImportedPage iPage)
Add an imported page to our outputvoid
addPage(Rectangle rect, int rotation)
Adds a blank page.PdfIndirectObject
addToBody(PdfObject object, PdfIndirectReference ref)
Use this method to add a PDF object to the PDF body.PdfIndirectObject
addToBody(PdfObject object, PdfIndirectReference ref, boolean formBranching)
Use this method to add a PDF object to the PDF body.protected void
cacheObject(PdfIndirectObject iobj)
Use this method for caching objects.void
close()
Signals that theDocument
was closed and that no otherElements
will be added.protected PdfArray
copyArray(PdfArray in)
Translate a PRArray to a PdfArray.protected PdfArray
copyArray(PdfArray in, boolean keepStruct, boolean directRootKids)
Translate a PRArray to a PdfArray.protected PdfDictionary
copyDictionary(PdfDictionary in)
Translate a PRDictionary to a PdfDictionary.protected PdfDictionary
copyDictionary(PdfDictionary in, boolean keepStruct, boolean directRootKids)
Translate a PRDictionary to a PdfDictionary.void
copyDocumentFields(PdfReader reader)
Copy document fields to a destination document.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 PdfIndirectReference
copyIndirect(PRIndirectReference in, boolean keepStructure, boolean directRootKids)
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-objectprotected PdfObject
copyObject(PdfObject in, boolean keepStruct, boolean directRootKids)
Translate a PR-object to a Pdf-objectprotected PdfStream
copyStream(PRStream in)
Translate a PRStream to a PdfStream.PdfCopy.PageStamp
createPageStamp(PdfImportedPage iPage)
Create a page stamp.protected void
fixStructureTreeRoot(HashSet<RefKey> activeKeys, HashSet<PdfName> activeClassMaps)
protected void
fixTaggedStructure()
protected void
flushAcroFields()
protected void
flushIndirectObjects()
protected void
flushTaggedObjects()
Fix structure of tagged document: remove unused objects, remove unused items from class map, fix xref table due to removed objects.void
freeReader(PdfReader reader)
Use this method to writes the reader to the document and free the memory used by it.protected PdfDictionary
getCatalog(PdfIndirectReference rootObj)
protected Counter
getCounter()
PdfImportedPage
getImportedPage(PdfReader reader, int pageNumber)
Grabs a page from the input documentPdfImportedPage
getImportedPage(PdfReader reader, int pageNumber, boolean keepTaggedPdfStructure)
protected PdfImportedPage
getImportedPageImpl(PdfReader reader, int pageNumber)
protected PdfName
getOffStateName(PdfDictionary widget)
boolean
isRotateContents()
Getter for property rotateContents.protected boolean
isStructTreeRootReference(PdfIndirectReference prRef)
protected int
setFromIPage(PdfImportedPage iPage)
convenience method.protected void
setFromReader(PdfReader reader)
convenience method.void
setMergeFields()
void
setPageEvent(PdfPageEvent event)
Setting page events isn't possible with Pdf(Smart)Copy.void
setRotateContents(boolean rotateContents)
Setter for property rotateContents.protected void
structureTreeRootKidsForReaderImported(PdfReader reader)
-
Methods inherited from class com.itextpdf.text.pdf.PdfWriter
add, addCalculationOrder, addDeveloperExtension, addDirectImageSimple, addDirectImageSimple, addFileAttachment, addFileAttachment, addFileAttachment, addJavaScript, addJavaScript, addJavaScript, addJavaScript, addJavaScript, addJavaScript, addNamedDestination, addNamedDestinations, addOCGRadioGroup, addPageDictEntry, addSharedObjectsToBody, addToBody, addToBody, addToBody, addToBody, addViewerPreference, addXFormsToBody, buildStructTreeRootForTagged, checkElementRole, checkPdfIsoConformance, checkPdfIsoConformance, clearTextWrap, createAnnotation, createAnnotation, createAnnotation, createXmpMetadata, createXmpWriter, createXmpWriter, fillOCProperties, getAcroForm, getBoxSize, getBoxSize, getColorProfile, getCompressionLevel, getCurrentDocumentSize, getCurrentPageNumber, getDefaultColorspace, getDirectContent, getDirectContentUnder, getExtraCatalog, getGroup, getIndirectReferenceNumber, getInfo, getInstance, getInstance, getNameString, getNewObjectNumber, getOCProperties, getOs, getPageDictEntries, getPageEvent, getPageNumber, getPageReference, getPageSize, getPdfIndirectReference, getPdfReaderInstance, getPDFXConformance, getReferenceJBIG2Globals, getRootOutline, getRunDirection, getSpaceCharRatio, getStandardStructElems, getStructureTreeRoot, getTabs, getTtfUnicodeWriter, getVerticalPosition, getXmpWriter, initPdfIsoConformance, isFullCompression, isPageEmpty, isPdfIso, isPdfX, isRgbTransparencyBlending, isStrictImageSequence, isTagged, isUserProperties, lockLayer, needToBeMarkedInContent, open, releaseTemplate, reorderPages, resetPageDictEntries, setAdditionalAction, setAtLeastPdfVersion, setBoxSize, setCollection, setCompressionLevel, setCropBoxSize, setDefaultColorspace, setDuration, setEncryption, setEncryption, setEncryption, setEncryption, setEncryption, setFullCompression, setGroup, setInitialLeading, setLanguage, setLinearPageMode, setOpenAction, setOpenAction, setOutlines, setOutputIntents, setOutputIntents, setOutputIntents, setPageAction, setPageEmpty, setPageLabels, setPageViewport, setPageXmpMetadata, setPdfVersion, setPdfVersion, setPDFXConformance, setRgbTransparencyBlending, setRunDirection, setSigFlags, setSpaceCharRatio, setStrictImageSequence, setTabs, setTagged, setTagged, setThumbnail, setTransition, setUserProperties, setUserunit, setViewerPreferences, setXmpMetadata, useExternalCacheForTagStructure, writeKeyInfo, writeOutlines
-
Methods inherited from class com.itextpdf.text.DocWriter
add, addTabs, flush, getISOBytes, isCloseStream, isPaused, newPage, pause, resetPageCount, resume, setCloseStream, setMarginMirroring, setMarginMirroringTopBottom, setMargins, setPageCount, setPageSize, write, write, writeEnd, writeEnd, writeMarkupAttributes, writeStart
-
-
-
-
Field Detail
-
COUNTER
protected static Counter COUNTER
-
indirectMap
protected HashMap<PdfReader,HashMap<RefKey,com.itextpdf.text.pdf.PdfCopy.IndirectReferences>> indirectMap
-
reader
protected PdfReader reader
-
namePtr
protected int[] namePtr
-
fieldArray
protected PdfArray fieldArray
-
fieldTemplates
protected HashSet<PdfTemplate> fieldTemplates
-
structTreeRootReference
protected PRIndirectReference structTreeRootReference
-
indirectObjects
protected LinkedHashMap<RefKey,PdfIndirectObject> indirectObjects
-
savedObjects
protected ArrayList<PdfIndirectObject> savedObjects
-
importedPages
protected ArrayList<PdfCopy.ImportedPage> importedPages
-
updateRootKids
protected boolean updateRootKids
-
mergeFields
protected boolean mergeFields
-
fields
protected ArrayList<AcroFields> fields
-
-
Constructor Detail
-
PdfCopy
public PdfCopy(Document document, OutputStream os) throws DocumentException
Constructor- Parameters:
document
- documentos
- outputstream- Throws:
DocumentException
-
-
Method Detail
-
getCounter
protected Counter getCounter()
- Overrides:
getCounter
in classPdfWriter
-
setPageEvent
public void setPageEvent(PdfPageEvent event)
Setting page events isn't possible with Pdf(Smart)Copy. Use the PageStamp class if you want to add content to copied pages.- Overrides:
setPageEvent
in classPdfWriter
- Parameters:
event
- thePdfPageEvent
for this document- See Also:
PdfWriter.setPageEvent(com.itextpdf.text.pdf.PdfPageEvent)
-
isRotateContents
public boolean isRotateContents()
Getter for property rotateContents.- Returns:
- Value of property rotateContents.
-
setRotateContents
public void setRotateContents(boolean rotateContents)
Setter for property rotateContents.- Parameters:
rotateContents
- New value of property rotateContents.
-
setMergeFields
public void setMergeFields()
-
getImportedPage
public PdfImportedPage getImportedPage(PdfReader reader, int pageNumber)
Grabs a page from the input document- Overrides:
getImportedPage
in classPdfWriter
- Parameters:
reader
- the reader of the documentpageNumber
- which page to get- Returns:
- the page
-
getImportedPage
public PdfImportedPage getImportedPage(PdfReader reader, int pageNumber, boolean keepTaggedPdfStructure) throws BadPdfFormatException
- Throws:
BadPdfFormatException
-
structureTreeRootKidsForReaderImported
protected void structureTreeRootKidsForReaderImported(PdfReader reader)
-
fixStructureTreeRoot
protected void fixStructureTreeRoot(HashSet<RefKey> activeKeys, HashSet<PdfName> activeClassMaps)
-
getImportedPageImpl
protected PdfImportedPage getImportedPageImpl(PdfReader reader, int pageNumber)
-
copyIndirect
protected PdfIndirectReference copyIndirect(PRIndirectReference in, boolean keepStructure, boolean directRootKids) 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
-
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, boolean keepStruct, boolean directRootKids) throws IOException, BadPdfFormatException
Translate a PRDictionary to a PdfDictionary. Also translate all of the objects contained in it.- 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, boolean keepStruct, boolean directRootKids) throws IOException, BadPdfFormatException
Translate a PRArray to a PdfArray. Also translate all of the objects contained in it- 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, boolean keepStruct, boolean directRootKids) throws IOException, BadPdfFormatException
Translate a PR-object to a Pdf-object- 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 imported page, 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
-
addPage
public void addPage(Rectangle rect, int rotation) throws DocumentException
Adds a blank page.- Parameters:
rect
- The page dimensionrotation
- The rotation angle in degrees- Throws:
DocumentException
- Since:
- 2.1.5
-
addDocument
public void addDocument(PdfReader reader, List<Integer> pagesToKeep) throws DocumentException, IOException
- Throws:
DocumentException
IOException
-
copyDocumentFields
public void copyDocumentFields(PdfReader reader) throws DocumentException, IOException
Copy document fields to a destination document.- Parameters:
reader
- a document where fields are copied from.- Throws:
DocumentException
IOException
-
addDocument
public void addDocument(PdfReader reader) throws DocumentException, IOException
- Throws:
DocumentException
IOException
-
addToBody
public PdfIndirectObject addToBody(PdfObject object, PdfIndirectReference ref) throws IOException
Description copied from class:PdfWriter
Use this method to add a PDF object to the PDF body. Use this method only if you know what you're doing!- Overrides:
addToBody
in classPdfWriter
- Returns:
- a PdfIndirectObject
- Throws:
IOException
-
addToBody
public PdfIndirectObject addToBody(PdfObject object, PdfIndirectReference ref, boolean formBranching) throws IOException
Description copied from class:PdfWriter
Use this method to add a PDF object to the PDF body. Use this method only if you know what you're doing!- Overrides:
addToBody
in classPdfWriter
- Returns:
- a PdfIndirectObject
- Throws:
IOException
-
cacheObject
protected void cacheObject(PdfIndirectObject iobj)
Description copied from class:PdfWriter
Use this method for caching objects.- Overrides:
cacheObject
in classPdfWriter
- See Also:
PdfIndirectObject
-
flushTaggedObjects
protected void flushTaggedObjects() throws IOException
Description copied from class:PdfWriter
Fix structure of tagged document: remove unused objects, remove unused items from class map, fix xref table due to removed objects.- Overrides:
flushTaggedObjects
in classPdfWriter
- Throws:
IOException
-
flushAcroFields
protected void flushAcroFields() throws IOException, BadPdfFormatException
- Overrides:
flushAcroFields
in classPdfWriter
- Throws:
IOException
BadPdfFormatException
-
fixTaggedStructure
protected void fixTaggedStructure() throws IOException
- Throws:
IOException
-
flushIndirectObjects
protected void flushIndirectObjects() throws IOException
- Throws:
IOException
-
getCatalog
protected PdfDictionary getCatalog(PdfIndirectReference rootObj)
- Overrides:
getCatalog
in classPdfWriter
-
isStructTreeRootReference
protected boolean isStructTreeRootReference(PdfIndirectReference prRef)
-
close
public void close()
Signals that theDocument
was closed and that no otherElements
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.
- Specified by:
close
in interfaceDocListener
- Overrides:
close
in classPdfWriter
- See Also:
DocWriter.close()
-
add
public PdfIndirectReference add(PdfOutline outline)
-
addAnnotation
public void addAnnotation(PdfAnnotation annot)
Description copied from interface:PdfAnnotations
Use this methods to add aPdfAnnotation
or aPdfFormField
to the document. Only the top parent of aPdfFormField
needs to be added.- Specified by:
addAnnotation
in interfacePdfAnnotations
- Overrides:
addAnnotation
in classPdfWriter
- Parameters:
annot
- thePdfAnnotation
or thePdfFormField
to add- See Also:
PdfAnnotations.addAnnotation(com.itextpdf.text.pdf.PdfAnnotation)
-
freeReader
public void freeReader(PdfReader reader) throws IOException
Description copied from class:PdfWriter
Use this method to writes the reader to the document and free the memory used by it. The main use is when concatenating multiple documents to keep the memory usage restricted to the current appending document.- Overrides:
freeReader
in classPdfWriter
- Parameters:
reader
- thePdfReader
to free- Throws:
IOException
- on error
-
getOffStateName
protected PdfName getOffStateName(PdfDictionary widget)
-
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
-
-