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.
- Author:
- Mark Thompson
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classstatic classstatic classNested classes/interfaces inherited from class PdfWriter
PdfWriter.PdfBody, PdfWriter.PdfTrailer -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static Counterprotected PdfArrayprotected ArrayList<AcroFields> protected HashSet<PdfTemplate> protected ArrayList<PdfCopy.ImportedPage> protected LinkedHashMap<RefKey, PdfIndirectObject> protected booleanprotected int[]protected PdfReaderprotected ArrayList<PdfIndirectObject> protected PRIndirectReferenceprotected booleanFields 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, 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) voidaddAnnotation(PdfAnnotation annot) Use this methods to add aPdfAnnotationor aPdfFormFieldto the document.voidaddDocument(PdfReader reader) voidaddDocument(PdfReader reader, List<Integer> pagesToKeep) voidaddPage(PdfImportedPage iPage) Add an imported page to our outputvoidAdds a blank page.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.protected voidcacheObject(PdfIndirectObject iobj) Use this method for caching objects.voidclose()Signals that theDocumentwas closed and that no otherElementswill be added.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.createPageStamp(PdfImportedPage iPage) Create a page stamp.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) protected CountergetImportedPage(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) booleanGetter for property rotateContents.protected booleanprotected 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 voidMethods inherited from class 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, 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
-
COUNTER
-
indirects
-
indirectMap
-
parentObjects
-
disableIndirects
-
reader
-
namePtr
protected int[] namePtr -
fieldArray
-
fieldTemplates
-
structTreeRootReference
-
indirectObjects
-
savedObjects
-
importedPages
-
updateRootKids
protected boolean updateRootKids -
mergeFields
protected boolean mergeFields -
fields
-
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
-
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
-
flushIndirectObjects
- Throws:
IOException
-
getCatalog
- Overrides:
getCatalogin classPdfWriter
-
isStructTreeRootReference
-
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:
-
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
-
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
-