List of usage examples for com.lowagie.text.pdf PdfWriter getDirectContent
public PdfContentByte getDirectContent()
From source file:org.eclipse.examples.slideshow.runner.PDFGraphics.java
License:Open Source License
public PDFGraphics(Document document, PdfWriter writer) throws DocumentException { this.document = document; this.writer = writer; contentByte = writer.getDirectContent(); //contentByte.concatCTM(1f, 0f, 0f, -1f, 0f, document.getPageSize().height()); //contentByte.concatCTM(0.5f, 0f, 0f, 0.5f, 0f, 200f); // Scale and shift up stateStack.push(new State()); }
From source file:org.efaps.esjp.common.file.FileUtil_Base.java
License:Apache License
/** * Resize.//www.j av a2 s . co m * * @param _parameter Parameter as passed by the eFaps API * @param _file the file * @param _fileName the file name * @param _pageSize the page size * @return the file * @throws EFapsException on error */ public File resizePdf(final Parameter _parameter, final File _file, final String _fileName, final String _pageSize) throws EFapsException { final Document document = new Document(); final File ret = getFile(_fileName, "pdf"); try { final File destFile = new File(_file.getPath() + ".tmp"); FileUtils.copyFile(_file, destFile); final OutputStream outputStream = new FileOutputStream(ret); final PdfReader pdfReader = new PdfReader(new FileInputStream(destFile)); // Create a writer for the outputstream final PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); PdfImportedPage page; final PdfContentByte cb = writer.getDirectContent(); int pageOfCurrentReaderPDF = 0; // Create a new page in the target for each source page. while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) { document.newPage(); pageOfCurrentReaderPDF++; page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF); document.setPageSize(page.getWidth() <= page.getHeight() ? PageSize.getRectangle(_pageSize) : PageSize.getRectangle(_pageSize).rotate()); final float widthFactor = document.getPageSize().getWidth() / page.getWidth(); final float heightFactor = document.getPageSize().getHeight() / page.getHeight(); final float factor = Math.min(widthFactor, heightFactor); final float offsetX = (document.getPageSize().getWidth() - page.getWidth() * factor) / 2; final float offsetY = (document.getPageSize().getHeight() - page.getHeight() * factor) / 2; cb.addTemplate(page, factor, 0, 0, factor, offsetX, offsetY); } pageOfCurrentReaderPDF = 0; outputStream.flush(); document.close(); outputStream.close(); } catch (final FileNotFoundException e) { LOG.error("FileNotFoundException", e); } catch (final IOException e) { LOG.error("IOException", e); } catch (final DocumentException e) { LOG.error("DocumentException", e); } return ret; }
From source file:org.efaps.esjp.common.file.FileUtil_Base.java
License:Apache License
/** * N up.//from w w w . j a v a 2s .c o m * * @param _parameter Parameter as passed by the eFaps API * @param _file the file * @param _fileName the file name * @return the file * @throws EFapsException on error */ public File nUpPdf(final Parameter _parameter, final File _file, final String _fileName) throws EFapsException { final File ret = getFile(_fileName, "pdf"); try { final int pow = Integer.parseInt(getProperty(_parameter, "NUpPow", "1")); final boolean duplicate = "true".equalsIgnoreCase(getProperty(_parameter, "NUpDuplicate", "true")); final File destFile = new File(_file.getPath() + ".tmp"); FileUtils.copyFile(_file, destFile); final OutputStream outputStream = new FileOutputStream(ret); final PdfReader pdfReader = new PdfReader(new FileInputStream(destFile)); final Rectangle pageSize = pdfReader.getPageSize(1); final Rectangle newSize = pow % 2 == 0 ? new Rectangle(pageSize.getWidth(), pageSize.getHeight()) : new Rectangle(pageSize.getHeight(), pageSize.getWidth()); Rectangle unitSize = new Rectangle(pageSize.getWidth(), pageSize.getHeight()); for (int i = 0; i < pow; i++) { unitSize = new Rectangle(unitSize.getHeight() / 2, unitSize.getWidth()); } final int n = (int) Math.pow(2, pow); final int r = (int) Math.pow(2, pow / 2); final int c = n / r; final Document document = new Document(newSize, 0, 0, 0, 0); // Create a writer for the outputstream final PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); PdfImportedPage page; final PdfContentByte cb = writer.getDirectContent(); // Create a new page in the target for each source page. Rectangle currentSize; float offsetX; float offsetY; float factor; final int total = pdfReader.getNumberOfPages(); for (int i = 0; i < total;) { if (i % n == 0) { document.newPage(); } currentSize = pdfReader.getPageSize(++i); factor = Math.min(unitSize.getWidth() / currentSize.getWidth(), unitSize.getHeight() / currentSize.getHeight()); offsetX = unitSize.getWidth() * (i % n % c) + (unitSize.getWidth() - currentSize.getWidth() * factor) / 2f; offsetY = newSize.getHeight() - (unitSize.getHeight() * (i % n % c) + 1) + (unitSize.getHeight() - currentSize.getHeight() * factor) / 2f; page = writer.getImportedPage(pdfReader, i); cb.addTemplate(page, factor, 0, 0, factor, offsetX, offsetY); if (duplicate) { for (int y = i + 1; y <= pow + 1; y++) { factor = Math.min(unitSize.getWidth() / currentSize.getWidth(), unitSize.getHeight() / currentSize.getHeight()); offsetX = unitSize.getWidth() * (y % n % c) + (unitSize.getWidth() - currentSize.getWidth() * factor) / 2f; offsetY = newSize.getHeight() - unitSize.getHeight() * (y % n / c + 1) + (unitSize.getHeight() - currentSize.getHeight() * factor) / 2f; cb.addTemplate(page, factor, 0, 0, factor, offsetX, offsetY); } } } outputStream.flush(); document.close(); outputStream.close(); } catch (final FileNotFoundException e) { LOG.error("FileNotFoundException", e); } catch (final IOException e) { LOG.error("IOException", e); } catch (final DocumentException e) { LOG.error("DocumentException", e); } return ret; }
From source file:org.egov.ptis.actions.reports.SearchNoticesAction.java
License:Open Source License
/** * @param streamOfPDFFiles/*from ww w. ja v a2 s.c o m*/ * @param outputStream * @return */ private byte[] concatPDFs(final List<InputStream> streamOfPDFFiles, final ByteArrayOutputStream outputStream) { if (LOGGER.isDebugEnabled()) LOGGER.debug("Entered into concatPDFs method"); Document document = null; try { final List<InputStream> pdfs = streamOfPDFFiles; final List<PdfReader> readers = new ArrayList<>(); 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); if (null == document) document = new Document(pdfReader.getPageSize(1)); } // Create a writer for the outputstream final PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); final PdfContentByte cb = writer.getDirectContent(); // Holds the // PDF // data PdfImportedPage page; int pageOfCurrentReaderPDF = 0; final Iterator<PdfReader> iteratorPDFReader = readers.iterator(); // Loop through the PDF files and add to the output. while (iteratorPDFReader.hasNext()) { final PdfReader pdfReader = iteratorPDFReader.next(); // Create a new page in the target for each source page. while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) { document.newPage(); pageOfCurrentReaderPDF++; page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF); cb.addTemplate(page, 0, 0); } pageOfCurrentReaderPDF = 0; } outputStream.flush(); document.close(); outputStream.close(); } catch (final Exception e) { LOGGER.error("Exception in concat PDFs : ", e); } finally { if (document.isOpen()) document.close(); try { if (outputStream != null) outputStream.close(); } catch (final IOException ioe) { LOGGER.error("Exception in concat PDFs : ", ioe); } } if (LOGGER.isDebugEnabled()) LOGGER.debug("Exit from concatPDFs method"); return outputStream.toByteArray(); }
From source file:org.egov.wtms.web.controller.reports.GenerateBillForConsumerCodeController.java
License:Open Source License
private byte[] concatPDFs(final List<InputStream> streamOfPDFFiles, final ByteArrayOutputStream outputStream) { Document document = null;// www. j a va 2 s . c o m try { final List<InputStream> pdfs = streamOfPDFFiles; final List<PdfReader> readers = new ArrayList<>(); 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); if (null == document) document = new Document(pdfReader.getPageSize(1)); } // Create a writer for the outputstream final PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); final PdfContentByte cb = writer.getDirectContent(); // Holds the // PDF // data PdfImportedPage page; int pageOfCurrentReaderPDF = 0; final Iterator<PdfReader> iteratorPDFReader = readers.iterator(); // Loop through the PDF files and add to the output. while (iteratorPDFReader.hasNext()) { final PdfReader pdfReader = iteratorPDFReader.next(); // Create a new page in the target for each source page. while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) { document.newPage(); pageOfCurrentReaderPDF++; page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF); cb.addTemplate(page, 0, 0); } pageOfCurrentReaderPDF = 0; } outputStream.flush(); document.close(); outputStream.close(); } catch (final Exception e) { LOGGER.error("Exception in concat PDFs : ", e); } finally { if (document.isOpen()) document.close(); try { if (outputStream != null) outputStream.close(); } catch (final IOException ioe) { LOGGER.error("Exception in concat PDFs : ", ioe); } } return outputStream.toByteArray(); }
From source file:org.egov.wtms.web.controller.reports.GenerateConnectionBillController.java
License:Open Source License
private byte[] concatPDFs(final List<InputStream> streamOfPDFFiles, final ByteArrayOutputStream outputStream) { Document document = null;/*from ww w . j a v a 2 s . c o m*/ try { final List<InputStream> pdfs = streamOfPDFFiles; final List<PdfReader> readers = new ArrayList<PdfReader>(); 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); if (null == document) document = new Document(pdfReader.getPageSize(1)); } // Create a writer for the outputstream final PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); final PdfContentByte cb = writer.getDirectContent(); // Holds the // PDF // data PdfImportedPage page; int pageOfCurrentReaderPDF = 0; final Iterator<PdfReader> iteratorPDFReader = readers.iterator(); // Loop through the PDF files and add to the output. while (iteratorPDFReader.hasNext()) { final PdfReader pdfReader = iteratorPDFReader.next(); // Create a new page in the target for each source page. while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) { document.newPage(); pageOfCurrentReaderPDF++; page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF); cb.addTemplate(page, 0, 0); } pageOfCurrentReaderPDF = 0; } outputStream.flush(); document.close(); outputStream.close(); } catch (final Exception e) { LOGGER.error("Exception in concat PDFs : ", e); } finally { if (document.isOpen()) document.close(); try { if (outputStream != null) outputStream.close(); } catch (final IOException ioe) { LOGGER.error("Exception in concat PDFs : ", ioe); } } return outputStream.toByteArray(); }
From source file:org.egov.wtms.web.controller.reports.SearchNoticeController.java
License:Open Source License
private byte[] concatPDFs(final List<InputStream> streamOfPDFFiles, final ByteArrayOutputStream outputStream) { Document document = null;/*www . j a v a 2 s .co m*/ try { final List<InputStream> pdfs = streamOfPDFFiles; final List<PdfReader> readers = new ArrayList<>(); 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); if (null == document) document = new Document(pdfReader.getPageSize(1)); } // Create a writer for the outputstream final PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); final PdfContentByte cb = writer.getDirectContent(); // Holds the // PDF // data PdfImportedPage page; int pageOfCurrentReaderPDF = 0; final Iterator<PdfReader> iteratorPDFReader = readers.iterator(); // Loop through the PDF files and add to the output. while (iteratorPDFReader.hasNext()) { final PdfReader pdfReader = iteratorPDFReader.next(); // Create a new page in the target for each source page. while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) { document.newPage(); pageOfCurrentReaderPDF++; page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF); cb.addTemplate(page, 0, 0); } pageOfCurrentReaderPDF = 0; } outputStream.flush(); document.close(); outputStream.close(); } catch (final Exception e) { LOGGER.error("Exception in concat PDFs : ", e); } finally { if (document.isOpen()) document.close(); try { if (outputStream != null) outputStream.close(); } catch (final IOException ioe) { LOGGER.error("Exception in concat PDFs : ", ioe); } } return outputStream != null ? outputStream.toByteArray() : null; }
From source file:org.encog.workbench.util.graph.DocumentPDF.java
License:Apache License
public static void writeChartAsPDF(OutputStream out, JFreeChart chart, int width, int height, FontMapper mapper) throws IOException { Rectangle pagesize = new Rectangle(width, height); Document document = new Document(pagesize, 50, 50, 50, 50); try {//from w w w. j a va 2s . c om PdfWriter writer = PdfWriter.getInstance(document, out); document.addAuthor("JFreeChart"); document.addSubject("Demonstration"); document.open(); PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(width, height); Graphics2D g2 = tp.createGraphics(width, height, mapper); Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height); chart.draw(g2, r2D); g2.dispose(); cb.addTemplate(tp, 0, 0); } catch (DocumentException de) { System.err.println(de.getMessage()); } }
From source file:org.encog.workbench.util.graph.DocumentPDF.java
License:Apache License
public static void savePDF(File filename, JFreeChart chart, int width, int height) { try {/* w w w. ja v a 2 s .c om*/ // step 1 Document document = new Document(new Rectangle(width, height)); // step 2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename)); // step 3 document.open(); // step 4 PdfContentByte canvas = writer.getDirectContent(); Graphics2D g2 = canvas.createGraphics(width, height); Rectangle2D area = new Rectangle2D.Double(0, 0, width, height); chart.draw(g2, area); g2.dispose(); // step 5 document.close(); } catch (DocumentException ex) { throw new WorkBenchError(ex); } catch (IOException ex) { throw new WorkBenchError(ex); } }
From source file:org.forester.archaeopteryx.PdfExporter.java
License:Open Source License
static String writePhylogenyToPdf(final String file_name, final TreePanel tree_panel, int width, int height) throws IOException { if (height < HEIGHT_LIMIT) { height = HEIGHT_LIMIT;/* w w w . j ava2 s . c o m*/ } if (width < WIDTH_LIMIT) { width = WIDTH_LIMIT; } final Phylogeny phylogeny = tree_panel.getPhylogeny(); if ((phylogeny == null) || phylogeny.isEmpty()) { return ""; } if (tree_panel.getMainPanel().getTreeFontSet().getSmallFont().getSize() < 1) { throw new IOException("fonts are too small for PDF export"); } final File file = new File(file_name); if (file.isDirectory()) { throw new IOException("[" + file_name + "] is a directory"); } final Document document = new Document(); document.setPageSize(new Rectangle(width, height)); document.setMargins(WIDTH_LIMIT / 2, WIDTH_LIMIT / 2, HEIGHT_LIMIT / 2, HEIGHT_LIMIT / 2); PdfWriter writer = null; try { writer = PdfWriter.getInstance(document, new FileOutputStream(file_name)); } catch (final DocumentException e) { throw new IOException(e); } document.open(); final DefaultFontMapper mapper = new DefaultFontMapper(); FontFactory.registerDirectories(); if (Util.isWindows()) { mapper.insertDirectory("C:\\WINDOWS\\Fonts\\"); } else if (Util.isMac()) { mapper.insertDirectory("/Library/Fonts/"); mapper.insertDirectory("/System/Library/Fonts/"); } else { mapper.insertDirectory("/usr/X/lib/X11/fonts/TrueType/"); mapper.insertDirectory("/usr/X/lib/X11/fonts/Type1/"); mapper.insertDirectory("/usr/share/fonts/default/TrueType/"); mapper.insertDirectory("/usr/share/fonts/default/Type1/"); } final PdfContentByte cb = writer.getDirectContent(); final Graphics2D g2 = cb.createGraphics(width, height, mapper); try { tree_panel.paintPhylogeny(g2, true, false, width - WIDTH_LIMIT, height - HEIGHT_LIMIT, 0, 0); } catch (final Exception e) { Util.unexpectedException(e); } finally { try { g2.dispose(); document.close(); } catch (final Exception e) { //Do nothing. } } String msg = file.toString(); if ((width > 0) && (height > 0)) { msg += " [size: " + width + ", " + height + "]"; } return msg; }