}
PDDocumentInformation destInfo = destination.getDocumentInformation();
PDDocumentInformation srcInfo = source.getDocumentInformation();
destInfo.getDictionary().mergeInto( srcInfo.getDictionary() );
PDDocumentCatalog destCatalog = destination.getDocumentCatalog();
PDDocumentCatalog srcCatalog = source.getDocumentCatalog();
if( destCatalog.getOpenAction() == null )
{
destCatalog.setOpenAction( srcCatalog.getOpenAction() );
}
try
{
PDAcroForm destAcroForm = destCatalog.getAcroForm();
PDAcroForm srcAcroForm = srcCatalog.getAcroForm();
if( destAcroForm == null )
{
cloneForNewDocument( destination, srcAcroForm );
destCatalog.setAcroForm( srcAcroForm );
}
else
{
if( srcAcroForm != null )
{
mergeAcroForm(destination, destAcroForm, srcAcroForm);
}
}
}
catch(Exception e)
{
// if we are not ignoring exceptions, we'll re-throw this
if(!ignoreAcroFormErrors)
{
throw (IOException)e;
}
}
COSArray destThreads = (COSArray)destCatalog.getCOSDictionary().getDictionaryObject(
COSName.THREADS);
COSArray srcThreads = (COSArray)cloneForNewDocument(
destination,
destCatalog.getCOSDictionary().getDictionaryObject( COSName.THREADS ));
if( destThreads == null )
{
destCatalog.getCOSDictionary().setItem( COSName.THREADS, srcThreads );
}
else
{
destThreads.addAll( srcThreads );
}
PDDocumentNameDictionary destNames = destCatalog.getNames();
PDDocumentNameDictionary srcNames = srcCatalog.getNames();
if( srcNames != null )
{
if( destNames == null )
{
destCatalog.getCOSDictionary().setItem( COSName.NAMES, cloneForNewDocument( destination, srcNames ) );
}
else
{
cloneMerge(destination, srcNames, destNames);
}
}
PDDocumentOutline destOutline = destCatalog.getDocumentOutline();
PDDocumentOutline srcOutline = srcCatalog.getDocumentOutline();
if( srcOutline != null )
{
if( destOutline == null )
{
PDDocumentOutline cloned =
new PDDocumentOutline( (COSDictionary)cloneForNewDocument( destination, srcOutline ) );
destCatalog.setDocumentOutline( cloned );
}
else
{
PDOutlineItem first = srcOutline.getFirstChild();
if(first != null)
{
PDOutlineItem clonedFirst = new PDOutlineItem( (COSDictionary)cloneForNewDocument(
destination, first ));
destOutline.appendChild( clonedFirst );
}
}
}
String destPageMode = destCatalog.getPageMode();
String srcPageMode = srcCatalog.getPageMode();
if( destPageMode == null )
{
destCatalog.setPageMode( srcPageMode );
}
COSDictionary destLabels = (COSDictionary)destCatalog.getCOSDictionary().getDictionaryObject( COSName.PAGE_LABELS );
COSDictionary srcLabels = (COSDictionary)srcCatalog.getCOSDictionary().getDictionaryObject( COSName.PAGE_LABELS );
if( srcLabels != null )
{
int destPageCount = destination.getNumberOfPages();
COSArray destNums = null;
if( destLabels == null )
{
destLabels = new COSDictionary();
destNums = new COSArray();
destLabels.setItem( COSName.NUMS, destNums );
destCatalog.getCOSDictionary().setItem( COSName.PAGE_LABELS, destLabels );
}
else
{
destNums = (COSArray)destLabels.getDictionaryObject( COSName.NUMS );
}
COSArray srcNums = (COSArray)srcLabels.getDictionaryObject( COSName.NUMS );
if (srcNums != null)
{
for( int i=0; i<srcNums.size(); i+=2 )
{
COSNumber labelIndex = (COSNumber)srcNums.getObject( i );
long labelIndexValue = labelIndex.intValue();
destNums.add( COSInteger.get( labelIndexValue + destPageCount ) );
destNums.add( cloneForNewDocument( destination, srcNums.getObject( i+1 ) ) );
}
}
}
COSStream destMetadata = (COSStream)destCatalog.getCOSDictionary().getDictionaryObject( COSName.METADATA );
COSStream srcMetadata = (COSStream)srcCatalog.getCOSDictionary().getDictionaryObject( COSName.METADATA );
if( destMetadata == null && srcMetadata != null )
{
PDStream newStream = new PDStream( destination, srcMetadata.getUnfilteredStream(), false );
newStream.getStream().mergeInto( srcMetadata );
newStream.addCompression();