5

Code:

    /**
 * Creates a
 * 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.
 * @return The temporary File that is the finished, cited document.
 * @throws java.io.FileNotFoundException
 * @throws SQLException
 * @throws org.dspace.authorize.AuthorizeException
 */
public File makeCitedDocument(Bitstream bitstream)
        throws IOException, SQLException, AuthorizeException, COSVisitorException {
    PDDocument document = new PDDocument();
    PDDocument sourceDocument = new PDDocument();
    try {
        Item item = (Item) bitstream.getParentObject();
        sourceDocument = sourceDocument.load(bitstream.retrieve());
        PDPage coverPage = new PDPage(PDPage.PAGE_SIZE_LETTER);
        generateCoverPage(document, coverPage, item);
        addCoverPageToDocument(document, sourceDocument, coverPage);

        document.save(tempDir.getAbsolutePath() + "/bitstream.cover.pdf");
        return new File(tempDir.getAbsolutePath() + "/bitstream.cover.pdf");
    } finally {
        sourceDocument.close();
        document.close();
    }
}

What I'm trying to achieve after pdf merge:

  • Retain PDF/A compliance if sourceDocument is PDF/A compliant
  • Retain bookmarks if sourceDocument contains bookmarks
  • Retain the metadata of sourceDocument (eg Title, Author, Subject, Keywords)

Please don't suggest iText, I have already achieved this using iText but due to licensing, we need to use pdfbox instead. Please also note that I did not write these code, this is from dspace. You can find the complete code here: CitationDocument.java

euler
  • 1,401
  • 2
  • 18
  • 39
  • 1
    DSpace 5.x uses [PDFBox 1.8.7](https://github.com/DSpace/DSpace/blob/dspace-5_x/pom.xml#L1060). Have you seen the PDFBox 1.8 guides for [creating PDF/A documents](https://pdfbox.apache.org/1.8/cookbook/pdfacreation.html) and [managing PDF metadata](https://pdfbox.apache.org/1.8/cookbook/workingwithmetadata.html)? There's also a separate StackOverflow question on [creating bookmarks in PDFBox](http://stackoverflow.com/questions/24954281/is-there-a-way-to-create-bookmarks-for-pdf-documents-in-pdfbox). – Tim Donohue May 11 '15 at 17:36

0 Answers0