List of usage examples for com.itextpdf.text Image getInstance
public static Image getInstance(final Image image)
From source file:com.cts.ptms.carrier.ups.UPSHTTPClient.java
public String createInvoicePDF(String imagePath, String OUTPUT_FILEPATH) throws FileNotFoundException, IOException, DocumentException, InterruptedException, URISyntaxException { float currPosition = 0; String sFilepath = OUTPUT_FILEPATH; Image image = Image.getInstance(imagePath); //create a paragraph Paragraph paragraph = new Paragraph(); Document d = new Document(PageSize.A4_LANDSCAPE.rotate()); PdfWriter w = PdfWriter.getInstance(d, new FileOutputStream(sFilepath)); d.open();/*from w ww . j a v a 2s . com*/ PdfContentByte cb = w.getDirectContent(); ByteArrayOutputStream stampedBuffer; URL resource = this.getClass().getClassLoader().getResource(ShippingConstants.INVOICE_TEMPLATE); File file = new File(resource.toURI()); PdfReader templateReader = new PdfReader(new FileInputStream(file)); stampedBuffer = new ByteArrayOutputStream(); PdfStamper stamper = new PdfStamper(templateReader, stampedBuffer); stamper.setFormFlattening(true); AcroFields form = stamper.getAcroFields(); float[] columnWidths = { 1f, 1f, 1f, 3f }; //create PDF table with the given widths PdfPTable table = new PdfPTable(columnWidths); // form.setField("field1", String.format("Form Text %d", i+1)); form.setField("OBName", "Ragav"); form.setField("OBCompany", "Ragav"); form.setField("OBAddress", "2002 SW Sarazen Cr"); form.setField("OBCity", "Bentonville"); form.setField("OBPhone", "1234567890"); form.setField("STName", "Ragav"); form.setField("STCompany", "Ragav"); form.setField("STAddress", "2002 SW Sarazen Cr"); form.setField("STCity", "Bentonville"); form.setField("STPhone", "1234567890"); form.setField("itemNo", "12334535"); form.setField("itemDesc", "Laundry Bag"); stamper.close(); templateReader.close(); form = null; stamper.close(); templateReader.close(); PdfReader stampedReader = new PdfReader(stampedBuffer.toByteArray()); PdfImportedPage page = w.getImportedPage(stampedReader, 1); cb.addTemplate(page, 0, currPosition); image.scaleAbsoluteHeight(325); image.scaleAbsoluteWidth(550); image.setRotationDegrees(270); image.setAbsolutePosition(450, 20); d.add(image); d.close(); w.close(); return sFilepath; }
From source file:com.datamyne.charts.FinallyDemo.java
License:Apache License
/** * Creates PDf file.//w w w .j a va2 s. c om * @param outputStream {@link OutputStream}. * @throws DocumentException * @throws IOException */ public void create(OutputStream outputStream) throws DocumentException, IOException { Document document = null; PdfWriter writer = null; try { //instantiate document and writer document = new Document(); writer = PdfWriter.getInstance(document, outputStream); //open document document.open(); //get dummy text String text = getText(); //create text font com.itextpdf.text.Font font = new com.itextpdf.text.Font(FontFamily.TIMES_ROMAN, 10.0f); //add text before document.add(new Paragraph(new Chunk(text, font))); //add image int width = 300; int height = 300; JFreeChart chart = getChart(); //create PdfContentByte //if you work with this object, you write to //the top most layer, meaning anything behind //will be clipped PdfContentByte contentByte = writer.getDirectContent(); //create PdfTemplate from PdfContentByte PdfTemplate template = contentByte.createTemplate(width, height); //create Graphics2D from PdfTemplate Graphics2D g2 = template.createGraphics(width, height, new DefaultFontMapper()); //setup the drawing area Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height); //pass the Graphics2D and drawing area to JFreeChart chart.draw(g2, r2D, null); g2.dispose(); //always dispose this //create Image from PdfTemplate Image image = Image.getInstance(template); document.add(image); //add text after document.add(new Paragraph(new Chunk(text, font))); //release resources document.close(); document = null; writer.close(); writer = null; } catch (DocumentException de) { throw de; } finally { //release resources if (null != document) { try { document.close(); } catch (Exception ex) { } } if (null != writer) { try { writer.close(); } catch (Exception ex) { } } } }
From source file:com.deadormi.servlet.CreaPdfServlet.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*ww w .ja va2 s. c om*/ * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String url = request.getQueryString(); Integer gruppo_id = Integer.parseInt(url.substring(url.indexOf("=") + 1, url.length())); List<Utente> iscritti = null; List<Post> posts = null; Gruppo gruppo = null; Utente utente = null; String imageUrl = null; String baseUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort(); try { iscritti = UtenteController.getUserByGroupId(request, gruppo_id); gruppo = GruppoController.getGruppoById(request, gruppo_id); } catch (SQLException ex) { log.error(ex); } // step 1: creation of a document-object Document document = new Document(); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter.getInstance(document, baos); document.open(); String incipit = "Report del " + CurrentDate.getCurrentDate() + "\n"; Paragraph pIncipit = new Paragraph(incipit, FontFactory.getFont(FontFactory.HELVETICA, 14, BaseColor.BLACK)); pIncipit.setAlignment(Element.ALIGN_CENTER); pIncipit.setSpacingAfter(10); document.add(pIncipit); String title = "Gruppo " + gruppo.getNome(); Paragraph pTitle = new Paragraph(title, FontFactory.getFont(FontFactory.HELVETICA, 18, BaseColor.BLUE)); pTitle.setAlignment(Element.ALIGN_CENTER); pTitle.setSpacingAfter(40); document.add(pTitle); PdfPCell labelNome = new PdfPCell(new Paragraph("Nome utente")); PdfPCell labelNumPost = new PdfPCell(new Paragraph("Num. post")); PdfPCell labelData = new PdfPCell(new Paragraph("Data ultimo post")); labelNome.setBorder(Rectangle.NO_BORDER); labelNumPost.setBorder(Rectangle.NO_BORDER); labelData.setBorder(Rectangle.NO_BORDER); for (int i = 0; i < iscritti.size(); i++) { utente = iscritti.get(i); try { posts = PostController.getPostByGruppoIdAndUserId(request, gruppo_id, utente.getId_utente()); } catch (SQLException ex) { Logger.getLogger(CreaPdfServlet.class.getName()).log(Level.SEVERE, null, ex); } if (utente.getNome_avatar() != null) { imageUrl = baseUrl + request.getContextPath() + AVATAR_RESOURCE_PATH + "/" + utente.getId_utente() + "_" + utente.getNome_avatar(); ; } else { imageUrl = baseUrl + request.getContextPath() + "/res/images/user_avatar.png"; } Image image = Image.getInstance(new URL(imageUrl)); image.scaleToFit(50, 50); PdfPTable table = new PdfPTable(3); PdfPCell cellAvatar = new PdfPCell(image); cellAvatar.setHorizontalAlignment(Element.ALIGN_CENTER); cellAvatar.setBorder(Rectangle.NO_BORDER); cellAvatar.setRowspan(3); PdfPCell cellNome = new PdfPCell(new Paragraph(utente.getUsername() + "")); cellNome.setBorder(Rectangle.NO_BORDER); PdfPCell cellNumPost = new PdfPCell(new Paragraph(posts.size() + "")); cellNumPost.setBorder(Rectangle.NO_BORDER); //L'ultimo sempre il piu recente siccome la query ORDER BY data_creazione PdfPCell cellData; if (posts.size() != 0) { cellData = new PdfPCell(new Paragraph(posts.get(posts.size() - 1).getData_creazione())); } else { cellData = new PdfPCell(new Paragraph("N/A")); } cellData.setBorder(Rectangle.NO_BORDER); PdfPCell cellVoidBottom = new PdfPCell(new Paragraph(" ")); cellVoidBottom.setBorder(Rectangle.BOTTOM); cellVoidBottom.setPaddingBottom(10); cellVoidBottom.setColspan(3); PdfPCell cellVoidTop = new PdfPCell(new Paragraph(" ")); cellVoidTop.setBorder(Rectangle.NO_BORDER); cellVoidTop.setPaddingTop(10); cellVoidTop.setColspan(3); table.addCell(cellVoidTop); table.addCell(cellAvatar); table.addCell(labelNome); table.addCell(cellNome); table.addCell(labelNumPost); table.addCell(cellNumPost); table.addCell(labelData); table.addCell(cellData); table.addCell(cellVoidBottom); document.add(table); } document.close(); response.setContentType("application/pdf"); response.addHeader("Content-Disposition", "inline; filename=ahahah.pdf"); OutputStream os = response.getOutputStream(); baos.writeTo(os); os.flush(); os.close(); } catch (DocumentException ex) { Logger.getLogger(CreaPdfServlet.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.devox.GUI.PDF.CrearReporteApto.java
@Override public void setLogo() { try {//from www .ja v a2 s.c om URL url = Main.class.getResource("dhl.png"); LOGO_DHL = (Image.getInstance(url)); LOGO_DHL.scaleAbsolute(110f, 25.2f); URL url1 = Main.class.getResource("tick.png"); TICK = (Image.getInstance(url1)); URL url2 = Main.class.getResource("cross.png"); CROSS = (Image.getInstance(url2)); } catch (BadElementException | IOException ex) { Log.print(ex); } }
From source file:com.devox.GUI.PDF.CrearReporteDestruccion.java
@Override public void setLogo() { try {//from ww w.j a v a 2 s . co m URL url = Main.class.getResource("dhl.png"); LOGO_DHL = (Image.getInstance(url)); } catch (BadElementException | IOException ex) { Log.print(ex); } }
From source file:com.devox.GUI.PDF.ExportarAPDF.java
private void setLogo() { try {//w w w .j a v a 2 s . c o m URL url = Main.class.getResource("dhl.png"); dhl = (Image.getInstance(url)); } catch (BadElementException | IOException ex) { Log.print(ex); System.err.println(ex.toString()); } }
From source file:com.dexter.fms.mbean.ReportsMBean.java
@SuppressWarnings("unchecked") public void createPDF(int type, String filename, int pageType) { try {/*from w w w . j a v a 2s . co m*/ FacesContext context = FacesContext.getCurrentInstance(); Document document = new Document(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter writer = PdfWriter.getInstance(document, baos); writer.setPageEvent(new HeaderFooter()); writer.setBoxSize("footer", new Rectangle(36, 54, 559, 788)); if (!document.isOpen()) { document.open(); } switch (pageType) { case 1: document.setPageSize(PageSize.A4); break; case 2: document.setPageSize(PageSize.A4.rotate()); break; } document.addAuthor("FMS"); document.addCreationDate(); document.addCreator("FMS"); document.addSubject("Report"); document.addTitle(getReport_title()); PdfPTable headerTable = new PdfPTable(1); ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext() .getContext(); String logo = servletContext.getRealPath("") + File.separator + "resources" + File.separator + "images" + File.separator + "satraklogo.jpg"; Hashtable<String, Object> params = new Hashtable<String, Object>(); params.put("partner", dashBean.getUser().getPartner()); GeneralDAO gDAO = new GeneralDAO(); Object pSettingsObj = gDAO.search("PartnerSetting", params); PartnerSetting setting = null; if (pSettingsObj != null) { Vector<PartnerSetting> pSettingsList = (Vector<PartnerSetting>) pSettingsObj; for (PartnerSetting e : pSettingsList) { setting = e; } } gDAO.destroy(); PdfPCell c = null; if (setting != null && setting.getLogo() != null) { Image logoImg = Image.getInstance(setting.getLogo()); logoImg.scaleToFit(212, 51); c = new PdfPCell(logoImg); } else c = new PdfPCell(Image.getInstance(logo)); c.setBorder(0); c.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); headerTable.addCell(c); Paragraph stars = new Paragraph(20); stars.add(Chunk.NEWLINE); stars.setSpacingAfter(20); c = new PdfPCell(stars); c.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); c.setBorder(0); headerTable.addCell(c); BaseFont helvetica = null; try { helvetica = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED); } catch (Exception e) { //font exception } Font font = new Font(helvetica, 16, Font.NORMAL | Font.BOLD); c = new PdfPCell(new Paragraph(getReport_title(), font)); c.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); c.setBorder(0); headerTable.addCell(c); if (getReport_start_dt() != null && getReport_end_dt() != null) { stars = new Paragraph(20); stars.add(Chunk.NEWLINE); stars.setSpacingAfter(10); c = new PdfPCell(stars); c.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); c.setBorder(0); headerTable.addCell(c); new Font(helvetica, 12, Font.NORMAL); Paragraph ch = new Paragraph("From " + getReport_start_dt() + " to " + getReport_end_dt(), font); c = new PdfPCell(ch); c.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); headerTable.addCell(c); } stars = new Paragraph(20); stars.add(Chunk.NEWLINE); stars.setSpacingAfter(20); c = new PdfPCell(stars); c.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); c.setBorder(0); headerTable.addCell(c); document.add(headerTable); PdfPTable pdfTable = exportPDFTable(type); if (pdfTable != null) document.add(pdfTable); //Keep modifying your pdf file (add pages and more) document.close(); String fileName = filename + ".pdf"; writeFileToResponse(context.getExternalContext(), baos, fileName, "application/pdf"); context.responseComplete(); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.dexter.fuelcard.mbean.UtilMBean.java
public byte[] createInvoicePDF(int pageType, String title, String dateperiod, String amount, String chargeType, String chargeAmount, String total) { try {//from ww w . j av a 2s .c o m Document document = new Document(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter writer = PdfWriter.getInstance(document, baos); writer.setPageEvent(new HeaderFooter()); writer.setBoxSize("footer", new Rectangle(36, 54, 559, 788)); if (!document.isOpen()) { document.open(); } switch (pageType) { case 1: document.setPageSize(PageSize.A4); break; case 2: document.setPageSize(PageSize.A4.rotate()); break; } document.addAuthor("FUELCARD"); document.addCreationDate(); document.addCreator("FUELCARD"); document.addSubject("Invoice"); document.addTitle(title); PdfPTable headerTable = new PdfPTable(1); ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext() .getContext(); String logo = servletContext.getRealPath("") + File.separator + "resources" + File.separator + "images" + File.separator + "satraklogo.jpg"; PdfPCell c = new PdfPCell(Image.getInstance(logo)); c.setBorder(0); c.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); headerTable.addCell(c); Paragraph stars = new Paragraph(20); stars.add(Chunk.NEWLINE); stars.setSpacingAfter(20); c = new PdfPCell(stars); c.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); c.setBorder(0); headerTable.addCell(c); BaseFont helvetica = null; try { helvetica = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED); } catch (Exception e) { //font exception } Font font = new Font(helvetica, 16, Font.NORMAL | Font.BOLD); c = new PdfPCell(new Paragraph(title, font)); c.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); c.setBorder(0); headerTable.addCell(c); if (dateperiod != null) { stars = new Paragraph(20); stars.add(Chunk.NEWLINE); stars.setSpacingAfter(10); c = new PdfPCell(stars); c.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); c.setBorder(0); headerTable.addCell(c); new Font(helvetica, 12, Font.NORMAL); Paragraph ch = new Paragraph("For " + dateperiod, font); c = new PdfPCell(ch); c.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); headerTable.addCell(c); } stars = new Paragraph(20); stars.add(Chunk.NEWLINE); stars.setSpacingAfter(20); c = new PdfPCell(stars); c.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); c.setBorder(0); headerTable.addCell(c); document.add(headerTable); PdfPTable pdfTable = new PdfPTable(4); try { helvetica = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED); } catch (Exception e) { } font = new Font(helvetica, 8, Font.BOLDITALIC); pdfTable.addCell(new Paragraph("Charge Type", font)); // % per transaction or flat per license pdfTable.addCell(new Paragraph("Charge Amount", font)); // the amount of the charge setting if (chargeType.equalsIgnoreCase("Percent-Per-Tran")) { pdfTable.addCell(new Paragraph("Total Amount", font)); // the amount of the charge setting } else { pdfTable.addCell(new Paragraph("Total License", font)); // the amount of the charge setting } //pdfTable.addCell(new Paragraph("Description", font)); // the pdfTable.addCell(new Paragraph("Amount Due", font)); font = new Font(helvetica, 8, Font.NORMAL); pdfTable.addCell(new Paragraph(chargeType, font)); pdfTable.addCell(new Paragraph(chargeAmount, font)); pdfTable.addCell(new Paragraph(total, font)); pdfTable.addCell(new Paragraph(amount, font)); pdfTable.setWidthPercentage(100); if (pdfTable != null) document.add(pdfTable); //Keep modifying your pdf file (add pages and more) document.close(); return baos.toByteArray(); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:com.docdoku.server.extras.TitleBlockGenerator.java
License:Open Source License
private void generateLyfeCycleState(Paragraph preface, ResourceBundle bundle) { // Table title preface.add(new Paragraph(bundle.getString("lifecycle") + " : " + lifeCycleState, BOLD_12)); addEmptyLine(preface, 1);//from w w w. j a va2s . co m PdfPTable lifeCycleTable = new PdfPTable(5); lifeCycleTable.setWidthPercentage(100f); PdfPCell cell; for (Activity activity : workflow.getActivities()) { boolean headerRendered = false; for (Task task : activity.getTasks()) { if (task.isApproved() || task.isRejected()) { if (!headerRendered) { // Table head cell = new PdfPCell(new Phrase(activity.getLifeCycleState(), BOLD_12)); cell.setBackgroundColor(BaseColor.LIGHT_GRAY); cell.setColspan(5); lifeCycleTable.addCell(cell); cell = new PdfPCell(new Phrase(bundle.getString("lifecycle.task"), BOLD_12)); cell.setBackgroundColor(BaseColor.LIGHT_GRAY); lifeCycleTable.addCell(cell); cell = new PdfPCell(new Phrase(bundle.getString("lifecycle.date"), BOLD_12)); cell.setBackgroundColor(BaseColor.LIGHT_GRAY); lifeCycleTable.addCell(cell); cell = new PdfPCell(new Phrase(bundle.getString("lifecycle.author"), BOLD_12)); cell.setBackgroundColor(BaseColor.LIGHT_GRAY); lifeCycleTable.addCell(cell); cell = new PdfPCell(new Phrase(bundle.getString("lifecycle.comments"), BOLD_12)); cell.setBackgroundColor(BaseColor.LIGHT_GRAY); lifeCycleTable.addCell(cell); cell = new PdfPCell(new Phrase(bundle.getString("lifecycle.signature"), BOLD_12)); cell.setBackgroundColor(BaseColor.LIGHT_GRAY); lifeCycleTable.addCell(cell); headerRendered = true; } // Table body cell = new PdfPCell(new Phrase(task.getTitle(), NORMAL_12)); lifeCycleTable.addCell(cell); SimpleDateFormat simpleFormat = new SimpleDateFormat(bundle.getString("date.format")); cell = new PdfPCell(new Phrase(simpleFormat.format(task.getClosureDate()), NORMAL_12)); lifeCycleTable.addCell(cell); cell = new PdfPCell(new Phrase(task.getWorker().getName(), NORMAL_12)); lifeCycleTable.addCell(cell); cell = new PdfPCell(new Phrase(task.getClosureComment(), NORMAL_12)); lifeCycleTable.addCell(cell); if (task.getSignature() != null) { try { byte[] imageByte; int indexOfFirstComma = task.getSignature().indexOf(","); String base64 = task.getSignature().substring(indexOfFirstComma + 1, task.getSignature().length()); imageByte = DatatypeConverter.parseBase64Binary(base64); Image image = Image.getInstance(imageByte); image.setCompressionLevel(Image.ORIGINAL_NONE); image.scaleToFit(SIGNATURE_SIZE_W, SIGNATURE_SIZE_H); cell = new PdfPCell(image); lifeCycleTable.addCell(cell); } catch (Exception e) { cell = new PdfPCell(new Phrase(bundle.getString("signature.error"), NORMAL_12)); lifeCycleTable.addCell(cell); } } else { cell = new PdfPCell(new Phrase("")); lifeCycleTable.addCell(cell); } } } } preface.add(lifeCycleTable); }
From source file:com.education.freelancer.MainActivity.java
private void setSignatureStamp(PdfStamper stamper, AcroFields acroFields, String signId) { String DIRECTORY = Environment.getExternalStorageDirectory().getPath() + "/DigitSign/"; String StoredPath = DIRECTORY + signId + ".png"; AcroFields.FieldPosition f = acroFields.getFieldPositions(signId).get(0); int page = f.page; Rectangle rect = f.position;/*w ww.ja v a 2s . c om*/ File imgFile = new File(StoredPath); Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); if (myBitmap == null) { return; } // myBitmap=getResizedBitmap(myBitmap,160); ByteArrayOutputStream stream = new ByteArrayOutputStream(); myBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); Image img = null; try { img = Image.getInstance(stream.toByteArray()); img.scaleToFit(rect.getWidth(), rect.getHeight()); img.setAbsolutePosition(rect.getLeft(), rect.getBottom()); stamper.getOverContent(page).addImage(img); imgFile.delete(); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }