### Question:
PdfServiceImpl implements PdfService { @Override public InputStream extractPages(InputStream is, String filename, List<Integer> pageNumbers) throws PdfServiceException { try (PDDocument extractedDocument = new PDDocument(); PDDocument sourceDocument = PDDocument.load(is)) { extractedDocument.setDocumentInformation(sourceDocument.getDocumentInformation()); extractedDocument.getDocumentCatalog().setViewerPreferences(sourceDocument.getDocumentCatalog().getViewerPreferences()); for (Integer pageNumber : pageNumbers) { PDPage page = sourceDocument.getPage(pageNumber - 1); PDPage imported = extractedDocument.importPage(page); imported.setCropBox(page.getCropBox()); imported.setMediaBox(page.getMediaBox()); imported.setResources(page.getResources()); imported.setRotation(page.getRotation()); } log.debug("Successfully extracted pages from [{}]", filename); ByteArrayOutputStream baos = new ByteArrayOutputStream(); extractedDocument.save(baos); return new ByteArrayInputStream(baos.toByteArray()); } catch (IOException | IndexOutOfBoundsException e) { log.error("Unable to extract pages from [{}]", filename, e); throw new PdfServiceException(e); } }  @Override String generatePdf(File xslFile, Source source); @Override String generatePdf(File xslFile, Source source, Map<String, String> parameters); @Override String generatePdf(String xslFilename, Source source, Map<String, String> parameters); @Override String generatePdf(File xslFile, Map<String, String> parameters); @Override String generatePdf(String xslFilename, Map<String, String> parameters); @Override void generatePdf(File xslFile, Source source, OutputStream targetStream, Map<String, String> parameters); @Deprecated @Override PDDocument append(PDDocument pdDocument, InputStream is, PDFMergerUtility pdfMergerUtility); @Deprecated @Override PDDocument append(PDDocument pdDocument, String filename, PDFMergerUtility pdfMergerUtility); @Override void generateTiffFromPdf(File inputPdf, File outputTiff); @Override void addSource(PDFMergerUtility pdfMergerUtility, InputStream is); @Override void addSource(PDFMergerUtility pdfMergerUtility, String filename); @Override void mergeSources(PDFMergerUtility pdfMergerUtility, String filename); @Override InputStream extractPages(InputStream is, String filename, List<Integer> pageNumbers); static final long MEGABYTE; static final int MAX_MAIN_MEMORY_BYTES; }### Answer:
@Test public void extractPages() throws Exception { String filename = "multipage_document.pdf"; List<Integer> pageNumbers = Arrays.asList(1, 2, 4); assertNotNull(pdfService); FileSystemResource sourcePdf = new FileSystemResource(this.getClass().getResource("/pdfs/" + filename).getFile()); assertTrue(sourcePdf.exists()); InputStream sourceDocInputStream = sourcePdf.getInputStream(); InputStream extractedDocInputStream = pdfService.extractPages(sourceDocInputStream, filename, pageNumbers); PDDocument extractedDoc = PDDocument.load(extractedDocInputStream); int extractedPageCount = extractedDoc.getNumberOfPages(); log.debug("Number of pages in extracted document [{}]", extractedPageCount); extractedDoc.close(); extractedDocInputStream.close(); assertEquals(extractedPageCount, pageNumbers.size()); }
@Test(expected = PdfServiceException.class) public void failPageExtraction() throws Exception { String filename = "multipage_document.pdf"; List<Integer> pageNumbers = Arrays.asList(1, 2, 9); assertNotNull(pdfService); FileSystemResource sourcePdf = new FileSystemResource(this.getClass().getResource("/pdfs/" + filename).getFile()); assertTrue(sourcePdf.exists()); InputStream sourceDocInputStream = sourcePdf.getInputStream(); pdfService.extractPages(sourceDocInputStream, filename, pageNumbers); }