package it.stefanochizzolini.clown.samples; import it.stefanochizzolini.clown.documents.Document; import it.stefanochizzolini.clown.files.File; import it.stefanochizzolini.clown.tools.PageManager; /** This sample demonstrates how to concatenate a document to another. @author Stefano Chizzolini (http://www.stefanochizzolini.it) @version 0.0.6 */ public class MergeSample implements ISample { public void run( PDFClownSampleLoader loader ) { // 1. Opening the PDF files... File sourceFile, targetFile; { // (boilerplate user choice -- ignore it) String sourceFilePath = loader.getPdfFileChoice("Please select source PDF file"); String targetFilePath = loader.getPdfFileChoice("Please select target PDF file"); // Source file. try{sourceFile = new File(sourceFilePath);} catch(Exception e){throw new RuntimeException(sourceFilePath + " file access error.",e);} // Target file. try{targetFile = new File(targetFilePath);} catch(Exception e){throw new RuntimeException(targetFilePath + " file access error.",e);} } Document targetDocument = targetFile.getDocument(); // 2. Append the source document's pages to the target document! new PageManager(targetDocument).add(sourceFile.getDocument()); // (boilerplate metadata insertion -- ignore it) loader.buildAccessories(targetDocument,this.getClass(),"Merge","concatenating two (or possibly more) documents"); // 3. Serialize the PDF file (again, boilerplate code -- see the PDFClownSampleLoader class source code)! loader.serialize(targetFile,this.getClass().getSimpleName()); } }