Top Description Inners Fields Constructors Methods
org.apache.pdfbox.multipdf

public Class Splitter

extends Object
Class Inheritance
Imports
java.io.IOException, java.util.ArrayList, .HashMap, .HashSet, .LinkedHashMap, .List, .Map, .Set, org.apache.commons.logging.Log, .LogFactory, org.apache.pdfbox.cos.COSArray, .COSBase, .COSDictionary, .COSName, .COSObject, org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction, org.apache.pdfbox.pdmodel.PDDocument, .PDDocumentCatalog, .PDDocumentInformation, .PDPage, .PDPageTree, .PDResources, .PDStructureElementNameTreeNode, org.apache.pdfbox.pdmodel.common.COSObjectable, .PDNameTreeNode, .PDNumberTreeNode, org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDParentTreeValue, .PDStructureElement, .PDStructureTreeRoot, org.apache.pdfbox.pdmodel.graphics.PDXObject, org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject, org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject, org.apache.pdfbox.pdmodel.interactive.action.PDAction, .PDActionFactory, .PDActionGoTo, org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation, .PDAnnotationLink, .PDAnnotationMarkup, .PDAnnotationPopup, .PDAnnotationWidget, .PDAppearanceStream, org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDDestination, .PDNamedDestination, .PDPageDestination

Split a document into several other documents.
Authors
Mario Ivankovits, Ben Litchfield, Tilman Hausherr

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
Splitter.KCloner

Class to help clone the /K tree.

Field Summary

Modifier and TypeField and Description
private Map<COSDictionary, COSDictionary>
private PDDocument
private int
private List<PDDocument>
private Map<PDPageDestination, PDPage>
private int
private Set<String>
private static final Log
private Map<COSDictionary, COSDictionary>
private Set<COSName>
private PDDocument
private int
private int
private RandomAccessStreamCache.StreamCacheCreateFunction
private Map<COSDictionary, COSDictionary>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private void
cloneIDTree(PDStructureTreeRoot srcStructTree, PDStructureTreeRoot destStructTree)

private void
cloneRoleMap(PDStructureTreeRoot srcStructTree, PDStructureTreeRoot destStructTree)

private void
cloneStructureTree(PDDocument destinationDocument)

Clone the structure tree from the source to the current destination document.

private void
cloneTreeElement(Map<Integer, COSObjectable> srcNumberTreeAsMap, Map<Integer, COSObjectable> dstNumberTreeAsMap, int sp)

protected PDDocument

Returns:

the newly created PDDocument.
createNewDocument
()

Create a new document to write the split contents to.

private void
createNewDocumentIfNecessary()

Helper method for creating new documents at the appropriate pages.

private void
fixDestinations(PDDocument destinationDocument)

Replace the page destinations, if the source and destination pages are in the target document.

protected final PDDocument

Returns:

current destination pdf
getDestinationDocument
()

The source PDF document.

protected final PDDocument

Returns:

the pdf to be split
getSourceDocument
()

The source PDF document.

public RandomAccessStreamCache.StreamCacheCreateFunction

Returns:

the current function to be used to create an instance of stream cache.
getStreamCacheCreateFunction
()

private void
processAnnotations(PDPage imported)

Clone all annotations because of changes possibly made, and because the structure tree is cloned.

protected void
processPage(PDPage
The page that is about to get processed.
page
)

Interface to start processing a new page.

private void
processPages()

Interface method to handle the start of the page processing.

private void
processResources(PDResources res, Map<Integer, COSObjectable> srcNumberTreeAsMap, Map<Integer, COSObjectable> dstNumberTreeAsMap, Set<COSDictionary> visited)

public void
setEndPage(int
the 1-based end page
end
)

This will set the end page.

public void
setSplitAtPage(int
The number of pages each split document should contain.
split
)

This will tell the splitting algorithm where to split the pages.

public void
setStartPage(int
the 1-based start page
start
)

This will set the start page.

public void
setStreamCacheCreateFunction(RandomAccessStreamCache.StreamCacheCreateFunction
the current function to be used to create an instance of stream cache.
streamCacheCreateFunction
)

Set the current function to be used to create an instance of stream cache.

public List<PDDocument>

Returns:

