Example usage for org.apache.pdfbox.multipdf PDFMergerUtility appendDocument

List of usage examples for org.apache.pdfbox.multipdf PDFMergerUtility appendDocument

Introduction

In this page you can find the example usage for org.apache.pdfbox.multipdf PDFMergerUtility appendDocument.

Prototype

public void appendDocument(PDDocument destination, PDDocument source) throws IOException 

Source Link

Document

append all pages from source to destination.

Usage

From source file:ch.dowa.jassturnier.pdf.PdfGenerator.java

public void exportTemplateWithTableMultiPage(HashMap<String, Table.TableBuilder> tableBuildersMap,
        String fileName) throws IOException {
    String vLogoPath = !ResourceLoader.readProperty("LOGOPATH").isEmpty()
            ? ResourceLoader.readProperty("LOGOPATH")
            : null;//from w w  w . java 2  s. co  m
    PDDocument mainDoc = new PDDocument();
    PDFMergerUtility merger = new PDFMergerUtility();
    for (Map.Entry<String, TableBuilder> entry : tableBuildersMap.entrySet()) {
        String title = entry.getKey();
        TableBuilder tableBuilder = entry.getValue();

        final PDDocument documentPage = new PDDocument();
        boolean firstPage = true;
        PDImageXObject image = null;
        float imageStartX = 0F;
        float imageStartY = 0F;
        PDRectangle imgSize = null;

        TableDrawer drawer = TableDrawer.builder().table(tableBuilder.build()).startX(docContentStartX())
                .startY(docContentStartY()).endY(documentPadding).build();

        if (vLogoPath != null) {
            image = PDImageXObject.createFromFile(vLogoPath, documentPage);
            imgSize = getImageSizePdf(image);
            imageStartX = imgEndX() - imgSize.getWidth();
            imageStartY = imgEndY() - imgSize.getHeight();
        }

        do {
            PDPage page = new PDPage(pageFormat);
            documentPage.addPage(page);
            try (PDPageContentStream contentStream = new PDPageContentStream(documentPage, page)) {
                if (firstPage) {
                    contentStream.beginText();
                    contentStream.setFont(STANDART_FONT_BOLD, 14);
                    contentStream.newLineAtOffset(docTitelStartX(), docTitelStartY());
                    contentStream.showText(title);
                    contentStream.endText();
                    if (image != null) {
                        contentStream.drawImage(image, imageStartX, imageStartY, imgSize.getWidth(),
                                imgSize.getHeight());
                    }
                    drawer.startY(docContentSartFirsPageY());
                    firstPage = false;
                }
                drawer.contentStream(contentStream).draw();
            }
            drawer.startY(docContentStartY());
        } while (!drawer.isFinished());

        merger.appendDocument(mainDoc, documentPage);
    }

    mainDoc.save(fileName);
    mainDoc.close();
}

From source file:src.controller.DocumentController.java

/**
 * Ajoute un PDF au document spcifi//from   w w w  .ja  v  a2  s . com
 *
 * @param document
 * @param file
 * @throws IOException
 */
public void addPDFToDocument(PDDocument document, File file) throws IOException {
    PDDocument doc = null;
    try {
        if (document != null && file.exists()) {
            doc = PDDocument.load(file);
            if (doc != null) {
                PDFMergerUtility merger = new PDFMergerUtility();
                merger.appendDocument(document, doc);
            }
        }
    } catch (IOException e) {
        System.out.println(e.toString());
    } finally {
        if (doc != null) {
            doc.close();
        }
    }
}