public List<byte[]> split(byte[] input) throws IOException, DocumentException { PdfReader pdfReader = new PdfReader(input); List<byte[]> pdfFiles = new ArrayList<>(); int pageCount = pdfReader.getNumberOfPages(); int pageIndex = 0; while (++pageIndex <= pageCount) { Document document = new Document(pdfReader.getPageSizeWithRotation(pageIndex)); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); PdfCopy pdfCopy = new PdfSmartCopy(document, byteArrayOutputStream); pdfCopy.setFullCompression(); PdfImportedPage pdfImportedPage = pdfCopy.getImportedPage(pdfReader, pageIndex); document.open(); pdfCopy.addPage(pdfImportedPage); document.close(); pdfCopy.close(); pdfFiles.add(byteArrayOutputStream.toByteArray()); } return pdfFiles; }
private static File copyToMultiplePages(CropJob cropJob) throws IOException, DocumentException { PdfReader reader = new PdfReader(cropJob.getSource().getAbsolutePath()); Document document = new Document(); File resultFile = File.createTempFile("cropped", ".pdf"); PdfSmartCopy pdfCopy = new PdfSmartCopy(document, new FileOutputStream(resultFile)); document.open(); PdfImportedPage page; for (int pageNumber = 1; pageNumber <= cropJob.getSourcePageCount(); pageNumber++) { SingleCluster currentCluster = cropJob.getClusterCollection().getSingleCluster(pageNumber); page = pdfCopy.getImportedPage(reader, pageNumber); pdfCopy.addPage(page); for (int j = 1; j < currentCluster.getRatiosList().size(); j++) { pdfCopy.addPage(page); } } document.close(); pdfCopy.close(); reader.close(); return resultFile; }
public static byte[] Merge(File[] documentPaths) throws IOException, DocumentException { byte[] mergedDocument; try (ByteArrayOutputStream memoryStream = new ByteArrayOutputStream()) { Document document = new Document(); PdfSmartCopy pdfSmartCopy = new PdfSmartCopy(document, memoryStream); document.open(); for (File docPath : documentPaths) { PdfReader reader = new PdfReader(docPath.toString()); try { reader.consolidateNamedDestinations(); int numberOfPages = reader.getNumberOfPages(); for (int page = 0; page < numberOfPages;) { PdfImportedPage pdfImportedPage = pdfSmartCopy.getImportedPage(reader, ++page); pdfSmartCopy.addPage(pdfImportedPage); } } finally { reader.close(); } } document.close(); mergedDocument = memoryStream.toByteArray(); } return mergedDocument; }
/** * initialize the copier using the given reader and the given output version. * * @param reader * @param outputStream * the output stream to write to. * @param version * version for the created pdf copy, if null the version number is taken from the input {@link PdfReader} */ void init(PdfReader reader, OutputStream outputStream, PdfVersion version) throws TaskException { try { pdfDocument = new Document(reader.getPageSizeWithRotation(1)); pdfCopy = new PdfSmartCopy(pdfDocument, outputStream); if (version == null) { pdfCopy.setPdfVersion(reader.getPdfVersion()); } else { pdfCopy.setPdfVersion(version.getVersionAsCharacter()); } pdfDocument.addCreator(Sejda.CREATOR); } catch (DocumentException e) { throw new TaskException("An error occurred opening the PdfSmartCopy.", e); } }
private static File copyToMultiplePages(final CropDefinition cropDefinition, final PdfMetaInformation pdfMetaInformation) throws IOException, DocumentException { PdfReader reader = new PdfReader(cropDefinition.getSourceFile().getAbsolutePath()); HashMap<String, String> map = SimpleNamedDestination.getNamedDestination(reader, false); Document document = new Document(); File resultFile = File.createTempFile("cropped", ".pdf"); PdfSmartCopy pdfCopy = new PdfSmartCopy(document, new FileOutputStream(resultFile)); document.open(); Map<Integer, List<String>> pageNrToDestinations = new HashMap<Integer, List<String>>(); for (String single : map.keySet()) { StringTokenizer st = new StringTokenizer(map.get(single), " "); if (st.hasMoreElements()) { String pageNrString = (String) st.nextElement(); int pageNr = Integer.parseInt(pageNrString); List<String> singleList = (pageNrToDestinations.get(pageNr)); if (singleList == null) { singleList = new ArrayList<String>(); singleList.add(single); pageNrToDestinations.put(pageNr, singleList); } else { singleList.add(single); } } } int outputPageNumber = 0; for (int pageNumber = 1; pageNumber <= pdfMetaInformation.getSourcePageCount(); pageNumber++) { PdfImportedPage pdfPage = pdfCopy.getImportedPage(reader, pageNumber); pdfCopy.addPage(pdfPage); outputPageNumber++; List<String> destinations = pageNrToDestinations.get(pageNumber); if (destinations != null) { for (String destination : destinations) pdfCopy.addNamedDestination(destination, outputPageNumber, new PdfDestination(PdfDestination.FIT)); } List<Float[]> rectangles = cropDefinition.getRectanglesForPage(pageNumber); for (int j = 1; j < rectangles.size(); j++) { pdfCopy.addPage(pdfPage); outputPageNumber++; } } document.close(); pdfCopy.close(); reader.close(); return resultFile; }