A list of all the split documents. These should all be saved before closing any documents, including the source document. Any further operations should be made after reloading them, to avoid problems due to resource sharing. For the same reason, they should not be saved with encryption.
split
(PDDocument
The document to split.
document
)

This will take a document and split into several other documents.

protected boolean

Returns:

true If a new document should be created.
splitAtPage
(int
the 0-based page number to be checked as splitting page
pageNumber
)

Check if it is necessary to create a new document.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

annotDictMapback to summary
private Map<COSDictionary, COSDictionary> annotDictMap
currentDestinationDocumentback to summary
private PDDocument currentDestinationDocument
currentPageNumberback to summary
private int currentPageNumber
destinationDocumentsback to summary
private List<PDDocument> destinationDocuments
destToFixMapback to summary
private Map<PDPageDestination, PDPage> destToFixMap
endPageback to summary
private int endPage
idSetback to summary
private Set<String> idSet
LOGback to summary
private static final Log LOG
pageDictMapback to summary
private Map<COSDictionary, COSDictionary> pageDictMap
roleSetback to summary
private Set<COSName> roleSet
sourceDocumentback to summary
private PDDocument sourceDocument
splitLengthback to summary
private int splitLength
startPageback to summary
private int startPage
streamCacheCreateFunctionback to summary
private RandomAccessStreamCache.StreamCacheCreateFunction streamCacheCreateFunction
structDictMapback to summary
private Map<COSDictionary, COSDictionary> structDictMap

Constructor Detail

Splitterback to summary
public Splitter()

Method Detail

cloneIDTreeback to summary
private void cloneIDTree(PDStructureTreeRoot srcStructTree, PDStructureTreeRoot destStructTree) throws IOException
cloneRoleMapback to summary
private void cloneRoleMap(PDStructureTreeRoot srcStructTree, PDStructureTreeRoot destStructTree)
cloneStructureTreeback to summary
private void cloneStructureTree(PDDocument destinationDocument) throws IOException

Clone the structure tree from the source to the current destination document.

cloneTreeElementback to summary
private void cloneTreeElement(Map<Integer, COSObjectable> srcNumberTreeAsMap, Map<Integer, COSObjectable> dstNumberTreeAsMap, int sp)
createNewDocumentback to summary
protected PDDocument createNewDocument() throws IOException

Create a new document to write the split contents to.

Returns:PDDocument

the newly created PDDocument.

Exceptions
IOException:
If there is an problem creating the new document.
createNewDocumentIfNecessaryback to summary
private void createNewDocumentIfNecessary() throws IOException

Helper method for creating new documents at the appropriate pages.

Exceptions
IOException:
If there is an error creating the new document.
fixDestinationsback to summary
private void fixDestinations(PDDocument destinationDocument)

Replace the page destinations, if the source and destination pages are in the target document. This must be called after all pages (and its annotations) are processed.

getDestinationDocumentback to summary
protected final PDDocument getDestinationDocument()

The source PDF document.

Returns:PDDocument

current destination pdf

getSourceDocumentback to summary
protected final PDDocument getSourceDocument()

The source PDF document.

Returns:PDDocument

the pdf to be split

getStreamCacheCreateFunctionback to summary
public RandomAccessStreamCache.StreamCacheCreateFunction getStreamCacheCreateFunction()
Returns:RandomAccessStreamCache.StreamCacheCreateFunction

the current function to be used to create an instance of stream cache.

processAnnotationsback to summary
private void processAnnotations(PDPage imported) throws IOException

Clone all annotations because of changes possibly made, and because the structure tree is cloned.

processPageback to summary
protected void processPage(PDPage page) throws IOException

Interface to start processing a new page.

Parameters
page:PDPage

The page that is about to get processed.

Exceptions
IOException:
If there is an error creating the new document.
processPagesback to summary
private void processPages() throws IOException

Interface method to handle the start of the page processing.

Exceptions
IOException:
If an IO error occurs.
processResourcesback to summary
private void processResources(PDResources res, Map<Integer, COSObjectable> srcNumberTreeAsMap, Map<Integer, COSObjectable> dstNumberTreeAsMap, Set<COSDictionary> visited) throws IOException
setEndPageback to summary
public void setEndPage(int end)

This will set the end page.

Parameters
end:int

the 1-based end page

