### Question:
PdfManipulator { @Programmatic public byte[] extractAndStamp( final byte[] docBytes, final ExtractSpec extractSpec, final Stamp stamp) throws IOException { List<byte[]> extractedPageDocBytes = Lists.newArrayList(); final PDDocument pdDoc = PDDocument.load(docBytes); try { final Splitter splitter = new Splitter(); final List<PDDocument> splitDocs = splitter.split(pdDoc); final int sizeOfDoc = splitDocs.size(); final Integer[] pageNums = extractSpec.pageNumbersFor(sizeOfDoc); for (Integer pageNum : pageNums) { final PDDocument docOfExtractedPage = splitDocs.get(pageNum); if(stamp != null) { final List<Line> leftLines = stamp.getLeftLines(); final List<Line> rightLines = stamp.getRightLines(); leftLines.add(new Line(String.format("Page: %d/%d", (pageNum+1), sizeOfDoc), TEXT_COLOR, null)); stamp.appendHyperlinkIfAnyTo(leftLines); extractedPageDocBytes.add(stamp(docOfExtractedPage, leftLines, rightLines)); } else { extractedPageDocBytes.add(asBytes(docOfExtractedPage)); } } for (PDDocument splitDoc : splitDocs) { splitDoc.close(); } } finally { pdDoc.close(); } final byte[] mergedBytes = pdfBoxService.merge(extractedPageDocBytes.toArray(new byte[][] {})); return mergedBytes; }  @Programmatic byte[] stamp(
            final byte[] docBytes,
            final Stamp stamp); @Programmatic byte[] extract(
            final byte[] docBytes,
            final ExtractSpec extractSpec); @Programmatic byte[] extractAndStamp(
            final byte[] docBytes,
            final ExtractSpec extractSpec,
            final Stamp stamp);  }### Answer:
@Ignore @Test public void firstPageOf() throws Exception { URL resource = Resources.getResource(PdfManipulatorTest.class, "sample-invoice.pdf"); byte[] bytes = Resources.toByteArray(resource); Stamp stamp = new Stamp(Arrays.asList( "approved by: Joe Bloggs", "approved on: 3-May-2017 14:15", "doc barcode: 3013011234" ), Arrays.asList( "debtor IBAN: FR12345678900000123", "crdtor IBAN: FR99999912312399800", "gross amt : 12345.99" ), "http: final PdfManipulator pdfManipulator = new PdfManipulator(); pdfManipulator.pdfBoxService = new PdfBoxService(); byte[] firstPageBytes = pdfManipulator.extractAndStamp(bytes, new ExtractSpec(3,1), stamp); IOUtils.copy(new ByteArrayInputStream(firstPageBytes), new FileOutputStream("x.pdf")); }