List of usage examples for com.itextpdf.text.pdf PdfWriter getInstance
public static PdfWriter getInstance(final Document document, final OutputStream os) throws DocumentException
PdfWriter
. From source file:com.maxl.java.amikodesk.SaveBasket.java
License:Open Source License
public void generatePdf(Author author, String filename, String type) { // A4: 8.267in x 11.692in => 595.224units x 841.824units (72units/inch) // marginLeft, marginRight, marginTop, marginBottom Document document = new Document(PageSize.A4, 50, 50, 80, 50); try {/*from w ww . j av a 2 s . c o m*/ if (m_shopping_basket.size() > 0) { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename)); writer.setBoxSize("art", new Rectangle(50, 50, 560, 790)); HeaderFooter event = new HeaderFooter(); writer.setPageEvent(event); document.open(); PdfContentByte cb = writer.getDirectContent(); document.addAuthor("ywesee GmbH"); document.addCreator("AmiKo for Windows"); document.addCreationDate(); // Logo String logoImageStr = m_prefs.get(LogoImageID, Constants.IMG_FOLDER + "empty_logo.png"); File logoFile = new File(logoImageStr); if (!logoFile.exists()) logoImageStr = Constants.IMG_FOLDER + "empty_logo.png"; Image logo = Image.getInstance(logoImageStr); logo.scalePercent(30); logo.setAlignment(Rectangle.ALIGN_RIGHT); document.add(logo); document.add(Chunk.NEWLINE); // Bestelladresse // --> String bestellAdrStr = m_prefs.get(BestellAdresseID, m_rb.getString("noaddress1")); String bestellAdrStr = getAddressAsString(BestellAdresseID); Paragraph p = new Paragraph(12); // p.setIndentationLeft(60); p.add(new Chunk(bestellAdrStr, font_norm_10)); document.add(p); document.add(Chunk.NEWLINE); // Title p = new Paragraph(m_rb.getString("order"), font_bold_16); document.add(p); // Date DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); Date date = new Date(); p = new Paragraph(m_rb.getString("date") + ": " + dateFormat.format(date), font_bold_10); p.setSpacingAfter(20); document.add(p); // document.add(Chunk.NEWLINE); // Add addresses (Lieferadresse + Rechnungsadresse) /* --> OLD String lieferAdrStr = m_prefs.get(LieferAdresseID, m_rb.getString("noaddress2")); String rechnungsAdrStr = m_prefs.get(RechnungsAdresseID, m_rb.getString("noaddress3")); */ // --> NEW String lieferAdrStr = getAddressAsString(LieferAdresseID); String rechnungsAdrStr = getAddressAsString(RechnungsAdresseID); PdfPTable addressTable = new PdfPTable(new float[] { 1, 1 }); addressTable.setWidthPercentage(100f); addressTable.getDefaultCell().setPadding(5); addressTable.setSpacingAfter(5f); addressTable.addCell(getStringCell(m_rb.getString("shipaddress"), font_bold_10, PdfPCell.NO_BORDER, Element.ALIGN_MIDDLE, 1)); addressTable.addCell(getStringCell(m_rb.getString("billaddress"), font_bold_10, PdfPCell.NO_BORDER, Element.ALIGN_MIDDLE, 1)); addressTable.addCell( getStringCell(lieferAdrStr, font_norm_10, PdfPCell.NO_BORDER, Element.ALIGN_MIDDLE, 1)); addressTable.addCell( getStringCell(rechnungsAdrStr, font_norm_10, PdfPCell.NO_BORDER, Element.ALIGN_MIDDLE, 1)); document.add(addressTable); document.add(Chunk.NEWLINE); // Add shopping basket if (type.equals("specific")) document.add(getShoppingBasketForAuthor(author, cb)); else if (type.equals("all")) document.add(getFullShoppingBasket(cb, "all")); else if (type.equals("rest")) document.add(getFullShoppingBasket(cb, "rest")); LineSeparator separator = new LineSeparator(); document.add(separator); } } catch (IOException e) { } catch (DocumentException e) { } document.close(); // System.out.println("Saved PDF to " + filename); }
From source file:com.microware.intrahealth.Createpdf2.java
@SuppressLint("SdCardPath") public boolean write(Context context, String fname, String[] Header, ArrayList<HashMap<String, String>> data, String[] Page2, int Flag) throws Exception { try {//from www .j av a 2 s . c om // file = new File(Environment.getExternalStorageDirectory()+IIHSPdf+ "/"+fname+".pdf"); this.context = context; g = (Global) context.getApplicationContext(); BaseFont urName = BaseFont.createFont("assets/FreeSans.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); urFontName = new Font(urName, 12); urName1 = FontFactory.getFont("assets/mangal.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); // urFontName = new Font(urName, 12); sHeader[0] = context.getResources().getString(R.string.hrpreport); sHeader[1] = context.getResources().getString(R.string.anmname) + " " + g.getsGlobalANMName() + " " + context.getResources().getString(R.string.distname); sHeader[2] = context.getResources().getString(R.string.Identificationcode); sHeader2[0] = context.getResources().getString(R.string.Identificationcode1); sHeader2[1] = context.getResources().getString(R.string.totalhrp); sHeader2[2] = context.getResources().getString(R.string.totalcheckup); sHeader2[3] = context.getResources().getString(R.string.anmsign); catFont2 = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD, new BaseColor(0, 85, 133)); if (Flag == 2) { catFont = new Font(urName, 16, Font.BOLD); catFont1 = new Font(urName, 16, Font.BOLD, new BaseColor(0, 85, 133)); subFont = new Font(urName, 14); smallBold = new Font(urName, 12, Font.BOLD, BaseColor.WHITE); } else { catFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD); catFont1 = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD, new BaseColor(0, 85, 133)); subFont = new Font(Font.FontFamily.TIMES_ROMAN, 14); smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD, BaseColor.WHITE); } String fpath = "/sdcard/msakhi/Pdf"; File path = new File(fpath); File file = new File(path, fname + ".pdf"); if (!path.exists()) { path.mkdirs(); if (!file.exists()) { file.createNewFile(); } } else { if (!file.exists()) { file.createNewFile(); } } // Font bfBold12 = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD, new BaseColor(0, 0, 0)); // Font bf12 = new Font(Font.FontFamily.TIMES_ROMAN, 12); Document document = new Document(A4.rotate()); PdfWriter.getInstance(document, new FileOutputStream(file.getAbsoluteFile())); document.open(); // addMetaData(document); // addTitlePage(document); addContent(document, Header, data, sHeader2, Flag); document.close(); return true; } catch (IOException e) { e.printStackTrace(); return false; } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } }
From source file:com.microware.intrahealth.Createpdfall.java
@SuppressLint("SdCardPath") public boolean write(String fname, String[] Header1, String[] Page1, String[] Page1Value, String heading1, String Page1Remark,/* ww w . j a v a 2s . com*/ String[] Header2, String[] Page2_1, String[] Page2_1source, String[] Page2_1value, String[] Page2_2, String[] Page2_2source, String[] Page2_2value, String[] Page2_3, String[] Page2_3source, String[] Page2_3value, String heading2, String heading3, String heading4, String heading5, String Remark2Value, String[] Header3, String[] Page31, String[] Page3Census1, String[] Page3Value1, String[] Page32, String[] Page3Census2, String[] Page3Value2, String heading6, String heading7, String heading8, String Page3Remark, String[] Header4, String[] Page4_1, String[] Page4Value1, String[] Page4_2, String[] Page4Value2, String[] Page4_3, String[] Page4Value3, String heading9, String heading10, String heading11, String heading12, String Page4Remark1, String Page4Remark3, String[] Header5, String[] Page5Value1, String[] Page5Value2, String[] Page5Value3, String[] Page5Value4, String[] Page5_2, String[] Page5Value5, String[] Page5_3, String[] Page5Value6, String text5, String value5, String heading13, String heading14, String heading15, String Page5Remark1, String Page5Remark2, String[] Header6, String[] Page6Value1, String[] Page6Value2, String[] Page6Value3, String[] Page6Value4, String text6, String value6, String heading16, String[] Header7_1, String[] Page7Value1, String[] Page7Value2, String[] Page7Value3, String[] Page7Value4, String[] Page7Value5, String[] Header7_2, String[] Page7Value6, String[] Page7Value7, String[] Page7Value8, String[] Page7Value9, String[] Page7Value10, String[] Page7Value11, String text7_1, String value7_1, String text7_2, String value7_2, String heading17, String heading18, String heading19, String pdf1, String pdf2, int Flag) throws Exception { try { // file = new File(Environment.getExternalStorageDirectory()+IIHSPdf+ "/"+fname+".pdf"); BaseFont urName = BaseFont.createFont("assets/baamini.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); urFontName = new Font(urName, 12); catFont2 = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD, new BaseColor(0, 85, 133)); if (Flag == 2) { catFont = new Font(urName, 16, Font.BOLD); catFont1 = new Font(urName, 16, Font.BOLD, new BaseColor(0, 85, 133)); subFont = new Font(urName, 14); smallBold = new Font(urName, 12, Font.BOLD, BaseColor.WHITE); } else { catFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD); catFont1 = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD, new BaseColor(0, 85, 133)); subFont = new Font(Font.FontFamily.TIMES_ROMAN, 14); smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD, BaseColor.WHITE); } String fpath = "/sdcard/IIHS/Pdfs"; File path = new File(fpath); File file = new File(path, fname + ".pdf"); if (!path.exists()) { path.mkdirs(); if (!file.exists()) { file.createNewFile(); } } else { if (!file.exists()) { file.createNewFile(); } } // Font bfBold12 = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD, new BaseColor(0, 0, 0)); // Font bf12 = new Font(Font.FontFamily.TIMES_ROMAN, 12); Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(file.getAbsoluteFile())); document.open(); // addMetaData(document); // addTitlePage(document); addContent(document, Header1, Page1, Page1Value, heading1, Page1Remark, Header2, Page2_1, Page2_1source, Page2_1value, Page2_2, Page2_2source, Page2_2value, Page2_3, Page2_3source, Page2_3value, heading2, heading3, heading4, heading5, Remark2Value, Header3, Page31, Page3Census1, Page3Value1, Page32, Page3Census2, Page3Value2, heading6, heading7, heading8, Page3Remark, Header4, Page4_1, Page4Value1, Page4_2, Page4Value2, Page4_3, Page4Value3, heading9, heading10, heading11, heading12, Page4Remark1, Page4Remark3, Header5, Page5Value1, Page5Value2, Page5Value3, Page5Value4, Page5_2, Page5Value5, Page5_3, Page5Value6, text5, value5, heading13, heading14, heading15, Page5Remark1, Page5Remark2, Header6, Page6Value1, Page6Value2, Page6Value3, Page6Value4, text6, value6, heading16, Header7_1, Page7Value1, Page7Value2, Page7Value3, Page7Value4, Page7Value5, Header7_2, Page7Value6, Page7Value7, Page7Value8, Page7Value9, Page7Value10, Page7Value11, text7_1, value7_1, text7_2, value7_2, heading17, heading18, heading19, pdf1, pdf2, Flag); document.close(); return true; } catch (IOException e) { e.printStackTrace(); return false; } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } }
From source file:com.mim.controllers.HomeCtrl.java
public String buildReport() { System.out.println("orden No. " + ordenId); FacesContext context = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse(); //response.setContentType("application/pdf"); //response.setHeader("Content-disposition", "inline=filename=file.pdf"); // Get the text that will be added to the PDF current = ordenFacade.find(Integer.parseInt(ordenId)); System.out.println("equipo: " + current.getEquipoIdequipo().getLugarIdlugar().getNombre()); // step 1/*w w w .j ava 2 s . co m*/ Document document = new Document(new Rectangle(800, 700), 7f, 7f, 50f, 7f); if (ordenId.equals("n/a")) { current.setNumeroOrden(current.getActividad()); } equipo = current.getEquipoIdequipo(); Lugar lugar = equipo.getLugarIdlugar(); //PdfWriter.getInstance(document, new FileOutputStream(orden.getNumeroOrden() + ".pdf")); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { PdfWriter.getInstance(document, baos); document.open(); ordenReport(document, lugar); //document.add(new Paragraph("dsadasdas")); document.close(); // step 2 // setting some response headers response.setHeader("Content-disposition", "attachment; filename=" + current.getNumeroOrden() + ".pdf");// esto hizo que fuera descarga directa //response.setHeader("Expires", "0"); //response.setHeader("Cache-Control", // "must-revalidate, post-check=0, pre-check=0"); //response.setHeader("Pragma", "public"); // setting the content type response.setContentType("application/pdf"); // the contentlength response.setContentLength(baos.size()); try ( // write ByteArrayOutputStream to the ServletOutputStream OutputStream os = response.getOutputStream()) { baos.writeTo(os); os.flush(); } } catch (DocumentException | IOException ex) { Logger.getLogger(HomeCtrl.class.getName()).log(Level.SEVERE, null, ex); } context.responseComplete(); return null; }
From source file:com.mim.controllers.OrdenCtrl.java
public String buildReport() { System.out.println("orden No. " + ordenId); FacesContext context = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse(); //response.setContentType("application/pdf"); //response.setHeader("Content-disposition", "inline=filename=file.pdf"); // Get the text that will be added to the PDF current = ordenFacade.find(Integer.parseInt(ordenId)); System.out.println("lugar equipo: " + current.getEquipoIdequipo().getLugarIdlugar().getNombre()); // step 1/*from www . j a v a 2 s . c om*/ Document document = new Document(new Rectangle(800, 700), 7f, 7f, 50f, 7f); if (ordenId.equals("n/a")) { current.setNumeroOrden(current.getActividad()); } equipo = current.getEquipoIdequipo(); Lugar lugar = equipo.getLugarIdlugar(); //PdfWriter.getInstance(document, new FileOutputStream(orden.getNumeroOrden() + ".pdf")); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { PdfWriter.getInstance(document, baos); document.open(); ordenReport(document, lugar); //document.add(new Paragraph("dsadasdas")); document.close(); // step 2 // setting some response headers response.setHeader("Content-disposition", "attachment; filename=" + current.getNumeroOrden() + ".pdf");// esto hizo que fuera descarga directa //response.setHeader("Expires", "0"); //response.setHeader("Cache-Control", // "must-revalidate, post-check=0, pre-check=0"); //response.setHeader("Pragma", "public"); // setting the content type response.setContentType("application/pdf"); // the contentlength response.setContentLength(baos.size()); try ( // write ByteArrayOutputStream to the ServletOutputStream OutputStream os = response.getOutputStream()) { baos.writeTo(os); os.flush(); } } catch (DocumentException | IOException ex) { Logger.getLogger(HomeCtrl.class.getName()).log(Level.SEVERE, null, ex); } context.responseComplete(); return null; }
From source file:com.mim.servlet.ReportGen.java
public String buildReport(HttpServletResponse response) { System.out.println("orden No. " + ordenId); FacesContext context = FacesContext.getCurrentInstance(); //response.setContentType("application/pdf"); //response.setHeader("Content-disposition", "inline=filename=file.pdf"); // Get the text that will be added to the PDF current = ordenFacade.find(Integer.parseInt(ordenId)); System.out.println("equipo: " + current.getEquipoIdequipo().getLugarIdlugar().getNombre()); // step 1//ww w . j a va2 s .c o m Document document = new Document(new Rectangle(800, 700), 7f, 7f, 50f, 7f); if (ordenId.equals("n/a")) { current.setNumeroOrden(current.getActividad()); } equipo = current.getEquipoIdequipo(); Lugar lugar = equipo.getLugarIdlugar(); //PdfWriter.getInstance(document, new FileOutputStream(orden.getNumeroOrden() + ".pdf")); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { PdfWriter.getInstance(document, baos); document.open(); ordenReport(document, lugar); //document.add(new Paragraph("dsadasdas")); document.close(); // step 2 // setting some response headers response.setHeader("Content-disposition", "attachment; filename=" + current.getNumeroOrden() + ".pdf");// esto hizo que fuera descarga directa //response.setHeader("Expires", "0"); //response.setHeader("Cache-Control", // "must-revalidate, post-check=0, pre-check=0"); //response.setHeader("Pragma", "public"); // setting the content type response.setContentType("application/pdf"); // the contentlength response.setContentLength(baos.size()); try ( // write ByteArrayOutputStream to the ServletOutputStream OutputStream os = response.getOutputStream()) { baos.writeTo(os); os.flush(); } } catch (DocumentException | IOException ex) { Logger.getLogger(HomeCtrl.class.getName()).log(Level.SEVERE, null, ex); } context.responseComplete(); return null; }
From source file:com.miraflorescarwash.controller.PdfController.java
@RequestMapping(value = "/cliente.html", method = RequestMethod.GET) public void doGetPdfCliente(@RequestParam("id") Long id, HttpServletResponse response) { Cliente cliente;//from www . j a va 2 s . c o m ByteArrayOutputStream baos; Document document; OutputStream os; cliente = clienteService.findById(id); if (cliente != null) { clienteService.iniciarRelacionesLazy(cliente); try { document = new Document(PageSize.A4); baos = new ByteArrayOutputStream(); PdfWriter.getInstance(document, baos); // document.open(); // Escribimos crearPdfCliente(document, cliente); // Cerramos // document.close(); //No cerrar ni abrir porque se hace en el metodo arriba // Hay que configurar las cabeceras para que //el navegador detecte que es un <strong>PDF</strong> response.setHeader("Expires", "0"); response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); response.setHeader("Pragma", "public"); // Configuramos el content type response.setContentType("application/pdf"); // Tamao response.setContentLength(baos.size()); // Esccribir el ByteArrayOutputStream a el ServletOutputStream os = response.getOutputStream(); baos.writeTo(os); os.flush(); os.close(); } catch (DocumentException | IOException ex) { Logger.getLogger(PdfController.class.getName()).log(Level.SEVERE, null, ex); } } }
From source file:com.miraflorescarwash.controller.PdfController.java
@RequestMapping(value = "/credito.html", method = RequestMethod.GET) public void doGetPdfClienteCredito(@RequestParam("id") Long id, HttpServletResponse response) { List<CreditoDisponibleCliente> reporteCredito; ByteArrayOutputStream baos;//from w w w . j av a2s .c o m Document document; OutputStream os; reporteCredito = clienteService.verCreditoDisponible(id); if (reporteCredito != null) { try { document = new Document(PageSize.A4); baos = new ByteArrayOutputStream(); PdfWriter.getInstance(document, baos); // Escribimos crearPdfClienteCreditoDisponible(document, reporteCredito); // Cerramos // Hay que configurar las cabeceras para que //el navegador detecte que es un <strong>PDF</strong> response.setHeader("Expires", "0"); response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); response.setHeader("Pragma", "public"); // Configuramos el content type response.setContentType("application/pdf"); // Tamao response.setContentLength(baos.size()); // Escribir el ByteArrayOutputStream a el ServletOutputStream os = response.getOutputStream(); baos.writeTo(os); os.flush(); os.close(); } catch (DocumentException | IOException ex) { Logger.getLogger(PdfController.class.getName()).log(Level.SEVERE, null, ex); } } }
From source file:com.miraflorescarwash.controller.PdfController.java
@RequestMapping(value = "/combos.html", method = RequestMethod.GET) public void doGetPdfCombos(HttpServletResponse response) { List<ComboPorModelo> combos; ByteArrayOutputStream baos;// w w w . ja v a 2 s . c om Document document; OutputStream os; combos = comboPorModeloService.findAll(); if (combos != null) { try { document = new Document(PageSize.A4); baos = new ByteArrayOutputStream(); PdfWriter.getInstance(document, baos); // Escribimos crearPdfCombos(document, combos); // Cerramos // Hay que configurar las cabeceras para que //el navegador detecte que es un <strong>PDF</strong> response.setHeader("Expires", "0"); response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); response.setHeader("Pragma", "public"); // Configuramos el content type response.setContentType("application/pdf"); // Tamao response.setContentLength(baos.size()); // Escribir el ByteArrayOutputStream a el ServletOutputStream os = response.getOutputStream(); baos.writeTo(os); os.flush(); os.close(); } catch (DocumentException | IOException ex) { Logger.getLogger(PdfController.class.getName()).log(Level.SEVERE, null, ex); } } }
From source file:com.miraflorescarwash.controller.PdfController.java
@RequestMapping(value = "/combosReporte.html", method = RequestMethod.GET) public void doGetPdfCombosReporte(HttpServletResponse response) { PdfWriter writer;/*from ww w . j a v a2s . co m*/ List<ComboReporte> combos; ByteArrayOutputStream baos; Document document; OutputStream os; combos = comboService.ComboReporte(); if (combos != null) { // clienteService.iniciarRelacionesLazy(cliente); try { document = new Document(PageSize.A4); baos = new ByteArrayOutputStream(); writer = PdfWriter.getInstance(document, baos); crearPdfCombosReporte(document, combos, writer); // Cerramos // Hay que configurar las cabeceras para que //el navegador detecte que es un <strong>PDF</strong> response.setHeader("Expires", "0"); response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); response.setHeader("Pragma", "public"); // Configuramos el content type response.setContentType("application/pdf"); // Tamao response.setContentLength(baos.size()); // Escribir el ByteArrayOutputStream a el ServletOutputStream os = response.getOutputStream(); baos.writeTo(os); os.flush(); os.close(); } catch (IOException | DocumentException ex) { Logger.getLogger(PdfController.class.getName()).log(Level.SEVERE, null, ex); } } }