/**
* Copyright (c) 2003, www.pdfbox.org
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of pdfbox; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://www.pdfbox.org
*
*/
package org.pdfbox.examples.persistence;
import java.io.IOException;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.FileInputStream;
import java.util.Iterator;
import org.pdfbox.pdfparser.PDFParser;
import org.pdfbox.pdfwriter.COSWriter;
import org.pdfbox.cos.COSDocument;
import org.pdfbox.cos.COSDictionary;
import org.pdfbox.cos.COSObject;
import org.pdfbox.cos.COSName;
import org.pdfbox.cos.COSArray;
import org.pdfbox.cos.COSNumber;
import org.pdfbox.cos.COSInteger;
import org.pdfbox.exceptions.COSVisitorException;
/**
* This example concatenates two documents and writes the result
*
* @author Michael Traut
* @version $Revision: 1.6 $
*/
public class AppendDoc
{
/**
* Constructor
*/
public AppendDoc()
{
super();
}
/**
* append all pages from source to destination
*
* todo: this method will go to the pdfmodel package one day
*
* @param destination the document to receive the pages
* @param source the document originating the new pages
*
*/
public void appendDocument(COSDocument destination, COSDocument source)
{
//first append the pages
COSDictionary pages2 = getPages(source);
COSArray kids = (COSArray) pages2.getItem(COSName.getPDFName("Kids"));
for (Iterator i = kids.iterator(); i.hasNext();)
{
COSDictionary page = (COSDictionary) ((COSObject) i.next()).getObject();
appendPage(destination, page);
}
COSDictionary destTrailer = destination.getTrailer();
COSDictionary destRoot = (COSDictionary)destTrailer.getDictionaryObject( COSName.ROOT );
COSDictionary srcTrailer = source.getTrailer();
COSDictionary srcRoot = (COSDictionary)srcTrailer.getDictionaryObject( COSName.ROOT );
COSName openAction = COSName.getPDFName( "OpenAction" );
if( destRoot.getItem( openAction ) == null )
{
COSObject open = (COSObject)srcRoot.getItem( openAction );
if( open != null )
{
destination.addObject( open );
destRoot.setItem( openAction, open );
}
}
COSName acroForm = COSName.getPDFName( "AcroForm" );
if( destRoot.getItem( acroForm ) == null )
{
COSObject form = (COSObject)srcRoot.getItem( acroForm );
if( form != null )
{
destination.addObject( form );
destRoot.setItem( acroForm, form );
}
}
}
/**
* append a page dict to destination
*
* todo: this method will go to the pdfmodel package one day
*
* @param destination the document to receive the page
* @param page the page to append to the document
*
*/
public void appendPage(COSDocument destination, COSDictionary page)
{
// get the parent object (pages dictionary)
COSDictionary pages = getPages(destination);
// and set in the page object
page.setItem(COSName.PARENT, destination.createObject(pages));
// add new page to kids entry in the pages dictionary
COSArray kids = (COSArray) pages.getItem(COSName.getPDFName("Kids"));
kids.add(destination.createObject(page));
// and increase count
COSNumber count = (COSNumber) pages.getItem(COSName.COUNT);
pages.setItem(COSName.COUNT, new COSInteger(count.intValue() + 1));
}
/**
* concat two pdf documents
*
* @param in1 The first template file
* @param in2 The second template file
* @param out The created fiel with all pages from document one and document two
*
* @throws IOException If there is an error writing the data.
* @throws COSVisitorException If there is an error generating the PDF document.
*/
public void doIt(String in1, String in2, String out) throws IOException, COSVisitorException
{
InputStream is1 = null;
COSDocument doc1 = null;
InputStream is2 = null;
COSDocument doc2 = null;
OutputStream os = null;
COSWriter writer = null;
try
{
is1 = new FileInputStream(in1);
PDFParser parser1 = new PDFParser(is1);
parser1.parse();
doc1 = parser1.getDocument();
is2 = new FileInputStream(in2);
PDFParser parser2 = new PDFParser(is2);
parser2.parse();
doc2 = parser2.getDocument();
appendDocument(doc1, doc2);
os = new FileOutputStream(out);
writer = new COSWriter(os);
writer.write(doc1);
}
finally
{
close( is1 );
close( doc1 );
close( is2 );
close( doc2 );
close( writer );
close( os );
}
}
private void close( InputStream is ) throws IOException
{
if( is != null )
{
is.close();
}
}
private void close( OutputStream os ) throws IOException
{
if( os != null )
{
os.close();
}
}
private void close( COSWriter writer ) throws IOException
{
if( writer != null )
{
writer.close();
}
}
private void close( COSDocument doc ) throws IOException
{
if( doc != null )
{
doc.close();
}
}
/**
* lookup the pages dictionary in a document
*
* todo: this method will go to the pdfmodel package one day
*
* @param doc the document where the pages dict is searched
*
* @return The document pages dictionary.
*/
public COSDictionary getPages(COSDocument doc)
{
COSDictionary trailer = doc.getTrailer();
COSDictionary root = (COSDictionary)trailer.getDictionaryObject( COSName.ROOT );
COSDictionary pages = (COSDictionary)root.getDictionaryObject( COSName.PAGES );
return pages;
}
/**
* This will concat two pdf documents
* <br />
* see usage() for commandline
*
* @param args command line arguments
*/
public static void main(String[] args)
{
AppendDoc app = new AppendDoc();
try
{
if( args.length != 3 )
{
app.usage();
}
else
{
app.doIt( args[0], args[1], args[2]);
}
}
catch( Exception e )
{
e.printStackTrace();
}
}
/**
* This will print out a message telling how to use this example.
*/
private void usage()
{
System.err.println( "usage: " + this.getClass().getName() + " <input-file1> <input-file2> <output-file>" );
}
}