0

Im migrating my PDFBox 1.8.x to 2.0.12 and have to fight some changes.

The last one that I can't figure out is occuring in the code seen below.

    public static byte[] mergeDocuments(byte[] document1, byte[] document2) {
    try (PDDocument pdDocument1 = load(document1); PDDocument pdDocument2 = load(document2)) {
        final List<PDPage> pages1 = getPages(pdDocument1);
        final List<PDPage> pages2 = getPages(pdDocument2);
        pages1.addAll(pages2);
        return createDocument(pages1);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

private static List getPages(PDDocument pdDocumentTarget) {
    return pdDocumentTarget.getDocumentCatalog().getAllPages();
}

Error occurs on the last line, I have to change the old ".getAllPages()" to ".getPages", but then I get PDPageTree as return and not List.

Code is written some years ago and not by me. I have tried some things like casting or changing the types, but it allways results in errors in different places.

Thanks in advance for any help

mkl
  • 90,588
  • 15
  • 125
  • 265
AdrianL
  • 335
  • 2
  • 18

1 Answers1

2

PDPageTree implements Iterable<PDPage>, so you effectively need a way to generate a List for an Iterable.

This question illustrates many ways to do so, e.g. assuming Java 8:

private static List<PDPage> getPages(PDDocument pdDocumentTarget) {
    List<PDPage> result = new ArrayList<>();
    pdDocumentTarget.getPages().forEach(result::add);
    return result;
}
mkl
  • 90,588
  • 15
  • 125
  • 265