Search in sources :

Example 1 with PdfCopy

use of com.itextpdf.text.pdf.PdfCopy in project ASCIIGenome by dariober.

the class Pdf method appendPdf.

/**
 * Append pdf file `in` to pdf file `dest`
 * @throws IOException
 * @throws DocumentException
 */
private void appendPdf(File in, File dest) throws IOException, DocumentException {
    if (!dest.exists()) {
        Files.move(Paths.get(in.getAbsolutePath()), Paths.get(dest.getAbsolutePath()));
        return;
    }
    File template = Utils.createTempFile(".template.", ".pdf");
    ;
    template.deleteOnExit();
    Files.copy(Paths.get(dest.getAbsolutePath()), Paths.get(template.getAbsolutePath()), StandardCopyOption.REPLACE_EXISTING);
    Document document = new Document();
    FileOutputStream outputStream = new FileOutputStream(template);
    PdfCopy copy = new PdfSmartCopy(document, outputStream);
    document.open();
    PdfReader reader0 = new PdfReader(dest.getAbsolutePath());
    copy.addDocument(reader0);
    reader0.close();
    PdfReader reader = new PdfReader(in.getAbsolutePath());
    copy.addDocument(reader);
    reader.close();
    document.close();
    Files.move(Paths.get(template.getAbsolutePath()), Paths.get(dest.getAbsolutePath()), StandardCopyOption.REPLACE_EXISTING);
}
Also used : PdfCopy(com.itextpdf.text.pdf.PdfCopy) FileOutputStream(java.io.FileOutputStream) PdfSmartCopy(com.itextpdf.text.pdf.PdfSmartCopy) PdfReader(com.itextpdf.text.pdf.PdfReader) Document(com.itextpdf.text.Document) File(java.io.File)

Aggregations

Document (com.itextpdf.text.Document)1 PdfCopy (com.itextpdf.text.pdf.PdfCopy)1 PdfReader (com.itextpdf.text.pdf.PdfReader)1 PdfSmartCopy (com.itextpdf.text.pdf.PdfSmartCopy)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1