List of usage examples for com.lowagie.text.pdf PdfWriter getInstance
public static PdfWriter getInstance(Document document, OutputStream os) throws DocumentException
PdfWriter
. From source file:com.bayareasoftware.chartengine.chart.jfree.JFreeChartDriver.java
License:Apache License
/** * Writes a chart to an output stream in PDF format. * * @param out the output stream.// w ww . j ava 2 s . c o m * @param chart the chart. * @param width the chart width. * @param height the chart height. * */ public void writeChartAsPDF(OutputStream out, JFreeChart chart, int width, int height, FontMapper mapper) { com.lowagie.text.Rectangle pagesize = new com.lowagie.text.Rectangle(width, height); Document document = new Document(pagesize, 50.0f, 50.0f, 50.0f, 50.0f); try { PdfWriter writer = PdfWriter.getInstance(document, out); document.addAuthor("ChartMechanic"); document.addSubject("ChartMechanic PDF"); 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()); } document.close(); }
From source file:com.bean.UserBean.java
public void gerarPDF1() { try {// ww w .j av a 2 s . c o m Document doc = new Document(PageSize.A4); OutputStream os = new FileOutputStream("C:/Users/lprates/Downloads/out.pdf"); PdfWriter.getInstance(doc, os); doc.open(); Paragraph p = new Paragraph("Meu primeiro arquivo PDF!"); doc.add(p); doc.close(); os.close(); //abre pdf usando o PDF Reader instalado na maquina do Usuario Desktop.getDesktop().open(new File("C:/Users/lprates/Downloads/out.pdf")); } catch (Exception e) { } }
From source file:com.bean.UserBean.java
public void gerarPDF2() { try {//from w ww.jav a 2s .co m Document doc = new Document(); ByteArrayOutputStream baosPDF = new ByteArrayOutputStream(); PdfWriter docWriter = null; docWriter = PdfWriter.getInstance(doc, baosPDF); doc.open(); doc.add(new Paragraph("This document was created by a class named: " + this.getClass().getName())); doc.add(new Paragraph("This document was created on " + new java.util.Date())); doc.close(); docWriter.close(); FacesContext facesContext = FacesContext.getCurrentInstance(); ExternalContext externalContext = facesContext.getExternalContext(); externalContext.setResponseContentType("application/pdf"); // com a opcao inline abre o PDF no browser do usuario //externalContext.setResponseHeader("Content-Disposition", "inline; filename=\"my.pdf\""); // com a opcao attachment faz download do PDF no computador do usuario externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"my.pdf\""); OutputStream responseOutputStream = externalContext.getResponseOutputStream(); baosPDF.writeTo(responseOutputStream); responseOutputStream.flush(); baosPDF.reset(); facesContext.responseComplete(); } catch (Exception e) { } }
From source file:com.beetle.framework.web.controller.DocumentController.java
License:LGPL
private void createPDF(OutputStream out, IDocument doc, WebInput wi) throws IOException, ServletException { this.setContentType("application/pdf"); ByteArrayOutputStream baos = new ByteArrayOutputStream(INITIAL_SIZE); Document document = new Document(PageSize.A4); DocInfo di;//from w w w . j ava 2 s. com try { PdfWriter.getInstance(document, baos); di = new DocInfo(IDocument.TYPE_PDF); di.setPdfDocument(document); // doc.createAttribute(wi, di); document.open(); // doc.createContent(wi, di); document.close(); this.setContentLength(baos.size()); baos.writeTo(out); } catch (Exception ex1) { throw new ServletException(ex1); } finally { if (out != null) { out.flush(); out.close(); out = null; } if (baos != null) { baos.close(); baos = null; } document = null; di = null; doc = null; } }
From source file:com.bibisco.export.ITextExporter.java
License:GNU General Public License
@Override public void init(String pStrFilePath, RichTextEditorSettings pRichTextEditorSettings) { try {/*from www . j a v a 2 s.co m*/ mFile = new File(pStrFilePath); if (mExportType == ExportType.PDF) { PdfWriter.getInstance(mDocument, new FileOutputStream(mFile)); } else { RtfWriter2.getInstance(mDocument, new FileOutputStream(mFile)); } initFont(pRichTextEditorSettings); mBlnParagraphIndent = pRichTextEditorSettings.isIndentParagraphEnabled(); mDocument.open(); } catch (Throwable t) { mLog.error(t); throw new BibiscoException(t, BibiscoException.FATAL); } }
From source file:com.bytecode.customexporter.PDFCustomExporter.java
@Override public void export(ActionEvent event, String tableId, FacesContext context, String filename, String tableTitle, boolean pageOnly, boolean selectionOnly, String encodingType, MethodExpression preProcessor, MethodExpression postProcessor, boolean subTable) throws IOException { try {//w ww. ja va 2 s . c om Document document = new Document(); if (orientation.equalsIgnoreCase("Landscape")) document.setPageSize(PageSize.A4.rotate()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter.getInstance(document, baos); StringTokenizer st = new StringTokenizer(tableId, ","); while (st.hasMoreElements()) { String tableName = (String) st.nextElement(); UIComponent component = SearchExpressionFacade.resolveComponent(context, event.getComponent(), tableName); if (component == null) { throw new FacesException("Cannot find component \"" + tableName + "\" in view."); } if (!(component instanceof DataTable || component instanceof DataList)) { throw new FacesException("Unsupported datasource target:\"" + component.getClass().getName() + "\", exporter must target a PrimeFaces DataTable/DataList."); } if (preProcessor != null) { preProcessor.invoke(context.getELContext(), new Object[] { document }); } if (!document.isOpen()) { document.open(); } if (tableTitle != null && !tableTitle.isEmpty() && !tableId.contains("" + ",")) { Font tableTitleFont = FontFactory.getFont(FontFactory.TIMES, encodingType, Font.DEFAULTSIZE, Font.BOLD); Paragraph title = new Paragraph(tableTitle, tableTitleFont); document.add(title); Paragraph preface = new Paragraph(); addEmptyLine(preface, 3); document.add(preface); } PdfPTable pdf; DataList list = null; DataTable table = null; if (component instanceof DataList) { list = (DataList) component; pdf = exportPDFTable(context, list, pageOnly, encodingType); } else { table = (DataTable) component; pdf = exportPDFTable(context, table, pageOnly, selectionOnly, encodingType, subTable); } if (pdf != null) { document.add(pdf); } // add a couple of blank lines Paragraph preface = new Paragraph(); addEmptyLine(preface, datasetPadding); document.add(preface); 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:com.byterefinery.rmbench.export.diagram.PDFDiagramExporter.java
License:Open Source License
protected void doExport(OutputStream out, IFigure figure) { Rectangle bounds = getBounds(figure); Document document = new Document(new com.lowagie.text.Rectangle(bounds.width, bounds.height)); PdfWriter pdf;/* w w w .j a v a2s . com*/ try { pdf = PdfWriter.getInstance(document, out); document.open(); document.add(new Chunk(" ")); } catch (DocumentException e) { ExportPlugin.logError(e); return; } PdfContentByte contentbytes = pdf.getDirectContent(); PdfTemplate template = contentbytes.createTemplate(bounds.width, bounds.height); Graphics2D graphics2d = template.createGraphics(bounds.width, bounds.height); try { GraphicsToGraphics2DAdaptor graphics = new GraphicsToGraphics2DAdaptor(graphics2d, bounds.getTranslated(bounds.getLocation().negate())); graphics.translate(bounds.getLocation().negate()); figure.paint(graphics); } finally { graphics2d.dispose(); contentbytes.addTemplate(template, 0, 0); document.close(); } }
From source file:com.centurylink.mdw.designer.pages.ExportHelper.java
License:Apache License
/** * Export a process/*from w w w.j ava 2s. c om*/ * @param filename the file name (including path) where the document will be generated * @param format can be docx, pdf, rtf, html and bpmn * @param canvas for printing process images * @param graph the process to be printed. */ public void exportProcess(String filename, String format, Graph process, DesignerCanvas canvas) throws Exception { initialize(false); String oldNodeIdType = process.getNodeIdType(); try { process.setNodeIdType(nodeIdType); options.add(SECTION_NUMBER); if (format.equals(DOCX)) { DocxBuilder builder = printProcessDocx(filename, process, canvas); builder.save(new java.io.File(filename)); return; } else if (format.equals(HTML)) { StringBuffer sb = printPrologHtml("Process " + process.getName()); printProcessHtml(sb, canvas, 0, process, filename); printEpilogHtml(sb, filename); return; } else if (format.equals(JPG) || format.equals(PNG)) { byte[] imgBytes = printImage(-1f, canvas, process.getGraphSize(), format.equals(JPG) ? "jpeg" : "png"); OutputStream os = null; try { os = new FileOutputStream(new File(filename)); os.write(imgBytes); return; } catch (Exception ex) { ex.printStackTrace(); throw ex; } finally { if (os != null) os.close(); } } else if (format.equals(BPMN2)) { new BPMNHelper().exportProcess(process.getProcessVO(), filename); } else { // itext processor Document document = new Document(); try { DocWriter writer = null; if (format.equals(RTF)) { writer = RtfWriter2.getInstance(document, new FileOutputStream(filename)); } else if (format.equals(PDF)) { writer = PdfWriter.getInstance(document, new FileOutputStream(filename)); } document.open(); document.setPageSize(PageSize.LETTER); Rectangle page_size = document.getPageSize(); Chapter chapter = printOneProcessPdf(writer, canvas, format, 1, process, filename, page_size); document.add(chapter); } catch (Exception ex) { ex.printStackTrace(); throw ex; } finally { // step 5: we close the document document.close(); } } } finally { process.setNodeIdType(oldNodeIdType); } }
From source file:com.centurylink.mdw.designer.pages.ExportHelper.java
License:Apache License
/** * Export multiple processes//from w ww . jav a 2 s . c o m * @param filename the file name (including path) where the document will be generated * @param type can be pdf, rtf and html * @param flowchart the designer page (for using its canvas and report errors) * @param graphs the list of processes to be printed. * @param options options for printing, from the print dialog. */ public void exportProcesses(String filename, String type, FlowchartPage flowchart, List<Graph> graphs) throws Exception { initialize(false); options.add(SECTION_NUMBER); // step 1: creation of a document-object Document document = new Document(); try { // step 2: create PDF or RTF writer DocWriter writer; if (type.equals(RTF)) { writer = RtfWriter2.getInstance(document, new FileOutputStream(filename)); } else if (type.equals(PDF)) { writer = PdfWriter.getInstance(document, new FileOutputStream(filename)); } else { boolean directHtml = true; if (directHtml) { StringBuffer sb = printPrologHtml("Processes"); Graph process; for (int i = 0; i < graphs.size(); i++) { process = graphs.get(i); flowchart.setProcess(process); this.printProcessHtml(sb, flowchart.canvas, i + 1, process, filename); } printEpilogHtml(sb, filename); return; } writer = HtmlWriter.getInstance(document, new FileOutputStream(filename)); } // step 3: we open the document document.open(); // step 4: we add contents to the document document.setPageSize(PageSize.LETTER); Graph process; Chapter chapter; Rectangle page_size = document.getPageSize(); for (int i = 0; i < graphs.size(); i++) { process = graphs.get(i); process.setNodeIdType(nodeIdType); flowchart.setProcess(process); chapter = printOneProcessPdf(writer, flowchart.canvas, type, i + 1, process, filename, page_size); document.add(chapter); } } finally { // step 5: we close the document document.close(); } }
From source file:com.centurylink.mdw.designer.pages.ExportHelper.java
License:Apache License
public void printImagePdf(String filename, DesignerCanvas canvas, Dimension graphsize) { try {/*w w w.j a v a 2 s . co m*/ DefaultFontMapper mapper = new DefaultFontMapper(); FontFactory.registerDirectories(); mapper.insertDirectory("c:\\winnt\\fonts"); // mapper.insertDirectory("c:\\windows\\fonts"); // we create a template and a Graphics2D object that corresponds // with it int margin = 72; // 1 inch float scale = 0.5f; boolean multiple_page = true; Rectangle page_size; if (multiple_page) { page_size = PageSize.LETTER.rotate(); } else { page_size = new Rectangle((int) (graphsize.getWidth() * scale) + margin, (int) (graphsize.getHeight() * scale) + margin); } Document document = new Document(page_size); DocWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename)); document.open(); document.setPageSize(page_size); int image_w = (int) page_size.getWidth() - margin; int image_h = (int) page_size.getHeight() - margin; boolean edsave = canvas.editable; canvas.editable = false; Color bgsave = canvas.getBackground(); canvas.setBackground(Color.white); if (multiple_page) { int horizontal_pages = (int) (graphsize.width * scale) / image_w + 1; int vertical_pages = (int) (graphsize.height * scale) / image_h + 1; for (int i = 0; i < horizontal_pages; i++) { for (int j = 0; j < vertical_pages; j++) { Image img; PdfContentByte cb = ((PdfWriter) writer).getDirectContent(); PdfTemplate tp = cb.createTemplate(image_w, image_h); Graphics2D g2 = tp.createGraphics(image_w, image_h, mapper); tp.setWidth(image_w); tp.setHeight(image_h); g2.scale(scale, scale); g2.translate(-i * image_w / scale, -j * image_h / scale); canvas.paintComponent(g2); g2.dispose(); img = new ImgTemplate(tp); document.add(img); } } } else { Image img; PdfContentByte cb = ((PdfWriter) writer).getDirectContent(); PdfTemplate tp = cb.createTemplate(image_w, image_h); Graphics2D g2 = tp.createGraphics(image_w, image_h, mapper); tp.setWidth(image_w); tp.setHeight(image_h); g2.scale(scale, scale); canvas.paintComponent(g2); g2.dispose(); img = new ImgTemplate(tp); document.add(img); } canvas.setBackground(bgsave); canvas.editable = edsave; document.close(); } catch (Exception e) { e.printStackTrace(); } }