pdf/src/main/java/se/idsec/signservice/integration/document/pdf/utils/PDDocumentUtils.java
public static PDDocument insertDocument(final PDDocument document, final PDDocument insertDocument, final int page)
      throws DocumentProcessingException {
    try {
      final int documentNumberOfPages = document.getNumberOfPages();
      final int pagesToAdd = insertDocument.getNumberOfPages();
      final boolean append = page == 0 || page == documentNumberOfPages + 1;

      final Iterator<PDPage> it = insertDocument.getPages().iterator();
      while (it.hasNext()) {
        document.importPage(it.next());
      }

      if (!append) {
        final PDPageTree tree = document.getPages();
        final List<PDPage> newPages = new ArrayList<>();
        for (int i = 0; i < pagesToAdd; i++) {
          newPages.add(tree.get(documentNumberOfPages + i));
        }
        int pageCount = documentNumberOfPages + pagesToAdd;
        while (pageCount > documentNumberOfPages) {
          tree.remove(--pageCount);
        }
        int insertionPos = page - 1;
        for (PDPage newPage : newPages) {
          tree.insertBefore(newPage, tree.get(insertionPos));
          insertionPos++;
        }
      }
      
      return PDDocumentUtils.load(PDDocumentUtils.toBytes(document));
    }
    catch (IndexOutOfBoundsException | IllegalStateException | IllegalArgumentException e) {
      throw new DocumentProcessingException(new ErrorCode.Code("pdf"),
        String.format("Failed to insert sign page at page %d of document (no such page)", page), e);
    }
    catch (IOException e) {
      throw new DocumentProcessingException(new ErrorCode.Code("pdf"),
        String.format("Failed to insert sign page into document"), e);
    }
    finally {
      PDDocumentUtils.close(document);
    }
  }
/**
   * Inserts the {@code insertDocument} in {@code document} at position {@code page} (1-based). This means that the
   * given page number is the page number for the first page of the {@code insertDocument} after insertion.
   * 
   * @param document
   *          the document to be updated (will be closed)
   * @param insertDocument
   *          the document to insert
   * @param page
   *          the page (1-based) number where to insert, 0 means at the end of the file
   * @return the updated document
   * @throws DocumentProcessingException
   *           for errors
   */
Inserts the  insertDocument in  document at position  page (1-based). This means that the
given page number is the page number for the first page of the  insertDocument after insertion.


Inserts the insertDocument in document at position page ( 1 - based ) . This means that the given page number is the page number for the first page of the insertDocument after insertion .
public static PDDocument insertDocument(final PDDocument document, final PDDocument insertDocument, final int page)
      throws DocumentProcessingException {
    try {
      final int documentNumberOfPages = document.getNumberOfPages();
      final int pagesToAdd = insertDocument.getNumberOfPages();
      final boolean append = page == 0 || page == documentNumberOfPages + 1;
      final Iterator<PDPage> it = insertDocument.getPages().iterator();
      while (it.hasNext()) {
        document.importPage(it.next());
      }
      if (!append) {
        final PDPageTree tree = document.getPages();
        final List<PDPage> newPages = new ArrayList<>();
        for (int i = 0; i < pagesToAdd; i++) {
          newPages.add(tree.get(documentNumberOfPages + i));
        }
        int pageCount = documentNumberOfPages + pagesToAdd;
        while (pageCount > documentNumberOfPages) {
          tree.remove(--pageCount);
        }
        int insertionPos = page - 1;
        for (PDPage newPage : newPages) {
          tree.insertBefore(newPage, tree.get(insertionPos));
          insertionPos++;
        }
      }
      return PDDocumentUtils.load(PDDocumentUtils.toBytes(document));
    }
    catch (IndexOutOfBoundsException | IllegalStateException | IllegalArgumentException e) {
      throw new DocumentProcessingException(new ErrorCode.Code("pdf"),
        String.format("Failed to insert sign page at page %d of document (no such page)", page), e);
    }
    catch (IOException e) {
      throw new DocumentProcessingException(new ErrorCode.Code("pdf"),
        String.format("Failed to insert sign page into document"), e);
    }
    finally {
      PDDocumentUtils.close(document);
    }
  }
public static PDDocument insertDocument ( final PDDocument document , final PDDocument insertDocument , final int page ) throws DocumentProcessingException { try { final int documentNumberOfPages = document . getNumberOfPages ( ) ; final int pagesToAdd = insertDocument . getNumberOfPages ( ) ; final boolean append = page == 0 || page == documentNumberOfPages + 1 ; final Iterator < PDPage > it = insertDocument . getPages ( ) . iterator ( ) ; while ( it . hasNext ( ) ) { document . importPage ( it . next ( ) ) ; } if ( ! append ) { final PDPageTree tree = document . getPages ( ) ; final List < PDPage > newPages = new ArrayList < > ( ) ; for ( int i = 0 ; i < pagesToAdd ; i ++ ) { newPages . add ( tree . get ( documentNumberOfPages + i ) ) ; } int pageCount = documentNumberOfPages + pagesToAdd ; while ( pageCount > documentNumberOfPages ) { tree . remove ( -- pageCount ) ; } int insertionPos = page - 1 ; for ( PDPage newPage : newPages ) { tree . insertBefore ( newPage , tree . get ( insertionPos ) ) ; insertionPos ++ ; } } return PDDocumentUtils . load ( PDDocumentUtils . toBytes ( document ) ) ; } catch ( IndexOutOfBoundsException | IllegalStateException | IllegalArgumentException e ) { throw new DocumentProcessingException ( new ErrorCode . Code ( "pdf" ) , String . format ( "Failed to insert sign page at page %d of document (no such page)" , page ) , e ) ; } catch ( IOException e ) { throw new DocumentProcessingException ( new ErrorCode . Code ( "pdf" ) , String . format ( "Failed to insert sign page into document" ) , e ) ; } finally { PDDocumentUtils . close ( document ) ; } }
Inserts the {@code insertDocument} in {@code document} at position {@code page} (1-based).
Inserts the { @code insertDocument } in { @code document } at position { @code page } ( 1 - based ) .