List of usage examples for com.lowagie.text Document isOpen
public boolean isOpen()
From source file:org.nabucco.framework.template.impl.service.pdf.util.MergePdfUtil.java
License:Open Source License
public static void concatPDFs(List<InputStream> streamOfPDFFiles, OutputStream outputStream, boolean paginate) { Document document = new Document(); try {//www. ja v a 2s .c o m List<InputStream> pdfs = streamOfPDFFiles; List<PdfReader> readers = new ArrayList<PdfReader>(); int totalPages = 0; Iterator<InputStream> iteratorPDFs = pdfs.iterator(); // Create Readers for the pdfs. while (iteratorPDFs.hasNext()) { InputStream pdf = iteratorPDFs.next(); PdfReader pdfReader = new PdfReader(pdf); readers.add(pdfReader); totalPages += pdfReader.getNumberOfPages(); } // Create a writer for the outputstream PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); PdfContentByte cb = writer.getDirectContent(); // Holds the PDF // data PdfImportedPage page; int currentPageNumber = 0; int pageOfCurrentReaderPDF = 0; Iterator<PdfReader> iteratorPDFReader = readers.iterator(); // Loop through the PDF files and add to the output. while (iteratorPDFReader.hasNext()) { PdfReader pdfReader = iteratorPDFReader.next(); // Create a new page in the target for each source page. while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) { document.newPage(); pageOfCurrentReaderPDF++; currentPageNumber++; page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF); cb.addTemplate(page, 0, 0); } pageOfCurrentReaderPDF = 0; } outputStream.flush(); document.close(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (document.isOpen()) document.close(); try { if (outputStream != null) outputStream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } }
From source file:org.oscarehr.common.printing.HtmlToPdfServlet.java
License:Open Source License
private static void close(Document document) { if (document != null && document.isOpen()) { try {// w w w . java 2 s. c o m document.close(); } catch (Exception e) { MiscUtils.getLogger().error("Unable to close document", e); } } }
From source file:org.primefaces.component.export.PDFExporter.java
License:Open Source License
@Override public void export(FacesContext context, DataTable table, String filename, boolean pageOnly, boolean selectionOnly, String encodingType, MethodExpression preProcessor, MethodExpression postProcessor, ExporterOptions options) throws IOException { try {/*from w ww. ja v a2s. c o m*/ Document document = new Document(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter.getInstance(document, baos); if (preProcessor != null) { preProcessor.invoke(context.getELContext(), new Object[] { document }); } if (!document.isOpen()) { document.open(); } if (options != null) { expOptions = options; } document.add(exportPDFTable(context, table, pageOnly, selectionOnly, encodingType)); if (postProcessor != null) { postProcessor.invoke(context.getELContext(), new Object[] { document }); } document.close(); writePDFToResponse(context.getExternalContext(), baos, filename); } catch (DocumentException e) { throw new IOException(e.getMessage()); } }
From source file:org.primefaces.component.export.PDFExporter.java
License:Open Source License
@Override public void export(FacesContext context, List<String> clientIds, String outputFileName, boolean pageOnly, boolean selectionOnly, String encodingType, MethodExpression preProcessor, MethodExpression postProcessor, ExporterOptions options) throws IOException { try {/*from w w w .j a v a 2 s . c om*/ Document document = new Document(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter.getInstance(document, baos); if (preProcessor != null) { preProcessor.invoke(context.getELContext(), new Object[] { document }); } if (!document.isOpen()) { document.open(); } if (options != null) { expOptions = options; } VisitContext visitContext = VisitContext.createVisitContext(context, clientIds, null); VisitCallback visitCallback = new PDFExportVisitCallback(this, document, pageOnly, selectionOnly, encodingType); context.getViewRoot().visitTree(visitContext, visitCallback); if (postProcessor != null) { postProcessor.invoke(context.getELContext(), new Object[] { document }); } document.close(); writePDFToResponse(context.getExternalContext(), baos, outputFileName); } catch (DocumentException e) { throw new IOException(e.getMessage()); } }
From source file:org.primefaces.component.export.PDFExporter.java
License:Open Source License
@Override public void export(FacesContext context, String outputFileName, List<DataTable> tables, boolean pageOnly, boolean selectionOnly, String encodingType, MethodExpression preProcessor, MethodExpression postProcessor, ExporterOptions options) throws IOException { try {/* ww w . j a v a2 s . c om*/ Document document = new Document(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter.getInstance(document, baos); if (preProcessor != null) { preProcessor.invoke(context.getELContext(), new Object[] { document }); } if (!document.isOpen()) { document.open(); } if (options != null) { expOptions = options; } for (DataTable table : tables) { document.add(exportPDFTable(context, table, pageOnly, selectionOnly, encodingType)); Paragraph preface = new Paragraph(); addEmptyLine(preface, 3); document.add(preface); } if (postProcessor != null) { postProcessor.invoke(context.getELContext(), new Object[] { document }); } document.close(); writePDFToResponse(context.getExternalContext(), baos, outputFileName); } catch (DocumentException e) { throw new IOException(e.getMessage()); } }
From source file:oscar.eform.util.EFormPDFServlet.java
License:Open Source License
/** * the form txt file has lines in the form: * * For Checkboxes:/*from w w w . ja v a2 s . c o m*/ * ie. ohip : left, 76, 193, 0, BaseFont.ZAPFDINGBATS, 8, \u2713 * requestParamName : alignment, Xcoord, Ycoord, 0, font, fontSize, textToPrint[if empty, prints the value of the request param] * NOTE: the Xcoord and Ycoord refer to the bottom-left corner of the element * * For single-line text: * ie. patientCity : left, 242, 261, 0, BaseFont.HELVETICA, 12 * See checkbox explanation * * For multi-line text (textarea) * ie. aci : left, 20, 308, 0, BaseFont.HELVETICA, 8, _, 238, 222, 10 * requestParamName : alignment, bottomLeftXcoord, bottomLeftYcoord, 0, font, fontSize, _, topRightXcoord, topRightYcoord, spacingBtwnLines * *NOTE: When working on these forms in linux, it helps to load the PDF file into gimp, switch to pt. coordinate system and use the mouse to find the coordinates. *Prepare to be bored! * * * @throws Exception */ protected ByteArrayOutputStream generatePDFDocumentBytes(final HttpServletRequest req, final ServletContext ctx, int multiple) throws Exception { // added by vic, hsfo if (HSFO_RX_DATA_KEY.equals(req.getParameter("__title"))) return generateHsfoRxPDF(req); String suffix = (multiple > 0) ? String.valueOf(multiple) : ""; ByteArrayOutputStream baosPDF = new ByteArrayOutputStream(); Document document = new Document(); PdfWriter writer = null; try { writer = PdfWriter.getInstance(document, baosPDF); String title = req.getParameter("__title" + suffix) != null ? req.getParameter("__title" + suffix) : "Unknown"; String template = req.getParameter("__template" + suffix) != null ? req.getParameter("__template" + suffix) + ".pdf" : ""; int numPages = 1; String pages = req.getParameter("__numPages" + suffix); if (pages != null) { numPages = Integer.parseInt(pages); } //load config files Properties[] printCfg = loadPrintCfg(req, suffix); Properties[][] graphicCfg = loadGraphicCfg(req, suffix, numPages); int cfgFileNo = printCfg == null ? 0 : printCfg.length; Properties props = new Properties(); getPrintPropValues(props, req, suffix); Properties measurements = new Properties(); //initialise measurement collections = a list of pages sections measurements List<List<List<String>>> xMeasurementValues = new ArrayList<List<List<String>>>(); List<List<List<String>>> yMeasurementValues = new ArrayList<List<List<String>>>(); for (int idx = 0; idx < numPages; ++idx) { MiscUtils.getLogger().debug("Adding page " + idx); xMeasurementValues.add(new ArrayList<List<String>>()); yMeasurementValues.add(new ArrayList<List<String>>()); } saveMeasurementValues(measurements, props, req, numPages, xMeasurementValues, yMeasurementValues); addDocumentProps(document, title, props); // create a reader for a certain document String propFilename = OscarProperties.getInstance().getProperty("eform_image", "") + "/" + template; PdfReader reader = null; try { reader = new PdfReader(propFilename); log.debug("Found template at " + propFilename); } catch (Exception dex) { log.warn("Cannot find template at : " + propFilename); } // retrieve the total number of pages int n = reader.getNumberOfPages(); // retrieve the size of the first page Rectangle pSize = reader.getPageSize(1); float height = pSize.getHeight(); PdfContentByte cb = writer.getDirectContent(); int i = 0; while (i < n) { document.newPage(); i++; PdfImportedPage page1 = writer.getImportedPage(reader, i); cb.addTemplate(page1, 1, 0, 0, 1, 0, 0); cb.setRGBColorStroke(0, 0, 255); // LEFT/CENTER/RIGHT, X, Y, if (i <= cfgFileNo) { writeContent(printCfg[i - 1], props, measurements, height, cb); } //end if there are print properties //graphic Properties[] tempPropertiesArray; if (i <= graphicCfg.length) { tempPropertiesArray = graphicCfg[i - 1]; MiscUtils.getLogger().debug("Plotting page " + i); } else { tempPropertiesArray = null; MiscUtils.getLogger().debug("Skipped Plotting page " + i); } //if there are properties to plot if (tempPropertiesArray != null) { MiscUtils.getLogger().debug("TEMP PROP LENGTH " + tempPropertiesArray.length); for (int k = 0; k < tempPropertiesArray.length; k++) { //initialise with measurement values which are mapped to config file by form get graphic function List<String> xDate, yHeight; if (xMeasurementValues.get(i - 1).size() > k && yMeasurementValues.get(i - 1).size() > k) { xDate = new ArrayList<String>(xMeasurementValues.get(i - 1).get(k)); yHeight = new ArrayList<String>(yMeasurementValues.get(i - 1).get(k)); } else { xDate = new ArrayList<String>(); yHeight = new ArrayList<String>(); } plotProperties(tempPropertiesArray[k], props, xDate, yHeight, height, cb, (k % 2 == 0)); } } //end: if there are properties to plot } } finally { if (document.isOpen()) document.close(); if (writer != null) writer.close(); } return baosPDF; }
From source file:test.itext.html.Main.java
License:Open Source License
public static void main(String args[]) throws Exception { Document document = new Document(); StringBuffer sb = new StringBuffer(); sb.append("<root><p><u><em><strong>Product Description</strong></em></u></p>"); sb.append(// www . j a va 2s .c o m "<p style=\"text-align: justify\">Face the biggest test in soccer and come out on top with FIFA 08. Bringing home the silverware is harder than ever before as you make critical selection decisions and battle bookings, injuries and fatigue to produce a winning team. Defeat the toughest opposition with your in-form players overpower teams with quick movement off the ball and varying attacks. Select from fully-licensed teams, raise your game with the help of the crowd and start your quest for glory.</p>"); sb.append("<p style=\"text-align: justify\"><strong>Starting the game</strong></p>"); sb.append( "<p style=\"text-align: justify\">Highlight EA SPORTS FIFA 08 in the games menu on your phone. Press Select (or OK on the D-Pad) to start the game. You are prompted to choose sound either on or off. After you make your selection, the EA Mobile splash screen appears followed by EA SPORTS FIFA 08 title sequence and the Main Menu.<br />"); sb.append("</p>"); sb.append("<p style=\"text-align: justify\"><strong>Main Menu</strong></p>"); sb.append("<ul>"); sb.append("<li>Play Start a game.</li>"); sb.append( "<li>Options View the available game options: sound, vibration, match length, difficulty, etc</li>"); sb.append("<li>Help View detailed instructions and information on how to play the game.</li>"); sb.append("<li>About Display copyright information and customer service information.</li>"); sb.append("<li>More Games Display link to more EA Mobile games.</li>"); sb.append("<li>Exit Exit the game.</li>"); sb.append("</ul></root>"); /*Tidy tidy=new Tidy(); tidy.setXmlOut(true); tidy.setXmlTags(true); byte[] buff=sb.toString().getBytes("utf-8"); ByteArrayOutputStream os=new ByteArrayOutputStream(); tidy.parse(new ByteArrayInputStream(buff), os); String temp=os.toString("utf-8");*/ try { Reader reader = new StringReader(sb.toString()); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("html1.pdf")); document.open(); writer.setPageEmpty(false); HtmlParser.parse(document, reader); } catch (Exception e) { e.printStackTrace(); } if (document.isOpen()) document.close(); }