List of usage examples for com.itextpdf.text Image getHeight
public float getHeight()
From source file:com.mui.certificate.core.HalalCertification.java
License:Apache License
@Override public void exportCertificate(Certificate cert, File outputPdf) throws Exception { ByteMatrix matrix = generateQRCode(cert.getCertificateURL().toString()); int scale = sMagicNumber / matrix.getWidth(); BufferedImage buffImage = convertToImage(matrix, scale); File tempOutput = new File("tmp-qrcode.png"); ImageIO.write(buffImage, "png", tempOutput); Image itImage = Image.getInstance(tempOutput.getCanonicalPath()); PdfReader reader = new PdfReader("doc/template_certificate_halal.pdf"); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputPdf)); int centerY = 715; int centerX = 135; itImage.setAbsolutePosition(centerX - (itImage.getWidth() / 2), centerY - (itImage.getHeight() / 2)); stamper.getOverContent(1).addImage(itImage); AcroFields form1 = stamper.getAcroFields(); String nameHeader = "topmostSubform[0].Page1[0]."; form1.setField(nameHeader + "no_certificate[0]", cert.getCertificateNumber().toString()); form1.setField(nameHeader + "name_product[0]", cert.getProductName()); form1.setField(nameHeader + "type_product[0]", cert.getProductType()); form1.setField(nameHeader + "name_company[0]", cert.getCompanyName()); form1.setField(nameHeader + "company_address[0]", cert.getCompanyAddress()); Calendar cal = Calendar.getInstance(new Locale("id")); Locale idLocale = new Locale("id"); cal.setTime(cert.getIssuedDate());/* w w w .j a va2s. c o m*/ form1.setField(nameHeader + "issued_date[0]", String.format(idLocale, "%1$tA, %1$te %1$tB %1$tY", cal)); cal.setTime(cert.getValidDate()); form1.setField(nameHeader + "expired_date[0]", String.format(idLocale, "%1$tA, %1$te %1$tB %1$tY", cal)); stamper.setFormFlattening(true); stamper.close(); tempOutput.delete(); }
From source file:com.ostrichemulators.semtool.util.ExportUtility.java
License:Open Source License
public static void exportAsPdf(BufferedImage img, File pdf) throws IOException, DocumentException { final double MAX_DIM = 14400; ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(img, "PNG", baos); Image image1 = Image.getInstance(baos.toByteArray(), true); Rectangle r;//w w w . ja va2s.c om if (image1.getHeight() > MAX_DIM) { r = new Rectangle((int) image1.getWidth(), (int) MAX_DIM); } else if (image1.getWidth() > MAX_DIM) { r = new Rectangle((int) MAX_DIM, (int) image1.getHeight()); } else { r = new Rectangle((int) image1.getWidth() + 20, (int) image1.getHeight() + 20); } Document document = new Document(r, 15, 25, 15, 25); PdfWriter.getInstance(document, new FileOutputStream(pdf)); document.open(); int pages = (int) Math.ceil((double) img.getHeight() / MAX_DIM); if (pages == 0) { pages = 1; } for (int i = 0; i < pages; i++) { BufferedImage temp; if (i < pages - 1) { temp = img.getSubimage(0, i * (int) MAX_DIM, img.getWidth(), (int) MAX_DIM); } else { temp = img.getSubimage(0, i * (int) MAX_DIM, img.getWidth(), img.getHeight() % (int) MAX_DIM); } File tempFile = new File(i + Constants.PNG); ImageIO.write(temp, Constants.PNG, tempFile); Image croppedImage = Image.getInstance(i + Constants.PNG); document.add(croppedImage); tempFile.delete(); if (i < pages - 1) { document.newPage(); } } document.close(); }
From source file:com.primeleaf.krystal.util.PDFConverter.java
License:Open Source License
public File getConvertedFile(DocumentRevision documentRevision, Document document, String password) throws Exception { File tempFile = documentRevision.getDocumentFile(); if ("TIF".equalsIgnoreCase(document.getExtension()) || "TIFF".equalsIgnoreCase(document.getExtension())) { try {//from ww w . ja va 2 s .com tempFile = File.createTempFile("temp", ".PDF"); com.itextpdf.text.Document pdf = new com.itextpdf.text.Document(); PdfWriter.getInstance(pdf, new FileOutputStream(tempFile)); pdf.open(); pdf.setMargins(0, 0, 0, 0); FileInputStream fis = new FileInputStream(documentRevision.getDocumentFile()); RandomAccessFileOrArray file = new RandomAccessFileOrArray(fis); int pages = TiffImage.getNumberOfPages(file); for (int page = 1; page <= pages; page++) { Image img = TiffImage.getTiffImage(file, page); img.setAbsolutePosition(0f, 0f); img.scaleToFit(PageSize.A4.getWidth(), PageSize.A4.getHeight()); pdf.setMargins(0, 0, 0, 0); pdf.add(img); pdf.newPage(); } fis.close(); pdf.close(); document.setExtension("PDF"); } catch (Exception e) { tempFile = documentRevision.getDocumentFile(); throw new Exception("Unable to convert TIFF Document to PDF"); } } else if ("JPG".equalsIgnoreCase(document.getExtension()) || "JPEG".equalsIgnoreCase(document.getExtension()) || "PNG".equalsIgnoreCase(document.getExtension()) || "BMP".equalsIgnoreCase(document.getExtension()) || "GIF".equalsIgnoreCase(document.getExtension())) { try { tempFile = File.createTempFile("temp", ".PDF"); Image img = Image.getInstance(documentRevision.getDocumentFile().getAbsolutePath()); com.itextpdf.text.Document pdf = new com.itextpdf.text.Document( new Rectangle(img.getWidth(), img.getHeight()), 0, 0, 0, 0); img.setAbsolutePosition(0f, 0f); PdfWriter.getInstance(pdf, new FileOutputStream(tempFile)); pdf.open(); pdf.add(img); pdf.close(); document.setExtension("PDF"); } catch (Exception e) { tempFile = documentRevision.getDocumentFile(); throw new Exception("Unable to convert Image Document to PDF"); } } else if ("PDF".equalsIgnoreCase(document.getExtension())) { tempFile = documentRevision.getDocumentFile(); } else { String tempFilePath = ""; String KRYSTAL_HOME = System.getProperty("krystal.home"); if (KRYSTAL_HOME == null) { KRYSTAL_HOME = System.getProperty("user.dir"); System.setProperty("krystal.home", KRYSTAL_HOME); } tempFilePath = KRYSTAL_HOME + File.separator + "/webapps/DMC/images/unsupport.pdf"; tempFile = new File(tempFilePath); } return tempFile; }
From source file:com.qmetric.document.watermark.PdfWatermarkFactory.java
License:Open Source License
private float getYOffset(final Rectangle pageSize, final Image image) { return (float) Math.round(pageSize.getHeight() - image.getHeight()) / 2; }
From source file:com.sparksoftsolutions.com.pdfcreator.MainActivity.java
private File generatePDFFromImages(ArrayList<ImageItem> images) { Document document = new Document(); File sdcard = Environment.getExternalStorageDirectory(); File file = new File(sdcard, generateFileName()); try {/*from w ww . j a v a 2s . c o m*/ PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file)); } catch (Exception ex) { return null; } // document = new PdfDocument(); Boolean opened = false; for (ImageItem img : images) { Bitmap bitmap = BitmapFactory.decodeFile(img.path); try { Image image = Image.getInstance(img.path); document.setPageSize(new Rectangle(image.getWidth(), image.getHeight())); if (!opened) { document.open(); opened = true; } else document.newPage(); document.add(image); } catch (Exception e) { e.printStackTrace(); return null; } } document.close(); return file; }
From source file:com.vectorprint.report.itext.style.stylers.ImportTiff.java
License:Open Source License
protected void drawOnPage(com.itextpdf.text.Image img) throws VectorPrintException { /*//from w w w .jav a 2 s .c om * we call draw here to use the positioning and shadow intelligence of the AbstractPositining styler. * this styler uses the top left corner of the argument rectangle as its positioning base. * therefore we call this method with the image rectangle moved down by its height, the effect is * that the image will be positioned as expected. */ draw(new Rectangle(img.getLeft(), img.getBottom() - img.getHeight(), img.getRight(), img.getBottom()), null); }
From source file:cz.zcu.kiv.eegdatabase.logic.pdf.PDFUtils.java
License:Apache License
public Paragraph setHeader(Document document, String title) throws IOException, BadElementException { Paragraph paragraph = new Paragraph(title, FontFactory.getFont(FontFactory.HELVETICA, 14, Font.BOLD, BaseColor.BLACK)); paragraph.setAlignment(Element.ALIGN_CENTER); Image img = GetResizedAndCenteredImage(path + convertPath("/files/images/" + HEADERIMG)); float topMargin = img.getHeight() + 2 * PADDING[0]; img.setAbsolutePosition(PADDING[3], (PageSize.A4.getHeight() - img.getHeight()) - PADDING[0]); paragraph.add(img);//from ww w. jav a 2s. c o m img = GetResizedAndCenteredImage(path + convertPath("/files/images/" + FOOTERIMG)); img.setAbsolutePosition((PageSize.A4.getWidth() - img.getWidth()) / 2, PADDING[2]); paragraph.add(img); float bottomMargin = img.getHeight() + 2 * PADDING[2]; document.setMargins(topMargin, PADDING[1], bottomMargin, PADDING[3]); paragraph.setSpacingAfter(1.5f * PADDING[0]); return paragraph; }
From source file:cz.zcu.kiv.eegdatabase.logic.pdf.PDFUtils.java
License:Apache License
private Image GetResizedAndCenteredImage(String filename) throws IOException, BadElementException { Image img = Image.getInstance(filename); if (img.getWidth() <= PageSize.A4.getWidth() - PADDING[1] - PADDING[3]) { return img; }/* w w w.j ava2 s .c o m*/ float newWidth = PageSize.A4.getWidth() - PADDING[1] - PADDING[3]; float ratio = newWidth / img.getWidth(); float newHeight = img.getHeight() * ratio; img.scaleAbsolute(newWidth, newHeight); return img; }
From source file:de.jost_net.JVerein.io.Reporter.java
License:Open Source License
public void addColumn(byte[] image, int width, int height, int horizontalalignment) throws BadElementException, MalformedURLException, IOException { Image i = Image.getInstance(image); float w = i.getWidth() / width; float h = i.getHeight() / height; if (w > h) { h = i.getHeight() / w;/* w w w . j av a 2 s.com*/ w = width; } else { w = i.getHeight() / h; h = height; } i.scaleToFit(w, h); PdfPCell cell = new PdfPCell(i, false); cell.setPadding(3); cell.setHorizontalAlignment(horizontalalignment); table.addCell(cell); }
From source file:Evento.action.ZapisDoPdfAction.java
License:Apache License
public void createPDF(String[] imgURL, String place, String album) throws DocumentException { Document document = new Document(); Rectangle pageSize = new Rectangle(szerokosc, wysokosc); document.setPageSize(pageSize);// w w w .j av a2 s . co m try { PdfWriter.getInstance(document, new FileOutputStream(new File(place, "nowy.pdf"))); document.open(); Image tlo = Image.getInstance(new URL(zdjecieTla)); tlo.setAbsolutePosition(0f, 0f); document.add(tlo); Paragraph preface = new Paragraph(album, new Font(FontFamily.HELVETICA, 72, Font.BOLDITALIC, new BaseColor(255, 255, 255))); preface.setAlignment(Element.ALIGN_CENTER); document.add(preface); document.newPage(); for (int i = 0; i < imgURL.length; i++) { Image tlo2 = Image.getInstance(new URL(zdjecieTla)); tlo2.setAbsolutePosition(0f, 0f); document.add(tlo2); Image image2 = Image.getInstance(new URL(imgURL[i])); if (szerokosc * 1.5f <= image2.getWidth() || wysokosc * 1.5f <= image2.getHeight()) { image2.scaleAbsolute(image2.getWidth() * 0.25f, image2.getHeight() * 0.25f); image2.setAbsolutePosition(szerokosc / 2f - (image2.getWidth() * 0.25f) / 2, wysokosc / 2 - (image2.getHeight() * 0.25f) / 2); } else if ((szerokosc * 0.8f <= image2.getWidth() || wysokosc * 0.8f <= image2.getHeight()) && (szerokosc * 1.2f >= image2.getWidth() || wysokosc * 1.2f >= image2.getHeight())) { image2.scaleAbsolute(image2.getWidth() * 0.8f, image2.getHeight() * 0.8f); image2.setAbsolutePosition(szerokosc / 2f - (image2.getWidth() * 0.8f) / 2, wysokosc / 2 - (image2.getHeight() * 0.8f) / 2); } else if ((szerokosc * 0.4f >= image2.getWidth() || wysokosc * 0.4f >= image2.getHeight()) && (szerokosc * 0.7f <= image2.getWidth() || wysokosc * 0.7f <= image2.getHeight())) { image2.scaleAbsolute(image2.getWidth() * 1.4f, image2.getHeight() * 1.4f); image2.setAbsolutePosition(szerokosc / 2f - (image2.getWidth() * 1.4f) / 2, wysokosc / 2 - (image2.getHeight() * 1.4f) / 2); } else { image2.scaleAbsolute(image2.getWidth(), image2.getHeight()); image2.setAbsolutePosition(szerokosc / 2f - (image2.getWidth()) / 2, wysokosc / 2 - (image2.getHeight()) / 2); } for (int k = 0; k <= 1000; k++) ; for (int j = 0; j <= 1000; j++) ; document.add(image2); document.newPage(); } document.close(); } catch (Exception e) { e.printStackTrace(); } }