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
Make copies of PDF documents. Documents can be edited after reading and
before writing them out.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static class(package private) static classThis class holds information about indirect references, since they are renumbered by iText.static classstatic classNested classes/interfaces inherited from class PdfWriter
PdfWriter.PdfBody, PdfWriter.PdfTrailer -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate PdfIndirectReferenceprivate static final PdfNameprivate static intprotected static Counterprivate intprotected PdfArrayprotected ArrayList<AcroFields> protected HashSet<PdfTemplate> private booleanprotected ArrayList<PdfCopy.ImportedPage> protected HashMap<PdfReader, HashMap<RefKey, PdfCopy.IndirectReferences>> protected LinkedHashMap<RefKey, PdfIndirectObject> protected HashMap<RefKey, PdfCopy.IndirectReferences> private static final PdfNameprivate static final Loggerprivate HashMap<Integer, PdfIndirectObject> private HashSet<PdfIndirectObject> protected booleanprivate booleanprotected int[]private booleanprotected PdfReaderprivate PdfDictionaryprivate booleanHolds value of property rotateContents.protected ArrayList<PdfIndirectObject> private PdfStructTreeControllerprotected PRIndirectReferenceprivate HashMap<RefKey, PdfIndirectObject> private HashMap<Integer, PdfIndirectObject> protected booleanprivate static final IntegerFields inherited from class 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, ENCRYPTION_MASK, 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 -
Method Summary
Modifier and TypeMethodDescriptionadd(PdfOutline outline) (package private) PdfIndirectReferenceadd(PdfPage page, PdfContents contents) Adds somePdfContentsto this Writer.voidaddAnnotation(PdfAnnotation annot) Use this methods to add aPdfAnnotationor aPdfFormFieldto the document.voidaddDocument(PdfReader reader) voidaddDocument(PdfReader reader, List<Integer> pagesToKeep) private voidaddFieldResources(PdfDictionary catalog) voidaddPage(PdfImportedPage iPage) Add an imported page to our outputvoidAdds a blank page.private voidaddPageOffsetToField(Map<String, AcroFields.Item> fd, int pageOffset) addToBody(PdfObject object, PdfIndirectReference ref) Use this method to add a PDF object to the PDF body.addToBody(PdfObject object, PdfIndirectReference ref, boolean formBranching) Use this method to add a PDF object to the PDF body.private voidadjustTabOrder(PdfArray annots, PdfIndirectReference ind, PdfNumber nn) private PdfArraybranchForm(HashMap<String, Object> level, PdfIndirectReference parent, String fname) protected voidcacheObject(PdfIndirectObject iobj) Use this method for caching objects.private intprivate voidclearIndirects(PdfReader reader) voidclose()Signals that theDocumentwas closed and that no otherElementswill be added.private booleancontainsInactivePg(PdfDictionary dict, HashSet<RefKey> activeKeys) protected PdfArrayTranslate a PRArray to a PdfArray.protected PdfArrayTranslate a PRArray to a PdfArray.protected PdfDictionaryTranslate a PRDictionary to a PdfDictionary.protected PdfDictionarycopyDictionary(PdfDictionary in, boolean keepStruct, boolean directRootKids) Translate a PRDictionary to a PdfDictionary.voidcopyDocumentFields(PdfReader reader) Copy document fields to a destination document.protected PdfIndirectReferenceTranslate a PRIndirectReference to a PdfIndirectReference In addition, translates the object numbers, and copies the referenced object to the output file.protected PdfIndirectReferencecopyIndirect(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 PdfObjectcopyObject(PdfObject in) Translate a PR-object to a Pdf-objectprotected PdfObjectcopyObject(PdfObject in, boolean keepStruct, boolean directRootKids) Translate a PR-object to a Pdf-objectprotected PdfStreamcopyStream(PRStream in) Translate a PRStream to a PdfStream.private voidcreatePageStamp(PdfImportedPage iPage) Create a page stamp.private voidcreateWidgets(ArrayList<Object> list, AcroFields.Item item) private ArrayList<PdfIndirectReference> findActiveParents(HashSet<RefKey> activeKeys) private voidfindActives(ArrayList<PdfIndirectReference> actives, HashSet<RefKey> activeKeys, HashSet<PdfName> activeClassMaps) private voidfindActivesFromArray(PdfArray array, ArrayList<PdfIndirectReference> actives, HashSet<RefKey> activeKeys, HashSet<PdfName> activeClassMaps) private voidfindActivesFromDict(PdfDictionary dict, ArrayList<PdfIndirectReference> actives, HashSet<RefKey> activeKeys, HashSet<PdfName> activeClassMaps) private voidfindActivesFromReference(PdfIndirectReference iref, ArrayList<PdfIndirectReference> actives, HashSet<RefKey> activeKeys) private voidfixPgKey(ArrayList<PdfIndirectReference> newRefs, HashSet<RefKey> activeKeys) protected voidfixStructureTreeRoot(HashSet<RefKey> activeKeys, HashSet<PdfName> activeClassMaps) protected voidprotected voidprotected voidprotected voidFix structure of tagged document: remove unused objects, remove unused items from class map, fix xref table due to removed objects.voidfreeReader(PdfReader reader) Use this method to writes the reader to the document and free the memory used by it.protected PdfDictionarygetCatalog(PdfIndirectReference rootObj) private static StringgetCOName(PdfReader reader, PRIndirectReference ref) protected Counter(package private) static IntegergetFlags(PdfDictionary field) getImportedPage(PdfReader reader, int pageNumber) Grabs a page from the input documentgetImportedPage(PdfReader reader, int pageNumber, boolean keepTaggedPdfStructure) protected PdfImportedPagegetImportedPageImpl(PdfReader reader, int pageNumber) protected PdfNamegetOffStateName(PdfDictionary widget) (package private) static booleanisCheckButton(PdfDictionary field) (package private) static booleanisRadioButton(PdfDictionary field) booleanGetter for property rotateContents.protected boolean(package private) static booleanisTextField(PdfDictionary field) private voidmergeField(String name, AcroFields.Item item) private voidprivate voidprivate PdfObjectprivate voidremoveInactiveReferences(PdfArray array, HashSet<RefKey> activeKeys) protected intsetFromIPage(PdfImportedPage iPage) convenience method.protected voidsetFromReader(PdfReader reader) convenience method.voidvoidsetPageEvent(PdfPageEvent event) Setting page events isn't possible with Pdf(Smart)Copy.voidsetRotateContents(boolean rotateContents) Setter for property rotateContents.protected voidprivate voidprivate voidupdateCalculationOrder(PdfReader reader) private voidprivate voidwriteObjectToBody(PdfIndirectObject object) Methods inherited from class PdfWriter
add, add, addAnnotation, addCalculationOrder, addDeveloperExtension, addDirectImageSimple, addDirectImageSimple, addDirectTemplateSimple, addFileAttachment, addFileAttachment, addFileAttachment, addJavaScript, addJavaScript, addJavaScript, addJavaScript, addJavaScript, addJavaScript, addLocalDestinations, addNamedDestination, addNamedDestinations, addOCGRadioGroup, addPageDictEntry, addSharedObjectsToBody, addSimple, addSimple, addSimpleExtGState, addSimplePattern, addSimplePatternColorspace, addSimpleProperty, addSimpleShading, addSimpleShadingPattern, addToBody, addToBody, addToBody, addToBody, addViewerPreference, addXFormsToBody, buildStructTreeRootForTagged, checkElementRole, checkPdfIsoConformance, checkPdfIsoConformance, clearTextWrap, createAnnotation, createAnnotation, createAnnotation, createXmpMetadata, createXmpWriter, createXmpWriter, eliminateFontSubset, fillOCProperties, getAcroForm, getBoxSize, getBoxSize, getColorProfile, getColorspaceName, getCompressionLevel, getCurrentDocumentSize, getCurrentPage, getCurrentPageNumber, getDefaultColorspace, getDirectContent, getDirectContentUnder, getEncryption, getExtraCatalog, getGroup, getImageReference, getIndirectReferenceNumber, getInfo, getInstance, getInstance, getNameString, getNewObjectNumber, getOCProperties, getOs, getPageDictEntries, getPageEvent, getPageNumber, getPageReference, getPageSize, getPdfDocument, getPdfIndirectReference, getPdfReaderInstance, getPdfVersion, getPDFXConformance, getReaderFile, getReferenceJBIG2Globals, getRootOutline, getRunDirection, getSpaceCharRatio, getStandardStructElems, getStructureTreeRoot, getTabs, getTtfUnicodeWriter, getVerticalPosition, getXmpWriter, initPdfIsoConformance, isFullCompression, isPageEmpty, isPdfIso, isPdfX, isRgbTransparencyBlending, isStrictImageSequence, isTagged, isUserProperties, lockLayer, needToBeMarkedInContent, open, propertyExists, registerLayer, releaseTemplate, reorderPages, resetContent, 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, writeOutlinesMethods inherited from class DocWriter
add, addTabs, flush, getISOBytes, isCloseStream, isPaused, newPage, pause, resetPageCount, resume, setCloseStream, setMarginMirroring, setMarginMirroringTopBottom, setMargins, setPageCount, setPageSize, write, write, writeEnd, writeEnd, writeMarkupAttributes, writeStart
-
Field Details
-
LOGGER
-
COUNTER
-
indirects
-
indirectMap
-
parentObjects
-
disableIndirects
-
reader
-
namePtr
protected int[] namePtr -
rotateContents
private boolean rotateContentsHolds value of property rotateContents. -
fieldArray
-
fieldTemplates
-
structTreeController
-
currentStructArrayNumber
private int currentStructArrayNumber -
structTreeRootReference
-
indirectObjects
-
savedObjects
-
importedPages
-
updateRootKids
protected boolean updateRootKids -
annotId
-
annotIdCnt
private static int annotIdCnt -
mergeFields
protected boolean mergeFields -
needAppearances
private boolean needAppearances -
hasSignature
private boolean hasSignature -
acroForm
-
tabOrder
-
calculationOrderRefs
-
resources
-
fields
-
calculationOrder
-
fieldTree
-
unmergedMap
-
unmergedIndirectRefsMap
-
mergedMap
-
mergedSet
-
mergeFieldsInternalCall
private boolean mergeFieldsInternalCall -
iTextTag
-
zero
-
mergedRadioButtons
-
mergedTextFields
-
readersWithImportedStructureTreeRootKids
-
widgetKeys
-
fieldKeys
-
-
Constructor Details
-
PdfCopy
Constructor- Parameters:
document- documentos- outputstream- Throws:
DocumentException
-
-
Method Details
-
getCounter
- Overrides:
getCounterin classPdfWriter
-
setPageEvent
Setting page events isn't possible with Pdf(Smart)Copy. Use the PageStamp class if you want to add content to copied pages.- Overrides:
setPageEventin classPdfWriter- Parameters:
event- thePdfPageEventfor this document- See Also:
-
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
Grabs a page from the input document- Overrides:
getImportedPagein 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
-
clearIndirects
-
checkStructureTreeRootKids
-
structureTreeRootKidsForReaderImported
-
fixStructureTreeRoot
-
getImportedPageImpl
-
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:
IOExceptionBadPdfFormatException
-
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:
IOExceptionBadPdfFormatException
-
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:
IOExceptionBadPdfFormatException
-
copyDictionary
Translate a PRDictionary to a PdfDictionary. Also translate all of the objects contained in it.- Throws:
IOExceptionBadPdfFormatException
-
copyStream
Translate a PRStream to a PdfStream. The data part copies itself.- Throws:
IOExceptionBadPdfFormatException
-
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:
IOExceptionBadPdfFormatException
-
copyArray
Translate a PRArray to a PdfArray. Also translate all of the objects contained in it- Throws:
IOExceptionBadPdfFormatException
-
copyObject
protected PdfObject copyObject(PdfObject in, boolean keepStruct, boolean directRootKids) throws IOException, BadPdfFormatException Translate a PR-object to a Pdf-object- Throws:
IOExceptionBadPdfFormatException
-
copyObject
Translate a PR-object to a Pdf-object- Throws:
IOExceptionBadPdfFormatException
-
setFromIPage
convenience method. Given an imported page, set our "globals" -
setFromReader
convenience method. Given a reader, set our "globals" -
addPage
Add an imported page to our output- Parameters:
iPage- an imported page- Throws:
IOExceptionBadPdfFormatException
-
addPage
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:
DocumentExceptionIOException
-
copyDocumentFields
Copy document fields to a destination document.- Parameters:
reader- a document where fields are copied from.- Throws:
DocumentExceptionIOException
-
addDocument
- Throws:
DocumentExceptionIOException
-
addToBody
Description copied from class:PdfWriterUse this method to add a PDF object to the PDF body. Use this method only if you know what you're doing!- Overrides:
addToBodyin classPdfWriter- Parameters:
object-ref-- Returns:
- a PdfIndirectObject
- Throws:
IOException
-
addToBody
public PdfIndirectObject addToBody(PdfObject object, PdfIndirectReference ref, boolean formBranching) throws IOException Description copied from class:PdfWriterUse this method to add a PDF object to the PDF body. Use this method only if you know what you're doing!- Overrides:
addToBodyin classPdfWriter- Parameters:
object-ref-formBranching-- Returns:
- a PdfIndirectObject
- Throws:
IOException
-
cacheObject
Description copied from class:PdfWriterUse this method for caching objects.- Overrides:
cacheObjectin classPdfWriter- Parameters:
iobj- @see PdfIndirectObject
-
flushTaggedObjects
Description copied from class:PdfWriterFix structure of tagged document: remove unused objects, remove unused items from class map, fix xref table due to removed objects.- Overrides:
flushTaggedObjectsin classPdfWriter- Throws:
IOException
-
flushAcroFields
- Overrides:
flushAcroFieldsin classPdfWriter- Throws:
IOExceptionBadPdfFormatException
-
fixTaggedStructure
- Throws:
IOException
-
removeInactiveReferences
-
containsInactivePg
-
findActiveParents
-
fixPgKey
-
findActives
-
findActivesFromReference
private void findActivesFromReference(PdfIndirectReference iref, ArrayList<PdfIndirectReference> actives, HashSet<RefKey> activeKeys) -
findActivesFromArray
-
findActivesFromDict
private void findActivesFromDict(PdfDictionary dict, ArrayList<PdfIndirectReference> actives, HashSet<RefKey> activeKeys, HashSet<PdfName> activeClassMaps) -
flushIndirectObjects
- Throws:
IOException
-
writeObjectToBody
- Throws:
IOException
-
updateAnnotationReferences
-
updateCalculationOrder
-
getCOName
-
mergeFields
private void mergeFields() -
addPageOffsetToField
-
mergeWithMaster
-
mergeField
-
createWidgets
-
propagate
- Throws:
IOException
-
createAcroForms
- Throws:
IOExceptionBadPdfFormatException
-
updateReferences
-
branchForm
private PdfArray branchForm(HashMap<String, Object> level, PdfIndirectReference parent, String fname) throws IOException, BadPdfFormatException- Throws:
IOExceptionBadPdfFormatException
-
adjustTabOrder
-
getCatalog
- Overrides:
getCatalogin classPdfWriter
-
isStructTreeRootReference
-
addFieldResources
- Throws:
IOException
-
close
public void close()Signals that theDocumentwas closed and that no otherElementswill 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:
closein interfaceDocListener- Overrides:
closein classPdfWriter- See Also:
-
add
-
addAnnotation
Description copied from interface:PdfAnnotationsUse this methods to add aPdfAnnotationor aPdfFormFieldto the document. Only the top parent of aPdfFormFieldneeds to be added.- Specified by:
addAnnotationin interfacePdfAnnotations- Overrides:
addAnnotationin classPdfWriter- Parameters:
annot- thePdfAnnotationor thePdfFormFieldto add- See Also:
-
add
Description copied from class:PdfWriterAdds somePdfContentsto this Writer.The document has to be open before you can begin to add content to the body of the document.
- Overrides:
addin classPdfWriter- Parameters:
page- thePdfPageto addcontents- thePdfContentsof the page- Returns:
- a
PdfIndirectReference - Throws:
PdfException- on error
-
freeReader
Description copied from class:PdfWriterUse 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:
freeReaderin classPdfWriter- Parameters:
reader- thePdfReaderto free- Throws:
IOException- on error
-
getOffStateName
-
getFlags
-
isCheckButton
-
isRadioButton
-
isTextField
-
createPageStamp
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
-