Exceptions
IllegalArgumentException:
if the end page is smaller than one.
setSplitAtPageback to summary
public void setSplitAtPage(int split)

This will tell the splitting algorithm where to split the pages. The default is 1, so every page will become a new document. If it was two then each document would contain 2 pages. If the source document had 5 pages it would split into 3 new documents, 2 documents containing 2 pages and 1 document containing one page.

Parameters
split:int

The number of pages each split document should contain.

Exceptions
IllegalArgumentException:
if the page is smaller than one.
setStartPageback to summary
public void setStartPage(int start)

This will set the start page.

Parameters
start:int

the 1-based start page

Exceptions
IllegalArgumentException:
if the start page is smaller than one.
setStreamCacheCreateFunctionback to summary
public void setStreamCacheCreateFunction(RandomAccessStreamCache.StreamCacheCreateFunction streamCacheCreateFunction)

Set the current function to be used to create an instance of stream cache.

Parameters
streamCacheCreateFunction:RandomAccessStreamCache.StreamCacheCreateFunction

the current function to be used to create an instance of stream cache.

splitback to summary
public List<PDDocument> split(PDDocument document) throws IOException

This will take a document and split into several other documents.

Parameters
document:PDDocument

The document to split.

Returns:List<PDDocument>

A list of all the split documents. These should all be saved before closing any documents, including the source document. Any further operations should be made after reloading them, to avoid problems due to resource sharing. For the same reason, they should not be saved with encryption.

Exceptions
IOException:
If there is an IOError
splitAtPageback to summary
protected boolean splitAtPage(int pageNumber)

Check if it is necessary to create a new document. By default a split occurs at every page. If you wanted to split based on some complex logic then you could override this method. For example. protected void splitAtPage() { // will split at pages with prime numbers only return isPrime(pageNumber); }

Parameters
pageNumber:int

the 0-based page number to be checked as splitting page

Returns:boolean

true If a new document should be created.

org.apache.pdfbox.multipdf back to summary

private Class Splitter.KCloner

extends Object
Class Inheritance

Class to help clone the /K tree. It clones structure elements and fills the structure elements map. Pages are replaced with the help of the page map. Elements with pages that don't belong to the destination are removed from the clone.

Field Summary

Modifier and TypeField and Description
pack-priv PDPageTree

Constructor Summary

AccessConstructor and Description
public
KCloner(PDPageTree dstPageTree)

Method Summary

Modifier and TypeMethod and Description
private COSBase
createArrayClone(COSBase src, COSBase dstParent, COSDictionary currentPageDict)

pack-priv COSBase

Returns:

a clone, or null if source is null or if there is no clone because it belongs to a different page or to no page.
createClone
(COSBase
source dictionary or array.
src
,
COSBase
for the /P entry; parameter needed because arrays don't keep a parent.
dstParent
,
COSDictionary
used to remember whether we have a page parent somewhere or not. Starts with null.
currentPageDict
)

Creates a clone of the source.

private COSBase
createDictionaryClone(COSBase src, COSBase dstParent, COSDictionary currentPageDict)

private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

dstPageTreeback to summary
pack-priv PDPageTree dstPageTree

Constructor Detail

KClonerback to summary
public KCloner(PDPageTree dstPageTree)

Method Detail

createArrayCloneback to summary
private COSBase createArrayClone(COSBase src, COSBase dstParent, COSDictionary currentPageDict)
createCloneback to summary
pack-priv COSBase createClone(COSBase src, COSBase dstParent, COSDictionary currentPageDict)

Creates a clone of the source.

Parameters
src:COSBase

source dictionary or array.

dstParent:COSBase

for the /P entry; parameter needed because arrays don't keep a parent.

currentPageDict:COSDictionary

used to remember whether we have a page parent somewhere or not. Starts with null.

Returns:COSBase

a clone, or null if source is null or if there is no clone because it belongs to a different page or to no page.

createDictionaryCloneback to summary
private COSBase createDictionaryClone(COSBase src, COSBase dstParent, COSDictionary currentPageDict)
removePossibleOrphanAnnotationback to summary
private void removePossibleOrphanAnnotation(COSDictionary srcObj, COSDictionary srcDict, COSDictionary currentPageDict, COSDictionary dstDict)