List of usage examples for com.itextpdf.text Image scalePercent
public void scalePercent(final float percent)
From source file:com.kohmiho.mpsr.export.PDFGenerator.java
protected void addPageByImage(PdfWriter writer, PdfImportedPage page) throws BadElementException, DocumentException { Image image = Image.getInstance(page); image.setAbsolutePosition(36, 36);//w w w.j a va2s . com image.scalePercent(90); writer.getDirectContentUnder().addImage(image); writer.setPageEmpty(false); }
From source file:com.kohmiho.mpsr.export.PDFGenerator.java
private void generatePageHeader(Document document, PdfContentByte directContentUnder, Image image, Phrase[] headerTexts) {/*from w w w. j a v a2s . co m*/ float left = document.getPageSize().getLeft(36); float top = document.getPageSize().getTop(36); float right = document.getPageSize().getRight(36); try { image.setAbsolutePosition(right - 150, top - 20); // image.setAlignment(Element.ALIGN_RIGHT); image.scalePercent(25); directContentUnder.addImage(image); } catch (DocumentException e) { } int i = 0; for (Phrase headerText : headerTexts) { ColumnText.showTextAligned(directContentUnder, Element.ALIGN_LEFT, headerText, left + 36, top - 10 * (i++), 0); } }
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 w w. ja v a2s . co 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.maxl.java.amikodesk.SaveBasket.java
License:Open Source License
public PdfPTable getShoppingBasketForAuthor(Author a, PdfContentByte cb) { int position = 0; float subtotal_CHF = 0.0f; float shipping_CHF = 0.0f; float vat25_CHF = 0.0f; float vat80_CHF = 0.0f; String author = a.getShortName(); BarcodeEAN codeEAN = new BarcodeEAN(); // Pos | Menge | Eancode | Bezeichnung | MwSt | Preis PdfPTable table = new PdfPTable(new float[] { 1, 2, 3, 6, 1, 2 }); table.setWidthPercentage(100f);//from w ww. j ava 2s . c o m table.getDefaultCell().setPadding(5); table.setSpacingAfter(5f); PdfPCell cell = new PdfPCell(); table.addCell(getStringCell(m_rb.getString("position"), font_bold_10, Rectangle.TOP | Rectangle.BOTTOM, Element.ALIGN_MIDDLE, 1)); table.addCell(getStringCell(m_rb.getString("quantity"), font_bold_10, Rectangle.TOP | Rectangle.BOTTOM, Element.ALIGN_MIDDLE, 1)); table.addCell(getStringCell(m_rb.getString("ean"), font_bold_10, Rectangle.TOP | Rectangle.BOTTOM, Element.ALIGN_MIDDLE, 1)); table.addCell(getStringCell(m_rb.getString("article"), font_bold_10, Rectangle.TOP | Rectangle.BOTTOM, Element.ALIGN_MIDDLE, 1)); table.addCell(getStringCell(m_rb.getString("vat"), font_bold_10, Rectangle.TOP | Rectangle.BOTTOM, Element.ALIGN_RIGHT, 1)); table.addCell(getStringCell(m_rb.getString("price") + " (CHF)", font_bold_10, Rectangle.TOP | Rectangle.BOTTOM, Element.ALIGN_RIGHT, 1)); if (m_shopping_basket.size() > 0 && !author.isEmpty()) { for (Map.Entry<String, Article> entry : m_shopping_basket.entrySet()) { Article article = entry.getValue(); if (article.getAuthor().trim().toLowerCase().contains(author)) { String price_pruned = ""; String total_price_CHF = ""; if (article.getCode() != null && article.getCode().equals("ibsa")) { float cr = article.getCashRebate(); if (article.getDraufgabe() > 0) { price_pruned = String.format("%.2f", article.getBuyingPrice(0.0f)); total_price_CHF = String.format("%.2f", article.getTotBuyingPrice(0.0f)); } else { price_pruned = String.format("%.2f", article.getBuyingPrice(cr)); total_price_CHF = String.format("%.2f", article.getTotBuyingPrice(cr)); } } else { price_pruned = article.getCleanExfactoryPrice(); total_price_CHF = String.format("%.2f", article.getTotExfactoryPrice()); } if (!price_pruned.isEmpty() && !price_pruned.equals("..")) { // Index table.addCell(getStringCell(Integer.toString(++position), font_norm_10, PdfPCell.NO_BORDER, Element.ALIGN_MIDDLE, 1)); // Anzahl table.addCell(getStringCell(Integer.toString(article.getQuantity()), font_norm_10, PdfPCell.NO_BORDER, Element.ALIGN_MIDDLE, 1)); // EAN code codeEAN.setCode(article.getEanCode()); Image img = codeEAN.createImageWithBarcode(cb, null, null); img.scalePercent(120); cell = new PdfPCell(img); cell.setBorder(Rectangle.NO_BORDER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setUseBorderPadding(true); cell.setBorderWidth(5); if (position == 1) cell.setPaddingTop(8); else cell.setPaddingTop(0); cell.setPaddingBottom(8); table.addCell(cell); // Artikelbezeichnung table.addCell(getStringCell(article.getPackTitle(), font_norm_10, PdfPCell.NO_BORDER, Element.ALIGN_MIDDLE, 1)); // MwSt table.addCell(getStringCell(String.format("%.1f%%", article.getVat()), font_norm_10, PdfPCell.NO_BORDER, Element.ALIGN_RIGHT, 1)); // Preis (exkl. MwSt) // float price_CHF = article.getQuantity()*Float.parseFloat(price_pruned); table.addCell(getStringCell(total_price_CHF, font_norm_10, PdfPCell.NO_BORDER, Element.ALIGN_RIGHT, 1)); } } } subtotal_CHF = a.getSubtotal(); shipping_CHF = a.getShippingCosts(); vat25_CHF = a.getVat25(); vat80_CHF = a.getVat80() + a.getShippingCosts() * 0.08f; float fulltotal_CHF = subtotal_CHF + shipping_CHF + vat25_CHF + vat80_CHF; table.addCell(getStringCell(m_rb.getString("subtotal"), font_bold_10, Rectangle.TOP, Element.ALIGN_MIDDLE, 2)); table.addCell(getStringCell("", font_bold_10, Rectangle.TOP, Element.ALIGN_MIDDLE, 2)); table.addCell(getStringCell(String.format("%.2f", subtotal_CHF), font_bold_10, Rectangle.TOP, Element.ALIGN_RIGHT, 2)); table.addCell(getStringCell(m_rb.getString("shipping"), font_norm_10, PdfPCell.NO_BORDER, Element.ALIGN_MIDDLE, 2)); table.addCell(getStringCell("", font_norm_10, PdfPCell.NO_BORDER, Element.ALIGN_MIDDLE, 2)); table.addCell(getStringCell(String.format("%.2f", shipping_CHF), font_norm_10, PdfPCell.NO_BORDER, Element.ALIGN_RIGHT, 2)); table.addCell(getStringCell(m_rb.getString("vat") + " (2.5%)", font_norm_10, PdfPCell.NO_BORDER, Element.ALIGN_MIDDLE, 2)); table.addCell(getStringCell("", font_norm_10, PdfPCell.NO_BORDER, Element.ALIGN_MIDDLE, 2)); table.addCell(getStringCell(String.format("%.2f", vat25_CHF), font_norm_10, PdfPCell.NO_BORDER, Element.ALIGN_RIGHT, 2)); table.addCell(getStringCell(m_rb.getString("vat") + " (8.0%)", font_norm_10, PdfPCell.NO_BORDER, Element.ALIGN_MIDDLE, 2)); table.addCell(getStringCell("", font_norm_10, PdfPCell.NO_BORDER, Element.ALIGN_MIDDLE, 2)); table.addCell(getStringCell(String.format("%.2f", vat80_CHF), font_norm_10, PdfPCell.NO_BORDER, Element.ALIGN_RIGHT, 2)); table.addCell(getStringCell(m_rb.getString("gesamttotal"), font_bold_10, PdfPCell.NO_BORDER, Element.ALIGN_MIDDLE, 2)); table.addCell(getStringCell("", font_bold_10, PdfPCell.NO_BORDER, Element.ALIGN_MIDDLE, 2)); table.addCell(getStringCell(String.format("%.2f", fulltotal_CHF), font_bold_10, PdfPCell.NO_BORDER, Element.ALIGN_RIGHT, 2)); } return table; }
From source file:com.maxl.java.amikodesk.SaveBasket.java
License:Open Source License
public PdfPTable getFullShoppingBasket(PdfContentByte cb, String mode) { int position = 0; float sub_total_CHF = 0.0f; BarcodeEAN codeEAN = new BarcodeEAN(); // Pos | Menge | Eancode | Bezeichnung | Preis PdfPTable table = new PdfPTable(new float[] { 1, 1, 3, 6, 2 }); table.setWidthPercentage(100f);// w w w .j a v a 2 s .co m table.getDefaultCell().setPadding(5); table.setSpacingAfter(5f); PdfPCell cell = new PdfPCell(); table.addCell(getStringCell(m_rb.getString("position"), font_bold_10, Rectangle.TOP | Rectangle.BOTTOM, Element.ALIGN_MIDDLE, 1)); table.addCell(getStringCell(m_rb.getString("quantity"), font_bold_10, Rectangle.TOP | Rectangle.BOTTOM, Element.ALIGN_MIDDLE, 1)); table.addCell(getStringCell(m_rb.getString("ean"), font_bold_10, Rectangle.TOP | Rectangle.BOTTOM, Element.ALIGN_MIDDLE, 1)); table.addCell(getStringCell(m_rb.getString("article"), font_bold_10, Rectangle.TOP | Rectangle.BOTTOM, Element.ALIGN_MIDDLE, 1)); table.addCell(getStringCell(m_rb.getString("price") + " (CHF)", font_bold_10, Rectangle.TOP | Rectangle.BOTTOM, Element.ALIGN_RIGHT, 1)); if (m_shopping_basket.size() > 0) { for (Map.Entry<String, Article> entry : m_shopping_basket.entrySet()) { Article article = entry.getValue(); if (mode.equals("all") || (mode.equals("rest") && (m_map_of_authors == null || !anyElemIsContained(m_map_of_authors, article.getAuthor().trim().toLowerCase())))) { String price_pruned = ""; if (article.getCode() != null && article.getCode().equals("ibsa")) { float cr = article.getCashRebate(); if (article.getDraufgabe() > 0) price_pruned = String.format("%.2f", article.getBuyingPrice(0.0f)); else price_pruned = String.format("%.2f", article.getBuyingPrice(cr)); } else { price_pruned = article.getCleanExfactoryPrice(); } if (!price_pruned.isEmpty() && !price_pruned.equals("..")) { table.addCell(getStringCell(Integer.toString(++position), font_norm_10, PdfPCell.NO_BORDER, Element.ALIGN_MIDDLE, 1)); table.addCell(getStringCell(Integer.toString(article.getQuantity()), font_norm_10, PdfPCell.NO_BORDER, Element.ALIGN_MIDDLE, 1)); codeEAN.setCode(article.getEanCode()); Image img = codeEAN.createImageWithBarcode(cb, null, null); img.scalePercent(120); cell = new PdfPCell(img); cell.setBorder(Rectangle.NO_BORDER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setUseBorderPadding(true); cell.setBorderWidth(5); if (position == 1) cell.setPaddingTop(8); else cell.setPaddingTop(0); cell.setPaddingBottom(8); table.addCell(cell); table.addCell(getStringCell(article.getPackTitle(), font_norm_10, PdfPCell.NO_BORDER, Element.ALIGN_MIDDLE, 1)); float price_CHF = article.getQuantity() * Float.parseFloat(price_pruned); sub_total_CHF += price_CHF; table.addCell(getStringCell(String.format("%.2f", price_CHF), font_norm_10, PdfPCell.NO_BORDER, Element.ALIGN_RIGHT, 1)); } } } table.addCell(getStringCell(m_rb.getString("subtotal"), font_bold_10, Rectangle.TOP, Element.ALIGN_MIDDLE, 2)); table.addCell(getStringCell("", font_bold_10, Rectangle.TOP, Element.ALIGN_MIDDLE, 2)); table.addCell(getStringCell(String.format("%.2f", sub_total_CHF), font_bold_10, Rectangle.TOP, Element.ALIGN_RIGHT, 2)); table.addCell(getStringCell(m_rb.getString("vat") + " (2.5%)", font_norm_10, PdfPCell.NO_BORDER, Element.ALIGN_MIDDLE, 2)); table.addCell(getStringCell("", font_norm_10, PdfPCell.NO_BORDER, Element.ALIGN_MIDDLE, 2)); table.addCell(getStringCell(String.format("%.2f", sub_total_CHF * 0.025f), font_norm_10, PdfPCell.NO_BORDER, Element.ALIGN_RIGHT, 2)); table.addCell(getStringCell(m_rb.getString("gesamttotal"), font_bold_10, PdfPCell.NO_BORDER, Element.ALIGN_MIDDLE, 2)); table.addCell(getStringCell("", font_bold_10, PdfPCell.NO_BORDER, Element.ALIGN_MIDDLE, 2)); table.addCell(getStringCell(String.format("%.2f", sub_total_CHF * 1.025f), font_bold_10, PdfPCell.NO_BORDER, Element.ALIGN_RIGHT, 2)); } return table; }
From source file:com.mobicage.rogerthat.enterprise.samples.hr.bizz.GenerateExpenseNote.java
License:Open Source License
public int handle(final User user, final User manager, final ExpenseNote en) throws MalformedURLException, IOException, DocumentException { log.info("Building list of expenses ..."); List<Expense> expenses = Expense.list(en); log.info("Retrieved " + expenses.size() + " expenses from the datastore"); log.info("Creating ExpenseNoteDocOutputStream"); ExpenseNoteDocOutputStream stream = new ExpenseNoteDocOutputStream(en); Document document = new Document(); PdfWriter.getInstance(document, stream); document.open();// w ww. j av a2 s . c om document.addTitle("Expense note " + en.id + " of " + user.name); document.addSubject("Expense note generated by Rogerthat Enterprise!"); document.addKeywords("expense note"); document.addAuthor(user.name); document.addCreator("Rogerthat OneApp Enterprise Mobility"); Paragraph preface = new Paragraph(); preface.add(new Paragraph("TP Vision expense note", titleFont)); preface.add(new Paragraph(" ")); DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy"); Date date = new Date(); preface.add(new Paragraph("Date: " + dateFormat.format(date))); preface.add(new Paragraph("Requestor: " + user.name)); preface.add(new Paragraph("Approver: " + manager.name)); preface.add(new Paragraph(" ")); PdfPTable table = new PdfPTable(7); table.setWidthPercentage(110); table.setWidths(new int[] { 5, 15, 15, 35, 10, 15, 10 }); addHeader(table, "Id"); addHeader(table, "Date"); addHeader(table, "Nature"); addHeader(table, "Description"); addHeader(table, "Account"); addHeader(table, "Amount"); addHeader(table, "Voucher"); table.setHeaderRows(1); Collections.sort(expenses, new Comparator<Expense>() { @Override public int compare(Expense e1, Expense e2) { return (int) (e1.date - e2.date); } }); int i = 0; double total = 0; for (Expense expense : expenses) { PdfPCell c1 = new PdfPCell(new Phrase("" + ++i)); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); table.addCell(dateFormat.format(new Date(expense.date * 1000))); table.addCell(expense.nature); table.addCell(expense.description); table.addCell("" + expense.account); c1 = new PdfPCell(new Phrase(DECIMAL_FORMAT.format(expense.amount) + " " + expense.currency)); c1.setHorizontalAlignment(Element.ALIGN_RIGHT); table.addCell(c1); table.addCell(expense.voucher); total += expense.amount; } preface.add(table); preface.add(new Paragraph(" ")); preface.add(new Paragraph("Total: " + DECIMAL_FORMAT.format(total), titleFont)); document.add(preface); i = 0; for (Expense expense : expenses) { i++; if (expense.receipt == null) continue; document.newPage(); Paragraph receipt = new Paragraph(); receipt.add(new Paragraph("Attachment " + i, titleFont)); document.add(receipt); Image image = Image.getInstance(new URL(expense.receipt)); image.setRotationDegrees(-90); float scaler = (document.getPageSize().getWidth() / image.getWidth()) * 100; image.scalePercent(scaler); document.add(image); } document.close(); stream.close(); return stream.getSize(); }
From source file:com.mycompany.mavenproject1.Createpdf.java
private void generateLayout(Document doc, PdfContentByte cb) { try {/* ww w .jav a2 s . c o m*/ cb.setLineWidth(1f); // Invoice Header box layout cb.rectangle(420, 700, 150, 60); cb.moveTo(420, 720); cb.lineTo(570, 720); cb.moveTo(420, 740); cb.lineTo(570, 740); cb.moveTo(480, 700); cb.lineTo(480, 760); cb.stroke(); // Invoice Header box Text Headings createHeadings(cb, 422, 743, "Account No."); createHeadings(cb, 422, 723, "Invoice No."); createHeadings(cb, 422, 703, "Invoice Date"); // Invoice Detail box layout cb.rectangle(20, 50, 550, 600); cb.moveTo(20, 630); cb.lineTo(570, 630); cb.moveTo(50, 50); cb.lineTo(50, 650); cb.moveTo(150, 50); cb.lineTo(150, 650); cb.moveTo(430, 50); cb.lineTo(430, 650); cb.moveTo(500, 50); cb.lineTo(500, 650); cb.stroke(); // Invoice Detail box Text Headings createHeadings(cb, 22, 633, "Qty"); createHeadings(cb, 52, 633, "Item Number"); createHeadings(cb, 152, 633, "Item Description"); createHeadings(cb, 432, 633, "Price"); createHeadings(cb, 502, 633, "Ext Price"); //add the images Image companyLogo = Image.getInstance("images/olympics_logo.gif"); companyLogo.setAbsolutePosition(25, 700); companyLogo.scalePercent(25); doc.add(companyLogo); } catch (DocumentException dex) { dex.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:com.pablog178.pdfcreator.android.PdfcreatorModule.java
License:Open Source License
private void generateiTextPDFfunction(HashMap args) { if (args.containsKey("fileName")) { Object fileName = args.get("fileName"); if (fileName instanceof String) { this.fileName = (String) fileName; Log.i(PROXY_NAME, "fileName: " + this.fileName); }//ww w .java2 s. c o m } else return; if (args.containsKey("view")) { Object viewObject = args.get("view"); if (viewObject instanceof TiViewProxy) { TiViewProxy viewProxy = (TiViewProxy) viewObject; this.view = viewProxy.getOrCreateView(); if (this.view == null) { Log.e(PROXY_NAME, "NO VIEW was created!!"); return; } Log.i(PROXY_NAME, "view: " + this.view.toString()); } } else return; if (args.containsKey("quality")) { this.quality = TiConvert.toInt(args.get("quality")); } if (args.containsKey("pageSize")) { Object pageSize = args.get("pageSize"); if (pageSize instanceof String) { if (pageSize.equals("letter")) { this.pageSize = PageSize.LETTER; } else if (pageSize.equals("A4")) { this.pageSize = PageSize.A4; } else { this.pageSize = PageSize.LETTER; } } } TiBaseFile file = TiFileFactory.createTitaniumFile(this.fileName, true); Log.i(PROXY_NAME, "file full path: " + file.nativePath()); try { Resources appResources = app.getResources(); OutputStream outputStream = file.getOutputStream(); final int MARGIN = 0; final float PDF_WIDTH = this.pageSize.getWidth() - MARGIN * 2; // A4: 595 //Letter: 612 final float PDF_HEIGHT = this.pageSize.getHeight() - MARGIN * 2; // A4: 842 //Letter: 792 final int DEFAULT_VIEW_WIDTH = 980; final int DEFAULT_VIEW_HEIGHT = 1384; int viewWidth = DEFAULT_VIEW_WIDTH; int viewHeight = DEFAULT_VIEW_HEIGHT; Document pdfDocument = new Document(this.pageSize, MARGIN, MARGIN, MARGIN, MARGIN); PdfWriter docWriter = PdfWriter.getInstance(pdfDocument, outputStream); Log.i(PROXY_NAME, "PDF_WIDTH: " + PDF_WIDTH); Log.i(PROXY_NAME, "PDF_HEIGHT: " + PDF_HEIGHT); WebView view = (WebView) this.view.getNativeView(); if (TiApplication.isUIThread()) { viewWidth = view.capturePicture().getWidth(); viewHeight = view.capturePicture().getHeight(); if (viewWidth <= 0) { viewWidth = DEFAULT_VIEW_WIDTH; } if (viewHeight <= 0) { viewHeight = DEFAULT_VIEW_HEIGHT; } } else { Log.e(PROXY_NAME, "NO UI THREAD"); viewWidth = DEFAULT_VIEW_WIDTH; viewHeight = DEFAULT_VIEW_HEIGHT; } view.setLayerType(View.LAYER_TYPE_SOFTWARE, null); Log.i(PROXY_NAME, "viewWidth: " + viewWidth); Log.i(PROXY_NAME, "viewHeight: " + viewHeight); float scaleFactorWidth = 1 / ((float) viewWidth / PDF_WIDTH); float scaleFactorHeight = 1 / ((float) viewHeight / PDF_HEIGHT); Log.i(PROXY_NAME, "scaleFactorWidth: " + scaleFactorWidth); Log.i(PROXY_NAME, "scaleFactorHeight: " + scaleFactorHeight); Bitmap viewBitmap = Bitmap.createBitmap(viewWidth, viewHeight, Bitmap.Config.ARGB_8888); Canvas viewCanvas = new Canvas(viewBitmap); // Paint paintAntialias = new Paint(); // paintAntialias.setAntiAlias(true); // paintAntialias.setFilterBitmap(true); Drawable bgDrawable = view.getBackground(); if (bgDrawable != null) { bgDrawable.draw(viewCanvas); } else { viewCanvas.drawColor(Color.WHITE); } view.draw(viewCanvas); TiBaseFile pdfImg = createTempFile(); // ByteArrayOutputStream stream = new ByteArrayOutputStream(32); // viewBitmap.compress(Bitmap.CompressFormat.JPEG, this.quality, stream); viewBitmap.compress(Bitmap.CompressFormat.JPEG, this.quality, pdfImg.getOutputStream()); FileInputStream pdfImgInputStream = new FileInputStream(pdfImg.getNativeFile()); byte[] pdfImgBytes = IOUtils.toByteArray(pdfImgInputStream); pdfImgInputStream.close(); // ByteBuffer buffer = ByteBuffer.allocate(viewBitmap.getByteCount()); // viewBitmap.copyPixelsToBuffer(buffer); pdfDocument.open(); float yFactor = viewHeight * scaleFactorWidth; int pageNumber = 1; do { if (pageNumber > 1) { pdfDocument.newPage(); } pageNumber++; yFactor -= PDF_HEIGHT; Image pageImage = Image.getInstance(pdfImgBytes, true); // Image pageImage = Image.getInstance(buffer.array()); pageImage.scalePercent(scaleFactorWidth * 100); pageImage.setAbsolutePosition(0f, -yFactor); pdfDocument.add(pageImage); Log.i(PROXY_NAME, "yFactor: " + yFactor); } while (yFactor > 0); pdfDocument.close(); sendCompleteEvent(); } catch (Exception exception) { Log.e(PROXY_NAME, "Error: " + exception.toString()); sendErrorEvent(exception.toString()); } }
From source file:com.propelics.pdfcreator.PdfcreatorModule.java
License:Open Source License
private void generateiTextPDFfunction(HashMap args) { Log.d(PROXY_NAME, "generateiTextPDFfunction()"); String filename = ""; TiUIView webview = null;/*ww w .j a va2 s. co m*/ int quality = 100; try { if (args.containsKey("filename")) { filename = (String) args.get("filename"); Log.d(PROXY_NAME, "filename: " + filename); } else return; if (args.containsKey("webview")) { TiViewProxy viewProxy = (TiViewProxy) args.get("webview"); webview = viewProxy.getOrCreateView(); Log.d(PROXY_NAME, "webview: " + webview.toString()); } else return; if (args.containsKey("quality")) { quality = TiConvert.toInt(args.get("quality")); } TiBaseFile file = TiFileFactory.createTitaniumFile(filename, true); Log.d(PROXY_NAME, "file full path: " + file.nativePath()); OutputStream outputStream = file.getOutputStream(); final int MARGIN = 0; final float PDF_WIDTH = PageSize.LETTER.getWidth() - MARGIN * 2; // A4: 595 //Letter: 612 final float PDF_HEIGHT = PageSize.LETTER.getHeight() - MARGIN * 2; // A4: 842 //Letter: 792 final int DEFAULT_VIEW_WIDTH = 980; final int DEFAULT_VIEW_HEIGHT = 1384; int viewWidth = DEFAULT_VIEW_WIDTH; int viewHeight = DEFAULT_VIEW_HEIGHT; Document pdfDocument = new Document(PageSize.LETTER, MARGIN, MARGIN, MARGIN, MARGIN); PdfWriter docWriter = PdfWriter.getInstance(pdfDocument, outputStream); Log.d(PROXY_NAME, "PDF_WIDTH: " + PDF_WIDTH); Log.d(PROXY_NAME, "PDF_HEIGHT: " + PDF_HEIGHT); WebView view = (WebView) webview.getNativeView(); if (TiApplication.isUIThread()) { viewWidth = view.capturePicture().getWidth(); viewHeight = view.capturePicture().getHeight(); if (viewWidth <= 0) { viewWidth = DEFAULT_VIEW_WIDTH; } if (viewHeight <= 0) { viewHeight = DEFAULT_VIEW_HEIGHT; } } else { Log.e(PROXY_NAME, "NO UI THREAD"); viewWidth = DEFAULT_VIEW_WIDTH; viewHeight = DEFAULT_VIEW_HEIGHT; } view.setLayerType(View.LAYER_TYPE_SOFTWARE, null); Log.d(PROXY_NAME, "viewWidth: " + viewWidth); Log.d(PROXY_NAME, "viewHeight: " + viewHeight); float scaleFactorWidth = 1 / (viewWidth / PDF_WIDTH); float scaleFactorHeight = 1 / (viewHeight / PDF_HEIGHT); Log.d(PROXY_NAME, "scaleFactorWidth: " + scaleFactorWidth); Log.d(PROXY_NAME, "scaleFactorHeight: " + scaleFactorHeight); Bitmap viewBitmap = Bitmap.createBitmap(viewWidth, viewHeight, Bitmap.Config.ARGB_8888); Canvas viewCanvas = new Canvas(viewBitmap); // Paint paintAntialias = new Paint(); // paintAntialias.setAntiAlias(true); // paintAntialias.setFilterBitmap(true); Drawable bgDrawable = view.getBackground(); if (bgDrawable != null) { bgDrawable.draw(viewCanvas); } else { viewCanvas.drawColor(Color.WHITE); } view.draw(viewCanvas); TiBaseFile pdfImg = createTempFile(filename); viewBitmap.compress(Bitmap.CompressFormat.PNG, quality, pdfImg.getOutputStream()); FileInputStream pdfImgInputStream = new FileInputStream(pdfImg.getNativeFile()); byte[] pdfImgBytes = IOUtils.toByteArray(pdfImgInputStream); pdfImgInputStream.close(); pdfDocument.open(); float yFactor = viewHeight * scaleFactorWidth; int pageNumber = 1; do { if (pageNumber > 1) { pdfDocument.newPage(); } pageNumber++; yFactor -= PDF_HEIGHT; Image pageImage = Image.getInstance(pdfImgBytes, true); // Image pageImage = Image.getInstance(buffer.array()); pageImage.scalePercent(scaleFactorWidth * 100); pageImage.setAbsolutePosition(0f, -yFactor); pdfDocument.add(pageImage); Log.d(PROXY_NAME, "yFactor: " + yFactor); } while (yFactor > 0); pdfDocument.close(); sendCompleteEvent(filename); } catch (Exception exception) { sendErrorEvent(exception); } }
From source file:com.semfapp.adamdilger.semf.Pdf.java
License:Open Source License
private void createPDFNew(String filePath, String htmlString, @Nullable ArrayList<ImageFile> images) { File file = null;/* w w w . j a v a 2 s. c om*/ try { file = new File(filePath); // step 1 Document document = new Document(); // step 2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file)); writer.setInitialLeading(12.5f); // step 3 document.open(); // step 4 // CSS CSSResolver cssResolver = new StyleAttrCSSResolver(); CssFile cssFile = XMLWorkerHelper.getCSS(activity.getAssets().open("styles.css")); cssResolver.addCss(cssFile); // HTML HtmlPipelineContext htmlContext = new HtmlPipelineContext(null); htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory()); // Pipelines PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer); HtmlPipeline html = new HtmlPipeline(htmlContext, pdf); CssResolverPipeline css = new CssResolverPipeline(cssResolver, html); // XML Worker XMLWorker worker = new XMLWorker(css, true); XMLParser p = new XMLParser(worker); Drawable d = activity.getResources().getDrawable(R.drawable.logo_icon); Bitmap bitmap = ((BitmapDrawable) d).getBitmap(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] bitmapData = stream.toByteArray(); Image image = Image.getInstance(bitmapData); image.setAbsolutePosition(35, 763); image.scalePercent(12); document.add(image); p.parse(new ByteArrayInputStream(htmlString.getBytes(StandardCharsets.UTF_8))); if (images != null) { System.out.println("Adding IMage"); for (int x = 0; x < images.size(); x++) { Image cursor = images.get(x).getImage(); float ratio = cursor.getPlainHeight() / cursor.getPlainWidth(); float imgWidth = document.getPageSize().getWidth() - 100; float imgHeight = document.getPageSize().getHeight() - 100; cursor.scaleToFit(new Rectangle(imgWidth, imgHeight)); document.add(cursor); } } // step 5 document.close(); } catch (Exception e) { e.printStackTrace(); } }