List of usage examples for com.lowagie.text.pdf PdfSmartCopy addPage
public void addPage(PdfImportedPage iPage) throws IOException, BadPdfFormatException
From source file:classroom.filmfestival_c.Movies24.java
@SuppressWarnings("unchecked") public static void main(String[] args) { Session session = null;/*from w ww.j a va2 s. co m*/ try { session = (Session) MySessionFactory.currentSession(); Query q = session.createQuery("from FilmTitle order by title"); List<FilmTitle> results = q.list(); Document document = new Document(); PdfSmartCopy copy = new PdfSmartCopy(document, new FileOutputStream(RESULT)); document.open(); PdfReader reader; PdfStamper stamper; AcroFields form; ByteArrayOutputStream baos; for (FilmTitle movie : results) { baos = new ByteArrayOutputStream(); reader = new PdfReader(DATASHEET); stamper = new PdfStamper(reader, baos); form = stamper.getAcroFields(); form.setField("title", movie.getTitle()); form.setField("director", getDirectors(movie)); form.setField("year", String.valueOf(movie.getYear())); form.setField("duration", String.valueOf(movie.getDuration())); form.setField("category", "c" + getCategory(movie)); Set<FestivalScreening> screenings = (Set<FestivalScreening>) movie.getFestivalScreenings(); for (FestivalScreening screening : screenings) { form.setField(screening.getId().getPlace(), "Yes"); } stamper.setFormFlattening(true); stamper.close(); reader = new PdfReader(baos.toByteArray()); copy.addPage(copy.getImportedPage(reader, 1)); } document.close(); } catch (HibernateException e) { LOGGER.warn("HibernateException: " + e); } catch (IOException e) { LOGGER.warn("IOException: " + e); } catch (DocumentException e) { LOGGER.warn("DocumentException: " + e); } finally { try { if (session != null) { session.close(); } } catch (HibernateException e) { LOGGER.warn("HibernateTest - Closing session: " + e); } } }
From source file:classroom.filmfestival_c.Movies25.java
@SuppressWarnings("unchecked") public static void main(String[] args) { createTemplate();//www . j a va 2s .c om Session session = (Session) MySessionFactory.currentSession(); Query q = session.createQuery("from FilmTitle order by title"); java.util.List<FilmTitle> results = q.list(); try { Document document = new Document(); PdfSmartCopy copy = new PdfSmartCopy(document, new FileOutputStream(RESULT)); document.open(); PdfReader reader; PdfStamper stamper = null; ByteArrayOutputStream baos = null; AcroFields form = null; int count = 0; for (FilmTitle movie : results) { if (count == 0) { baos = new ByteArrayOutputStream(); reader = new PdfReader(BACKGROUND); stamper = new PdfStamper(reader, baos); stamper.setFormFlattening(true); form = stamper.getAcroFields(); } count++; byte[] pdf = createPdf(movie); reader = new PdfReader(pdf); PdfImportedPage page = stamper.getImportedPage(reader, 1); PushbuttonField bt = form.getNewPushbuttonFromField("movie_" + count); bt.setLayout(PushbuttonField.LAYOUT_ICON_ONLY); bt.setProportionalIcon(true); bt.setTemplate(page); form.replacePushbuttonField("movie_" + count, bt.getField()); if (count == 16) { stamper.close(); reader = new PdfReader(baos.toByteArray()); copy.addPage(copy.getImportedPage(reader, 1)); count = 0; } } if (count > 0) { stamper.close(); reader = new PdfReader(baos.toByteArray()); copy.addPage(copy.getImportedPage(reader, 1)); count = 0; } document.close(); } catch (IOException ioe) { LOGGER.error("IOException: ", ioe); } catch (DocumentException de) { LOGGER.error("DocumentException: ", de); } }
From source file:com.openkm.util.PDFUtils.java
License:Open Source License
/** * Merge several PDFs into a new one/*from w w w . ja va 2 s .c om*/ */ public static void merge(List<InputStream> inputs, OutputStream output) throws IOException, DocumentException { Document document = new Document(); try { PdfSmartCopy copy = new PdfSmartCopy(document, output); document.open(); for (InputStream is : inputs) { PdfReader reader = new PdfReader(is); for (int i = 1; i <= reader.getNumberOfPages(); i++) { copy.addPage(copy.getImportedPage(reader, i)); } } output.flush(); document.close(); } finally { IOUtils.closeQuietly(output); } }
From source file:de.offis.health.icardea.cied.pdf.extractor.PDFiText2Extractor.java
License:LGPL
public byte[] getPDFPages(int fromPageNumber, int toPageNumber) { ByteArrayOutputStream byteArrayOutputStream = null; boolean extractionSuccessful = false; if (pdfReader != null) { int numberOfPages = getNumberOfPages(); /*/*w ww . j a v a 2 s . com*/ * 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; }
From source file:org.efaps.esjp.common.file.FileUtil_Base.java
License:Apache License
/** * @param _files pdfs to be combined into one file * @param _fileName name of the file to be generated * @param _paginate paginat or not/*from w w w. j ava2s. c o m*/ * @return file * @throws EFapsException on error */ public File combinePdfs(final List<File> _files, final String _fileName, final boolean _paginate) throws EFapsException { File ret = null; if (_files.size() == 1) { ret = _files.get(0); } else { try { final List<InputStream> pdfs = new ArrayList<>(); for (final File file : _files) { pdfs.add(new FileInputStream(file)); } ret = getFile(_fileName, "pdf"); final OutputStream outputStream = new FileOutputStream(ret); final Document document = new Document(); try { final List<PdfReader> readers = new ArrayList<>(); int totalPages = 0; final Iterator<InputStream> iteratorPDFs = pdfs.iterator(); // Create Readers for the pdfs. while (iteratorPDFs.hasNext()) { final InputStream pdf = iteratorPDFs.next(); final PdfReader pdfReader = new PdfReader(pdf); readers.add(pdfReader); totalPages += pdfReader.getNumberOfPages(); } final PdfSmartCopy copy = new PdfSmartCopy(document, outputStream); final Iterator<PdfReader> iteratorPDFReader = readers.iterator(); document.open(); while (iteratorPDFReader.hasNext()) { final PdfReader pdfReader = iteratorPDFReader.next(); for (int i = 0; i < pdfReader.getNumberOfPages(); i++) { final PdfImportedPage importedPage = copy.getImportedPage(pdfReader, i + 1); copy.addPage(importedPage); if (_paginate) { LOG.debug("Missing page ", totalPages); } } } outputStream.flush(); document.close(); outputStream.close(); // CHECKSTYLE:OFF } catch (final Exception e) { // CHECKSTYLE:ON e.printStackTrace(); } finally { if (document.isOpen()) { document.close(); } try { if (outputStream != null) { outputStream.close(); } } catch (final IOException ioe) { ioe.printStackTrace(); } } } catch (final FileNotFoundException e) { LOG.error("FileNotFoundException", e); } } return ret; }