MergeSample.java :  » PDF » PDFClown-0.0.5 » it » stefanochizzolini » clown » samples » Java Open Source

Java Open Source » PDF » PDFClown 0.0.5 
PDFClown 0.0.5 » it » stefanochizzolini » clown » samples » MergeSample.java
package it.stefanochizzolini.clown.samples;

import it.stefanochizzolini.clown.documents.Document;
import it.stefanochizzolini.clown.documents.Page;
import it.stefanochizzolini.clown.documents.Pages;
import it.stefanochizzolini.clown.files.File;
import it.stefanochizzolini.clown.tokens.FileFormatException;

import java.util.Collection;

/**
  This sample demonstrates how to concatenate a document to another.
  <h3>Remarks</h3>
  <p>This implementation is based on the <b>contextual cloning</b> functionality
  of PDF Clown which allows to transparently and intuitively import contents from
  one document to another.</p>
*/
public class MergeSample
  implements ISample
{
  public void run(
    PDFClownSampleLoader loader
    )
  {
    // (boilerplate user choice -- ignore it)
    String sourceFilePath = loader.getPdfFileChoice("Please select source PDF file");
    String targetFilePath = loader.getPdfFileChoice("Please select target PDF file");

    // 1. Opening the PDF files...
    // Source file.
    File sourceFile;
    try{sourceFile = new File(sourceFilePath);}
    catch(FileFormatException e){throw new RuntimeException(sourceFilePath + " file has a bad file format.",e);}
    catch(Exception e){throw new RuntimeException(sourceFilePath + " file access error.",e);}
    // Target file.
    File targetFile;
    try{targetFile = new File(targetFilePath);}
    catch(FileFormatException e){throw new RuntimeException(targetFilePath + " file has a bad file format.",e);}
    catch(Exception e){throw new RuntimeException(targetFilePath + " file access error.",e);}

    // Get the PDF documents!
    Document sourceDocument = sourceFile.getDocument();
    Document targetDocument = targetFile.getDocument();
    Pages targetPages = targetDocument.getPages();

    // 2. Append the source document's pages to the target document!
    /*
      NOTE: To be added to an alien document,
      pages MUST be firstly contextualized into it,
      then added to the target pages collection.
    */
    targetPages.addAll(
      (Collection<Page>)targetDocument.contextualize(
        (Collection<Page>)sourceDocument.getPages()
        )
      );
    targetPages.update(); // NOTE: Update is fundamental to override original page collection.

    // (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());
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.