How to combine two pdf files and save bookmarks and pdf / match using pdfbox?

The 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 a PDF merge:

  • Maintain PDF / A compliance if sourceDocument is compatible with PDF / A
  • Keep bookmarks if sourceDocument contains bookmarks
  • Save sourceDocument metadata (e.g. Title, Author, Subject, Keywords)

Please do not offer iText, I have already achieved this with iText, but due to licensing we need to use pdfbox. Also note that I did not write this code, this is from dspace. You can find the full code here: CitationDocument.java

+5
source share

Source: https://habr.com/ru/post/1206033/


All Articles