List of usage examples for com.lowagie.text Image setAbsolutePosition
public void setAbsolutePosition(float absoluteX, float absoluteY)
Image
. From source file:com.ikon.util.DocConverter.java
License:Open Source License
/** * TIFF to PDF conversion/*w w w . j ava2 s .c o m*/ */ public void tiff2pdf(File input, File output) throws ConversionException { RandomAccessFileOrArray ra = null; Document doc = null; try { // Open PDF doc = new Document(); PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(output)); PdfContentByte cb = writer.getDirectContent(); doc.open(); //int pages = 0; // Open TIFF ra = new RandomAccessFileOrArray(input.getPath()); int comps = TiffImage.getNumberOfPages(ra); for (int c = 0; c < comps; ++c) { Image img = TiffImage.getTiffImage(ra, c + 1); if (img != null) { log.debug("tiff2pdf - page {}", c + 1); if (img.getScaledWidth() > 500 || img.getScaledHeight() > 700) { img.scaleToFit(500, 700); } img.setAbsolutePosition(20, 20); //doc.add(new Paragraph("page " + (c + 1))); cb.addImage(img); doc.newPage(); //++pages; } } } catch (FileNotFoundException e) { throw new ConversionException("File not found: " + e.getMessage(), e); } catch (DocumentException e) { throw new ConversionException("Document exception: " + e.getMessage(), e); } catch (IOException e) { throw new ConversionException("IO exception: " + e.getMessage(), e); } finally { if (ra != null) { try { ra.close(); } catch (IOException e) { // Ignore } } if (doc != null) { doc.close(); } } }
From source file:com.ikon.util.PDFUtils.java
License:Open Source License
public static void stampImage(InputStream input, byte[] image, int layer, float opacity, String exprX, String exprY, OutputStream output) throws FileNotFoundException, DocumentException, EvalError, IOException { log.debug("stampImage({}, {}, {}, {}, {}, {}, {})", new Object[] { input, image, Integer.valueOf(layer), Float.valueOf(opacity), exprX, exprY, output }); Image img = Image.getInstance(image); PdfReader reader = new PdfReader(input); PdfStamper stamper = new PdfStamper(reader, output); PdfGState gs = new PdfGState(); gs.setFillOpacity(opacity);//from w w w . jav a2 s . c o m gs.setStrokeOpacity(opacity); int numPages = reader.getNumberOfPages(); int count = 0; while (count++ < numPages) { int pageCenter = (int) reader.getPageSizeWithRotation(count).getWidth() / 2; int pageMiddle = (int) reader.getPageSizeWithRotation(count).getHeight() / 2; Interpreter i = new Interpreter(); i.set("IMAGE_WIDTH", (int) img.getWidth()); i.set("IMAGE_HEIGHT", (int) img.getHeight()); i.set("PAGE_WIDTH", (int) reader.getPageSizeWithRotation(count).getWidth()); i.set("PAGE_HEIGHT", (int) reader.getPageSizeWithRotation(count).getHeight()); i.set("PAGE_CENTER", pageCenter); i.set("PAGE_MIDDLE", pageMiddle); Integer evalX = (Integer) i.eval(exprX); Integer evalY = (Integer) i.eval(exprY); if (evalX == null) evalX = Integer.valueOf(pageCenter); if (evalY == null) { evalY = Integer.valueOf(pageMiddle); } log.debug("evalX: {}", evalX); log.debug("evalY: {}", evalY); img.setAbsolutePosition(evalX.intValue(), evalY.intValue()); PdfContentByte cb = null; if (layer == LAYER_UNDER_CONTENT) cb = stamper.getUnderContent(count); else if (layer == LAYER_OVER_CONTENT) cb = stamper.getOverContent(count); else { throw new IllegalArgumentException(); } cb.saveState(); cb.setGState(gs); cb.addImage(img); cb.restoreState(); } stamper.close(); }
From source file:com.openkm.util.DocConverter.java
License:Open Source License
/** * TIFF to PDF conversion/* w w w. jav a2 s . c o m*/ */ public void tiff2pdf(File input, File output) throws ConversionException { RandomAccessFileOrArray ra = null; Document doc = null; try { // Open PDF doc = new Document(); PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(output)); PdfContentByte cb = writer.getDirectContent(); doc.open(); // int pages = 0; // Open TIFF ra = new RandomAccessFileOrArray(input.getPath()); int comps = TiffImage.getNumberOfPages(ra); for (int c = 0; c < comps; ++c) { Image img = TiffImage.getTiffImage(ra, c + 1); if (img != null) { log.debug("tiff2pdf - page {}", c + 1); if (img.getScaledWidth() > 500 || img.getScaledHeight() > 700) { img.scaleToFit(500, 700); } img.setAbsolutePosition(20, 20); // doc.add(new Paragraph("page " + (c + 1))); cb.addImage(img); doc.newPage(); // ++pages; } } } catch (FileNotFoundException e) { throw new ConversionException("File not found: " + e.getMessage(), e); } catch (DocumentException e) { throw new ConversionException("Document exception: " + e.getMessage(), e); } catch (IOException e) { throw new ConversionException("IO exception: " + e.getMessage(), e); } finally { if (ra != null) { try { ra.close(); } catch (IOException e) { // Ignore } } if (doc != null) { doc.close(); } } }
From source file:com.orange.atk.atkUI.corecli.utils.PdfUtilities.java
License:Apache License
/** * Adds a logo to the given pdf file./*from w w w .j ava 2 s .c o m*/ * @param pdfFileName * @param imageFileName * @param x position for image * @param y position for image * @throws Exception */ public void addWatermark(String pdfFileName, String imageFileName, int x, int y) throws Exception { // 1. copy File tmpPDFFile = new File(tmpDir, "tmpPDF.pdf"); copyFile(new File(pdfFileName), tmpPDFFile); // 2. add watermark // we create a reader for a certain document PdfReader reader = new PdfReader(tmpPDFFile.getAbsolutePath()); int n = reader.getNumberOfPages(); // we create a stamper that will copy the document to a new file PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(pdfFileName)); // adding content to each page int i = 0; PdfContentByte under; Image img = Image.getInstance(imageFileName); img.setAbsolutePosition(x, y); while (i < n) { i++; // watermark under the existing page under = stamp.getUnderContent(i); under.addImage(img); } // closing PdfStamper will generate the new PDF file stamp.close(); }
From source file:convert.Convertings.java
public void convertTif2PDF(String tifPath, String path) { System.out.println("one"); String arg[] = { tifPath };//from w w w . j a v a2 s.c o m System.out.println("one2"); if (arg.length < 1) { System.out.println("Usage: Tiff2Pdf file1.tif [file2.tif ... fileN.tif]"); System.exit(1); } String tiff; String pdf; System.out.println("two"); for (int i = 0; i < arg.length; i++) { tiff = arg[i]; pdf = path + ".pdf"; Document document = new Document(PageSize.LETTER, 0, 0, 0, 0); try { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdf)); int pages = 0; document.open(); PdfContentByte cb = writer.getDirectContent(); RandomAccessFileOrArray ra = null; int comps = 0; try { ra = new RandomAccessFileOrArray(tiff); comps = TiffImage.getNumberOfPages(ra); } catch (Throwable e) { System.out.println("Exception in " + tiff + " " + e.getMessage()); continue; } System.out.println("Processing: " + tiff); for (int c = 0; c < comps; ++c) { try { Image img = TiffImage.getTiffImage(ra, c + 1); if (img != null) { System.out.println("page " + (c + 1)); System.out.println("img.getDpiX() : " + img.getDpiX()); System.out.println("img.getDpiY() : " + img.getDpiY()); img.scalePercent(6200f / img.getDpiX(), 6200f / img.getDpiY()); //img.scalePercent(img.getDpiX(), img.getDpiY()); //document.setPageSize(new Rectangle(img.getScaledWidth(), img.getScaledHeight())); img.setAbsolutePosition(0, 0); cb.addImage(img); document.newPage(); ++pages; } } catch (Throwable e) { System.out.println("Exception " + tiff + " page " + (c + 1) + " " + e.getMessage()); } } ra.close(); document.close(); } catch (Throwable e) { e.printStackTrace(); } System.out.println("done"); } }
From source file:corner.orm.tapestry.pdf.components.PdfImage.java
License:Apache License
/** * @see corner.orm.tapestry.pdf.components.AbstractPdfComponent#renderPdf(corner.orm.tapestry.pdf.PdfWriterDelegate, com.lowagie.text.Document) *///from w ww. j a va 2 s . c o m @Override public void renderPdf(PdfWriterDelegate writer, Document doc) { Defense.notNull(getRectangle(), "TextField?"); String[] p = getRectangle().split(","); try { Image image = Image.getInstance(getValue()); image.setAbsolutePosition(Float.valueOf(p[0]), Float.valueOf(p[1]));// ? if (getAutoSize()) { if (Constants.PDFIMAGE_COMPONENT_SCALE_TYPE_BORDER.equals(getScaleType())) { image.scaleAbsolute((Float.valueOf(p[2]) - Float.valueOf(p[0])), (Float.valueOf(p[3]) - Float.valueOf(p[1])));// ? } else { float picHeight = image.plainHeight();// float picWeight = image.plainWidth();// float borderHeight = Float.valueOf(p[3]) - Float.valueOf(p[1]);// float borderWeight = Float.valueOf(p[2]) - Float.valueOf(p[0]);// float heightPer = borderHeight / picHeight * 100; float widthPer = borderWeight / picWeight * 100; // ???? if (heightPer > widthPer) { image.scalePercent(widthPer); } else { image.scalePercent(heightPer); } } } doc.add(image); } catch (BadElementException e) { throw new CornerSystemException(e); } catch (MalformedURLException e) { throw new CornerSystemException(e); } catch (IOException e) { throw new CornerSystemException(e); } catch (DocumentException e) { throw new CornerSystemException(e); } }
From source file:Cotizacion.ExportarPDF.java
private static void acomodarImagenPie(Paragraph imagen) throws BadElementException { try {/*from ww w. j a v a 2s . c o m*/ Image im = Image.getInstance("src/Imagenes/abajo.png"); im.setAbsolutePosition(0, 0); im.setAlignment(Image.ALIGN_CENTER); im.scalePercent(60f); imagen.add(im); } catch (Exception e) { System.out.print(e.getMessage()); } }
From source file:cz.incad.kramerius.pdf.impl.AbstractPDFRenderSupport.java
License:Open Source License
public static ScaledImageOptions insertJavaImage(Document document, float percentage, BufferedImage javaImg) throws IOException, BadElementException, MalformedURLException, DocumentException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); writeImageToStream(javaImg, "jpeg", bos); com.lowagie.text.Image img = com.lowagie.text.Image.getInstance(bos.toByteArray()); Float ratio = ratio(document, percentage, javaImg); int fitToPageWidth = (int) (javaImg.getWidth(null) * ratio); int fitToPageHeight = (int) (javaImg.getHeight(null) * ratio); int offsetX = ((int) document.getPageSize().getWidth() - fitToPageWidth) / 2; int offsetY = ((int) document.getPageSize().getHeight() - fitToPageHeight) / 2; img.scaleAbsoluteHeight(ratio * img.getHeight()); img.scaleAbsoluteWidth(ratio * img.getWidth()); img.setAbsolutePosition((offsetX), document.getPageSize().getHeight() - offsetY - (ratio * img.getHeight())); document.add(img);/*from w ww .j a v a2s . com*/ ScaledImageOptions options = new ScaledImageOptions(); options.setXdpi(img.getDpiX()); options.setYdpi(img.getDpiY()); options.setXoffset(offsetX); options.setYoffset(offsetY); options.setWidth(fitToPageWidth); options.setHeight(fitToPageHeight); options.setScaleFactor(ratio); return options; }
From source file:de.cuseb.bilderbuch.pdf.PdfController.java
License:Open Source License
@RequestMapping(value = "/pdf", method = RequestMethod.GET) public void generatePdf(HttpSession session, HttpServletResponse httpServletResponse) { try {/*from w w w . j a v a 2 s. c o m*/ PdfRequest pdfRequest = (PdfRequest) session.getAttribute("pdfRequest"); httpServletResponse.setContentType("application/pdf"); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, httpServletResponse.getOutputStream()); writer.setDefaultColorspace(PdfName.COLORSPACE, PdfName.DEFAULTRGB); //document.addAuthor(pdfRequest.getAuthor()); //document.addTitle(pdfRequest.getTitle()); document.setPageSize( new Rectangle(Utilities.millimetersToPoints(156), Utilities.millimetersToPoints(148))); document.open(); FontFactory.defaultEmbedding = true; FontFactory.register("IndieRock.ttf", "IndieRock"); Font font = FontFactory.getFont("IndieRock"); BaseFont baseFont = font.getBaseFont(); PdfContentByte cb = writer.getDirectContent(); Iterator<PdfPage> pages = pdfRequest.getPages().iterator(); while (pages.hasNext()) { PdfPage page = pages.next(); if (page.getImage() != null) { Image image = Image.getInstance(new URL(page.getImage().getUrl())); image.setDpi(300, 300); image.setAbsolutePosition(0f, 0f); image.scaleAbsolute(document.getPageSize().getWidth(), document.getPageSize().getHeight()); document.add(image); cb.saveState(); cb.beginText(); cb.setColorFill(Color.WHITE); cb.moveText(10f, 10f); cb.setFontAndSize(baseFont, 18); cb.showText(page.getSentence()); cb.endText(); cb.restoreState(); if (pages.hasNext()) { document.newPage(); } } } document.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:de.intranda.test_ics.ImageHelper.java
License:Apache License
@SuppressWarnings("unused") private void addPage(File imageFile, PdfWriter pdfWriter, Document pdfDocument, float shrinkRatio, float rotationDegree) throws DocumentException, IOException { float pointsPerInch = 200.0f; Image pageImage = null; float pageImageHeight = 0, pageImageWidth = 0; boolean lowMemory = (shrinkRatio == 1 ? false : true); URL inputImage = imageFile.toURI().toURL(); pdfWriter.setFullCompression();/*from w w w . j av a 2 s.c o m*/ pdfWriter.setStrictImageSequence(true); pdfWriter.setLinearPageMode(); LOGGER.debug("Out of memory on loading image for pdf generation"); // ByteArrayOutputStream stream = new ByteArrayOutputStream(); BufferedImage bitmap = ImageIO.read(imageFile); // LOGGER.debug( "Size of temporary image bitmap: Width = " + bitmap.getWidth() + "; Height = " + bitmap.getHeight()); LOGGER.debug("Reading file " + imageFile.getAbsolutePath()); pageImage = Image.getInstance(bitmap, null, false); bitmap.flush(); // stream.close(); pageImage.setRotationDegrees(-rotationDegree); LOGGER.debug("Image dimensions: Width = " + pageImage.getWidth() + "; Height = " + pageImage.getHeight()); pageImageHeight = pageImage.getHeight(); pageImageWidth = pageImage.getWidth(); pageImage.setAbsolutePosition(0, 0); // Rectangle pageRect = new Rectangle(pageImageWidth/shrinkRatio, pageImageHeight/shrinkRatio); com.lowagie.text.Rectangle pageRect = new com.lowagie.text.Rectangle(pageImageWidth, pageImageHeight); LOGGER.debug("Creating rectangle: Width = " + pageRect.getWidth() + "; Height = " + pageRect.getHeight()); pdfDocument.setPageSize(pageRect); if (pdfDocument.isOpen()) { pdfDocument.newPage(); pdfWriter.getDirectContent().addImage(pageImage); } else { pdfDocument.open(); pdfWriter.getDirectContent().addImage(pageImage); } pdfWriter.flush(); System.gc(); }