List of usage examples for com.itextpdf.text.pdf PdfSmartCopy close
@Override public void close()
Document
was closed and that no other Elements
will be added. From source file:at.laborg.briss.CropManager.java
License:Open Source License
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();/*w w w. jav a 2s . com*/ 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; }
From source file:at.laborg.briss.utils.DocumentCropper.java
License:Open Source License
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();// ww w .j a va 2s.c o m 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; }
From source file:com.preselect.pdfservice.tasks.PdfConversionTask.java
License:Open Source License
private static void copyDocument(PdfReader reader, int start, int end, String path, OutlineItems outline) throws IOException, DocumentException { Document document = new Document(); PdfSmartCopy copy = new PdfSmartCopy(document, new FileOutputStream(path)); document.open();/* ww w . j av a 2 s .com*/ for (int i = (start - 1); i <= (end - 1);) { copy.addPage(copy.getImportedPage(reader, ++i)); } List<OutlineItem> outlineForChapter = getOutlineBetweenPages(outline, start, end); Iterator<OutlineItem> iterator = outlineForChapter.iterator(); if (iterator.hasNext()) { List<HashMap<String, Object>> bookmarksForChapter = getBookmarks(iterator.next(), iterator, 1); SimpleBookmark.shiftPageNumbers(bookmarksForChapter, (-start + 1), null); copy.setOutlines(bookmarksForChapter); } if (outlineForChapter.size() > 0) { OutlineItem firstOutline = outlineForChapter.get(0); document.addTitle(firstOutline.getTitle()); } document.addCreator("Content Select"); document.close(); copy.close(); }
From source file:de.offis.health.icardea.cied.pdf.extractor.PDFiText5Extractor.java
License:GNU General Public License
public byte[] getPDFPages(int fromPageNumber, int toPageNumber) { ByteArrayOutputStream byteArrayOutputStream = null; boolean extractionSuccessful = false; if (pdfReader != null) { int numberOfPages = getNumberOfPages(); /*/*from ww w . ja v a 2s . c om*/ * Check if the given page numbers are in the allowed range. */ if (fromPageNumber > 0 && fromPageNumber <= numberOfPages && toPageNumber > 0 && toPageNumber <= numberOfPages) { /* * Now check if the given fromPageNumber is smaller * as the given toPageNumber. If not swap the numbers. */ if (fromPageNumber > toPageNumber) { int tmpPageNumber = toPageNumber; toPageNumber = fromPageNumber; fromPageNumber = tmpPageNumber; } Document newDocument = new Document(); try { byteArrayOutputStream = new ByteArrayOutputStream(); PdfSmartCopy pdfCopy = new PdfSmartCopy(newDocument, byteArrayOutputStream); newDocument.open(); for (int currentPage = fromPageNumber; currentPage <= toPageNumber; currentPage++) { pdfCopy.addPage(pdfCopy.getImportedPage(pdfReader, currentPage)); } // end for pdfCopy.flush(); pdfCopy.close(); newDocument.close(); extractionSuccessful = true; } catch (DocumentException ex) { // TODO: Create an own exception for PDF processing errors. logger.error("An exception occurred while extracting " + "pages from the input PDF file.", ex); } catch (IOException ex) { // TODO: Create an own exception for PDF processing errors. logger.error("An exception occurred while extracting " + "pages from the input PDF file.", ex); } finally { if (!extractionSuccessful) { byteArrayOutputStream = null; } } // end try..catch..finally } // end if checking range of given pages } // end if (pdfReader != null) if (byteArrayOutputStream != null) { return byteArrayOutputStream.toByteArray(); } return null; }