Example usage for com.itextpdf.text.pdf PdfConcatenate addPages

List of usage examples for com.itextpdf.text.pdf PdfConcatenate addPages

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfConcatenate addPages.

Prototype

public int addPages(PdfReader reader) throws DocumentException, IOException 

Source Link

Document

Adds the pages from an existing PDF document.

Usage

From source file:org.dspace.disseminate.CitationDocument.java

/**
 * Creates a//from  ww w . ja  va  2s . com
 * cited document from the given bitstream of the given item. This
 * requires that bitstream is contained in item.
 * <p>
 * The Process for adding a cover page is as follows:
 * <ol>
 *  <li> Load source file into PdfReader and create a
 *     Document to put our cover page into.</li>
 *  <li> Create cover page and add content to it.</li>
 *  <li> Concatenate the coverpage and the source
 *     document.</li>
 * </p>
 *
 * @param bitstream The source bitstream being cited. This must be a PDF.
 * @param cMeta The citation information used to generate the coverpage.
 * @return The temporary File that is the finished, cited document.
 * @throws com.itextpdf.text.DocumentException
 * @throws java.io.FileNotFoundException
 * @throws SQLException
 * @throws org.dspace.authorize.AuthorizeException
 */
private File makeCitedDocument(Bitstream bitstream, CitationMeta cMeta)
        throws DocumentException, IOException, SQLException, AuthorizeException {
    //Read the source bitstream
    PdfReader source = new PdfReader(bitstream.retrieve());

    Document citedDoc = new Document(PageSize.LETTER);

    File coverTemp = File.createTempFile(bitstream.getName(), ".cover.pdf");

    //Need a writer instance to make changed to the document.
    PdfWriter writer = PdfWriter.getInstance(citedDoc, new FileOutputStream(coverTemp));

    //Call helper function to add content to the coverpage.
    this.generateCoverPage(citedDoc, writer, cMeta);

    //Create reader from finished cover page.
    PdfReader cover = new PdfReader(new FileInputStream(coverTemp));

    //Get page labels from source document
    String[] labels = PdfPageLabels.getPageLabels(source);

    //Concatenate the finished cover page with the source document.
    File citedTemp = File.createTempFile(bitstream.getName(), ".cited.pdf");
    OutputStream citedOut = new FileOutputStream(citedTemp);
    PdfConcatenate concat = new PdfConcatenate(citedOut);
    concat.open();

    //Is the citation-page the first page or last-page?
    if (isCitationFirstPage()) {
        //citation as cover page
        concat.addPages(cover);
        concat.addPages(source);
    } else {
        //citation as tail page
        concat.addPages(source);
        concat.addPages(cover);
    }

    //Put all of our labels in from the orignal document.
    if (labels != null) {
        PdfPageLabels citedPageLabels = new PdfPageLabels();
        log.debug("Printing arbitrary page labels.");

        for (int i = 0; i < labels.length; i++) {
            citedPageLabels.addPageLabel(i + 1, PdfPageLabels.EMPTY, labels[i]);
            log.debug("Label for page: " + (i + 1) + " -> " + labels[i]);
        }
        citedPageLabels.addPageLabel(labels.length + 1, PdfPageLabels.EMPTY, "Citation Page");
        concat.getWriter().setPageLabels(citedPageLabels);
    }

    //Close it up
    concat.close();

    //Close the cover-page
    writer.close();
    coverTemp.delete();

    citedTemp.deleteOnExit();
    return citedTemp;
}