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.gst.infrastructure.dataqueries.service.ReadReportingServiceImpl.java
License:Apache License
@Override public String retrieveReportPDF(final String reportName, final String type, final Map<String, String> queryParams) { final String fileLocation = FileSystemContentRepository.FINERACT_BASE_DIR + File.separator + ""; if (!new File(fileLocation).isDirectory()) { new File(fileLocation).mkdirs(); }/* ww w . j a v a 2 s . c o m*/ final String genaratePdf = fileLocation + File.separator + reportName + ".pdf"; try { final GenericResultsetData result = retrieveGenericResultset(reportName, type, queryParams); final List<ResultsetColumnHeaderData> columnHeaders = result.getColumnHeaders(); final List<ResultsetRowData> data = result.getData(); List<String> row; logger.info("NO. of Columns: " + columnHeaders.size()); final Integer chSize = columnHeaders.size(); final Document document = new Document(PageSize.B0.rotate()); PdfWriter.getInstance(document, new FileOutputStream(new File(fileLocation + reportName + ".pdf"))); document.open(); final PdfPTable table = new PdfPTable(chSize); table.setWidthPercentage(100); for (int i = 0; i < chSize; i++) { table.addCell(columnHeaders.get(i).getColumnName()); } table.completeRow(); Integer rSize; String currColType; String currVal; logger.info("NO. of Rows: " + data.size()); for (int i = 0; i < data.size(); i++) { row = data.get(i).getRow(); rSize = row.size(); for (int j = 0; j < rSize; j++) { currColType = columnHeaders.get(j).getColumnType(); currVal = row.get(j); if (currVal != null) { if (currColType.equals("DECIMAL") || currColType.equals("DOUBLE") || currColType.equals("BIGINT") || currColType.equals("SMALLINT") || currColType.equals("INT")) { table.addCell(currVal.toString()); } else { table.addCell(currVal.toString()); } } } } table.completeRow(); document.add(table); document.close(); return genaratePdf; } catch (final Exception e) { logger.error("error.msg.reporting.error:" + e.getMessage()); throw new PlatformDataIntegrityException("error.msg.exception.error", e.getMessage()); } }
From source file:com.gtdfree.test.PDFTestFont.java
License:Open Source License
/** * Fonts and encoding./*from w w w .ja v a 2s . co m*/ * @param args no arguments needed */ public static void main(String[] args) { System.out.println("Encodings"); String[] names = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); System.out.println(Arrays.toString(names)); /*System.out.println("---"); try { System.getProperties().store(System.out, ""); } catch (IOException e1) { e1.printStackTrace(); } System.out.println("---");*/ //System.out.println(System.getenv()); //System.out.println("---"); //String font= System.getProperty("java.home")+"/lib/fonts/LucidaBrightRegular.ttf"; //String font= "fonts/DejaVuSans.ttf"; //byte[] ttf= ApplicationHelper.loadResource(font); try { // step 1 Document document = new Document(PageSize.A4, 50, 50, 50, 50); // step 2 PdfWriter.getInstance(document, new FileOutputStream("encodingfont.pdf")); // step 3 document.open(); // step 4 String all[] = { "Symbol", "ZapfDingbats" }; Font hex = new Font(Font.HELVETICA, 5); for (int z = 0; z < all.length; ++z) { String file = all[z]; document.add(new Paragraph( "Unicode equivalence for the font \"" + file + "\" with the encoding \"" + file + "\"\n")); /*char tb[]; if (z == 0) tb = SYMBOL_TABLE; else tb = DINGBATS_TABLE;*/ BaseFont bf; if (z == 2) { bf = BaseFont.createFont(file, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, true, null, null); ; } else { bf = BaseFont.createFont(file, file, true); } Font f = new Font(bf, 12); PdfPTable table = new PdfPTable(16); table.setWidthPercentage(100); table.getDefaultCell().setBorderWidth(1); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); for (char k = 0; k < Character.MAX_VALUE; ++k) { char c = k; if (bf.charExists(c)) { Phrase ph = new Phrase(12, new String(new char[] { c }), f); ph.add(new Phrase(12, "\n" + Integer.toString(c) + "\n" + cst(c), hex)); table.addCell(ph); } /* else { Phrase ph = new Phrase("\u00a0"); ph.add(new Phrase(12, "\n\n" + cst(c), hex)); table.addCell(ph); }*/ } document.add(table); document.newPage(); } // step 5 document.close(); FontFactory.registerDirectories(); Set<?> s = FontFactory.getRegisteredFonts(); System.out.println("Fonts: " + s); s = FontFactory.getRegisteredFamilies(); System.out.println("Families: " + s); ArrayList<Font> f = new ArrayList<Font>(s.size()); for (Object name : s) { try { f.add(FontFactory.getFont(name.toString(), "UTF-8", true, 12, Font.NORMAL, Color.BLACK, true)); } catch (Exception e) { f.add(FontFactory.getFont(name.toString(), "UTF-8", false, 12, Font.NORMAL, Color.BLACK, true)); } } Collections.sort(f, new Comparator<Font>() { @Override public int compare(Font o1, Font o2) { return o1.getFamilyname().compareTo(o2.getFamilyname()); } }); for (Font ff : f) { if (ff.getBaseFont() == null) { continue; } System.out.println(ff.getFamilyname() + " " + ff.getBaseFont().isEmbedded()); } } catch (Exception de) { de.printStackTrace(); } }
From source file:com.gtdfree.test.TableBorders.java
License:Open Source License
/** * Demonstrates different borderstyles.//from w ww . j a v a 2 s. c o m * * @param args * the number of rows for each table fragment. */ public static void main(String[] args) { System.out.println("Table Borders"); // step1 Document document = new Document(PageSize.A4, 50, 50, 50, 50); try { // step2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TableBorders.pdf")); // step3 document.open(); // step4 // page 1 Font tableFont = FontFactory.getFont("Helvetica", 8, Font.BOLD, Color.BLACK); float padding = 0f; Rectangle border = new Rectangle(0f, 0f); border.setBorderWidthLeft(6f); border.setBorderWidthBottom(5f); border.setBorderWidthRight(4f); border.setBorderWidthTop(2f); border.setBorderColorLeft(Color.RED); border.setBorderColorBottom(Color.ORANGE); border.setBorderColorRight(Color.YELLOW); border.setBorderColorTop(Color.GREEN); makeTestPage(tableFont, border, writer, document, padding, true, true); Font font = FontFactory.getFont("Helvetica", 10); Paragraph p; p = new Paragraph("\nVarious border widths and colors\nuseAscender=true, useDescender=true", font); document.add(p); document.newPage(); // page 2 padding = 2f; border = new Rectangle(0f, 0f); border.setBorderWidthLeft(1f); border.setBorderWidthBottom(2f); border.setBorderWidthRight(1f); border.setBorderWidthTop(2f); border.setBorderColor(Color.BLACK); makeTestPage(tableFont, border, writer, document, padding, true, true); p = new Paragraph( "More typical use - padding of 2\nuseBorderPadding=true, useAscender=true, useDescender=true", font); document.add(p); document.newPage(); // page 3 padding = 0f; border = new Rectangle(0f, 0f); border.setBorderWidthLeft(1f); border.setBorderWidthBottom(2f); border.setBorderWidthRight(1f); border.setBorderWidthTop(2f); border.setBorderColor(Color.BLACK); makeTestPage(tableFont, border, writer, document, padding, false, true); p = new Paragraph("\nuseBorderPadding=true, useAscender=false, useDescender=true", font); document.add(p); document.newPage(); // page 4 padding = 0f; border = new Rectangle(0f, 0f); border.setBorderWidthLeft(1f); border.setBorderWidthBottom(2f); border.setBorderWidthRight(1f); border.setBorderWidthTop(2f); border.setBorderColor(Color.BLACK); makeTestPage(tableFont, border, writer, document, padding, false, false); p = new Paragraph("\nuseBorderPadding=true, useAscender=false, useDescender=false", font); document.add(p); document.newPage(); // page 5 padding = 0f; border = new Rectangle(0f, 0f); border.setBorderWidthLeft(1f); border.setBorderWidthBottom(2f); border.setBorderWidthRight(1f); border.setBorderWidthTop(2f); border.setBorderColor(Color.BLACK); makeTestPage(tableFont, border, writer, document, padding, true, false); p = new Paragraph("\nuseBorderPadding=true, useAscender=true, useDescender=false", font); document.add(p); } catch (Exception de) { de.printStackTrace(); } // step5 document.close(); }
From source file:com.hotaviano.tableexporter.pdf.PDFExporter.java
License:Open Source License
@Override public byte[] export(String htmlTable) throws DocumentException { com.lowagie.text.Document doc = new com.lowagie.text.Document(); Document document = null;// w ww. ja v a2 s . c o m try { document = parser.parse(htmlTable); } catch (JDOMException | IOException ex) { throw new DocumentException(ex); } ByteArrayOutputStream out = new ByteArrayOutputStream(); PdfWriter.getInstance(doc, out); doc.open(); PdfPTable table = new PdfPTable(columnLength(document)); List<PdfPCell> cells = getHeaders(document); for (PdfPCell cell : cells) { table.addCell(cell); } List<String> data = getBodyValues(document); for (String item : data) { table.addCell(new PdfPCell(new Phrase(item))); } doc.add(table); doc.close(); return out.toByteArray(); }
From source file:com.huateng.system.util.PdfUtil.java
License:Open Source License
public static void create(String mchtId, String selMchtId, String path, List<Object[]> list, LinkedHashMap<String, List<Object[]>> map, Set<String> set) throws Exception { BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); ///*from ww w. j a v a 2 s. c o m*/ Font font17 = new Font(bfChinese, 17, Font.BOLD); Font font8 = new Font(bfChinese, 8, Font.NORMAL); Font font9 = new Font(bfChinese, 9, Font.NORMAL); Font font9Bold = new Font(bfChinese, 9, Font.BOLD); Font font8Red = new Font(bfChinese, 8, Font.NORMAL); font8Red.setColor(Color.RED); Font font8Green = new Font(bfChinese, 8, Font.NORMAL); font8Green.setColor(Color.GREEN); logger.info("Starting build document..."); Document document = new Document(PageSize.A4, 36, 36, 36, 36); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path)); document.open(); LINECANVAS border = new LINECANVAS(); // float[] widths = { 0.1f, 0.35f, 0.35f, 0.1f, 0.1f }; PdfPTable table = new PdfPTable(widths); table.setWidthPercentage(100); table.getDefaultCell().setBorder(PdfPCell.NO_BORDER); table.getDefaultCell().setFixedHeight(12); //CELL PdfPCell cellMchntId = new PdfPCell(new Paragraph(mchtId, font8)); cellMchntId.setBorder(PdfPCell.BOTTOM); cellMchntId.setHorizontalAlignment(Element.ALIGN_CENTER); PdfPCell cellMchntName = new PdfPCell(new Paragraph(InformationUtil.getMchtName(mchtId), font8)); cellMchntName.setBorder(PdfPCell.BOTTOM); cellMchntName.setHorizontalAlignment(Element.ALIGN_CENTER); String point = InformationUtil.getCurPaperPoint(selMchtId); PdfPCell cellPoint; cellPoint = new PdfPCell(new Paragraph(point, font8Red)); cellPoint.setBorder(PdfPCell.NO_BORDER); cellPoint.setHorizontalAlignment(Element.ALIGN_CENTER); PdfPCell cellLevel; String level = InformationUtil.getCurPaperLevel(selMchtId); if (Integer.valueOf(point) >= 60) { cellLevel = new PdfPCell(new Paragraph(level, font8Green)); } else { cellLevel = new PdfPCell(new Paragraph(level, font8Red)); } cellLevel.setBorder(PdfPCell.NO_BORDER); cellLevel.setHorizontalAlignment(Element.ALIGN_CENTER); // Image img = Image.getInstance( ServletActionContext.getServletContext().getResource("/ext/resources/images/Title_logo.gif")); img.scalePercent(70); float w = img.getScaledWidth(); float h = img.getScaledHeight(); writer.getDirectContent().addImage(img, w, 0, 0, h, 36, PageSize.A4.getHeight() - 36 - h); // PdfPCell cell = new PdfPCell(new Paragraph("?", font17)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setColspan(5); cell.setFixedHeight(h); cell.setBorder(PdfPCell.NO_BORDER); table.addCell(cell); cell = new PdfPCell(); cell.setFixedHeight(20); cell.setColspan(5); cell.setBorder(PdfPCell.NO_BORDER); table.addCell(cell); table.addCell(new Paragraph("?", font8)); table.addCell(cellMchntId); table.addCell(" "); table.addCell(new Paragraph(": ", font8)); table.addCell(cellPoint); table.addCell(new Paragraph("??", font8)); table.addCell(cellMchntName); table.addCell(" "); table.addCell(new Paragraph(": ", font8)); table.addCell(cellLevel); document.add(table); document.add(new Paragraph("\n\n")); //? PdfPTable t = new PdfPTable(1); Iterator<Object[]> it0 = list.iterator(); int i = 1; while (it0.hasNext()) { Object[] obj = it0.next(); PdfPCell c = new PdfPCell(); c.addElement(new Paragraph("Q" + String.valueOf(i++) + "" + obj[1].toString(), font9Bold)); List<Object[]> opts = map.get(obj[0].toString()); String opt = ""; Iterator<Object[]> it1 = opts.iterator(); while (it1.hasNext()) { Object[] o = it1.next(); if (set.contains(o[0])) { opt += "? "; opt += o[1].toString(); opt += " "; } else { opt += " "; opt += o[1].toString(); opt += " "; } } c.addElement(new Paragraph(opt.trim(), font9)); c.setBorder(PdfPCell.NO_BORDER); if (i - 1 != list.size()) { c.setCellEvent(border); } t.addCell(c); } PdfPTable oTable = new PdfPTable(1); oTable.setWidthPercentage(100); PdfPCell ce = new PdfPCell(t); ce.setBorderColor(Color.GRAY); oTable.addCell(ce); document.add(oTable); document.close(); logger.info("Finish build document..."); }
From source file:com.huateng.system.util.PdfUtil.java
License:Open Source License
public static void create(String path, List<Object[]> list, LinkedHashMap<String, List<Object[]>> map) throws Exception { BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); //// w w w . j a v a 2 s. c o m Font font17 = new Font(bfChinese, 17, Font.BOLD); Font font8 = new Font(bfChinese, 8, Font.NORMAL); Font font10 = new Font(bfChinese, 10, Font.NORMAL); Font font10Bold = new Font(bfChinese, 10, Font.BOLD); Font font8Red = new Font(bfChinese, 8, Font.NORMAL); font8Red.setColor(Color.RED); Font font8Green = new Font(bfChinese, 8, Font.NORMAL); font8Green.setColor(Color.GREEN); logger.info("Starting build document..."); Document document = new Document(PageSize.A4, 36, 36, 36, 36); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path)); document.open(); LINECANVAS border = new LINECANVAS(); // float[] widths = { 0.1f, 0.35f, 0.55f }; PdfPTable table = new PdfPTable(widths); table.setWidthPercentage(100); table.getDefaultCell().setBorder(PdfPCell.NO_BORDER); table.getDefaultCell().setFixedHeight(12); //CELL PdfPCell cellMchntId = new PdfPCell(new Paragraph("XXXXXXXXXXXXXXX", font8)); cellMchntId.setBorder(PdfPCell.BOTTOM); cellMchntId.setHorizontalAlignment(Element.ALIGN_CENTER); PdfPCell cellMchntName = new PdfPCell(new Paragraph("?", font8)); cellMchntName.setBorder(PdfPCell.BOTTOM); cellMchntName.setHorizontalAlignment(Element.ALIGN_CENTER); // Image img = Image.getInstance( ServletActionContext.getServletContext().getResource("/ext/resources/images/Title_logo.gif")); img.scalePercent(70); float w = img.getScaledWidth(); float h = img.getScaledHeight(); writer.getDirectContent().addImage(img, w, 0, 0, h, 36, PageSize.A4.getHeight() - 36 - h); // PdfPCell cell = new PdfPCell(new Paragraph("?", font17)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setColspan(3); cell.setFixedHeight(h); cell.setBorder(PdfPCell.NO_BORDER); table.addCell(cell); cell = new PdfPCell(); cell.setFixedHeight(20); cell.setColspan(3); cell.setBorder(PdfPCell.NO_BORDER); table.addCell(cell); table.addCell(new Paragraph("?", font8)); table.addCell(cellMchntId); table.addCell(" "); table.addCell(new Paragraph("??", font8)); table.addCell(cellMchntName); table.addCell(" "); document.add(table); document.add(new Paragraph("\n\n")); //? PdfPTable t = new PdfPTable(1); Iterator<Object[]> it0 = list.iterator(); int i = 1; while (it0.hasNext()) { Object[] obj = it0.next(); PdfPCell c = new PdfPCell(); c.addElement(new Paragraph("Q" + String.valueOf(i++) + "" + obj[1].toString(), font10Bold)); List<Object[]> opts = map.get(obj[0].toString()); String opt = ""; Iterator<Object[]> it1 = opts.iterator(); while (it1.hasNext()) { Object[] o = it1.next(); opt += " "; opt += o[1].toString(); opt += " "; } c.addElement(new Paragraph(opt.trim(), font10)); c.setBorder(PdfPCell.NO_BORDER); if (i - 1 != list.size()) { c.setCellEvent(border); } t.addCell(c); } PdfPTable oTable = new PdfPTable(1); oTable.setWidthPercentage(100); PdfPCell ce = new PdfPCell(t); ce.setBorderColor(Color.GRAY); oTable.addCell(ce); document.add(oTable); document.close(); logger.info("Finish build document..."); }
From source file:com.iana.boesc.utility.BOESCUtil.java
License:Open Source License
/** * /* w w w. ja v a 2 s .c o m*/ * @param list * @param outputStream * @throws DocumentException * @throws IOException */ public static void doMerge(List<InputStream> list, OutputStream outputStream) throws DocumentException, IOException { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); PdfContentByte cb = writer.getDirectContent(); for (InputStream in : list) { PdfReader reader = new PdfReader(in); for (int i = 1; i <= reader.getNumberOfPages(); i++) { document.newPage(); // import the page from source pdf PdfImportedPage page = writer.getImportedPage(reader, i); // add the page to the destination pdf cb.addTemplate(page, 0, 0); } } outputStream.flush(); document.close(); outputStream.close(); }
From source file:com.ideaspymes.proyecttemplate.stock.web.ProductoConsultaBean.java
public String createPdf() throws IOException, DocumentException { EtiquetaConf conf = etiquetaConfDAO.getEtiquetaConfDefault(); if (hayParaImprimir() && conf != null) { HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance() .getExternalContext().getResponse(); response.setContentType("application/x-pdf"); response.setHeader("Content-Disposition", "attachment; filename=\"etiquetas.pdf\""); float ancho = Utilities.millimetersToPoints(conf.getAnchoHoja().floatValue()); System.out.println("Ancho: " + ancho); float largo = Utilities.millimetersToPoints(conf.getLargoHoja().floatValue()); System.out.println("Alto: " + largo); // step 1 Document document = new Document(new Rectangle(ancho, largo)); // step 2 document.setMargins(0f, 0f, 0f, 0f); PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream()); // step 3 document.open();//from ww w . j a v a2s . c o m // step 4 PdfContentByte cb = writer.getDirectContent(); for (Producto p : getLista()) { if (p.getCantidadEtiquetas() > 0) { PdfPTable table = new PdfPTable(2); table.setWidthPercentage(96); Font fontbold = FontFactory.getFont("Times-Roman", 8, Font.NORMAL); Chunk productTitle = new Chunk("HC", fontbold); Paragraph pTitile = new Paragraph(productTitle); pTitile.setAlignment(Element.ALIGN_LEFT); pTitile.setLeading(6, 0); PdfPCell cellTitle = new PdfPCell(); cellTitle.setHorizontalAlignment(Element.ALIGN_LEFT); cellTitle.setVerticalAlignment(Element.ALIGN_TOP); cellTitle.setBorder(Rectangle.NO_BORDER); cellTitle.addElement(pTitile); Font font2 = FontFactory.getFont("Times-Roman", conf.getTamDescripcion().floatValue(), Font.NORMAL); Chunk productName = new Chunk(p.getNombre(), font2); Paragraph pName = new Paragraph(productName); pName.setAlignment(Element.ALIGN_CENTER); //pTitile.setLeading(0, 1); cellTitle.addElement(pName); table.addCell(cellTitle); Barcode39 code39 = new Barcode39(); code39.setCode(p.getCodigo()); //code39.setCodeType(Barcode.EAN13); code39.setBarHeight(conf.getAltoCodBarra().floatValue()); //codeEan.setX(0.7f); code39.setSize(conf.getTamDescripcion().floatValue()); //code39.setAltText("HC - " + p.getNombre()); PdfPCell cellBarcode = new PdfPCell(); cellBarcode.setHorizontalAlignment(Element.ALIGN_CENTER); cellBarcode.setBorder(Rectangle.NO_BORDER); cellBarcode.addElement(code39.createImageWithBarcode(cb, null, Color.BLACK)); table.addCell(cellBarcode); for (int i = 0; i < p.getCantidadEtiquetas(); i++) { document.add(table); document.newPage(); } } } // step 5 document.close(); response.getOutputStream().flush(); response.getOutputStream().close(); FacesContext.getCurrentInstance().responseComplete(); } else { FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "No hay nada que imprimir", "")); } return null; }
From source file:com.ikon.util.DocConverter.java
License:Open Source License
/** * Convert HTML to PDF//from w w w. j a v a 2s .c o m */ public void html2pdf(File input, File output) throws ConversionException, DatabaseException, IOException { log.debug("** Convert from HTML to PDF **"); FileOutputStream fos = null; try { fos = new FileOutputStream(output); // Make conversion Document doc = new Document(PageSize.A4); PdfWriter.getInstance(doc, fos); doc.open(); HTMLWorker html = new HTMLWorker(doc); html.parse(new FileReader(input)); doc.close(); } catch (DocumentException e) { throw new ConversionException("Exception in conversion: " + e.getMessage(), e); } finally { IOUtils.closeQuietly(fos); } }
From source file:com.ikon.util.DocConverter.java
License:Open Source License
/** * Convert TXT to PDF/*from w ww . j av a2 s .com*/ */ public void txt2pdf(InputStream is, File output) throws ConversionException, DatabaseException, IOException { log.debug("** Convert from TXT to PDF **"); FileOutputStream fos = null; String line = null; try { fos = new FileOutputStream(output); // Make conversion BufferedReader br = new BufferedReader(new InputStreamReader(is)); Document doc = new Document(PageSize.A4); PdfWriter.getInstance(doc, fos); doc.open(); while ((line = br.readLine()) != null) { doc.add(new Paragraph(12F, line)); } doc.close(); } catch (DocumentException e) { throw new ConversionException("Exception in conversion: " + e.getMessage(), e); } finally { IOUtils.closeQuietly(fos); } }