List of usage examples for com.lowagie.text Rectangle getHeight
public float getHeight()
From source file:it.flavianopetrocchi.jpdfbookmarks.itextbookmarksconverter.iTextBookmarksConverter.java
License:Open Source License
private String makeBookmarkParam(PdfReader reader, Bookmark bookmark, PdfArray dest, IntHashtable pages) { StringBuilder s = new StringBuilder(); PdfObject obj = dest.getPdfObject(0); if (obj.isNumber()) { s.append(((PdfNumber) obj).intValue() + 1); bookmark.setPageNumber(((PdfNumber) obj).intValue() + 1); } else {// ww w. j a v a2 s. c o m if (pages == null) { initPages(); } if (pages != null) { s.append(pages.get(getNumber((PdfIndirectReference) obj))); //changed by ujihara 2004-06-13 bookmark.setPageNumber(pages.get(getNumber((PdfIndirectReference) obj))); } } Rectangle pageSize = null; //if (bookmark.getType().equals(BookmarkType.GoToFile) == false) { //if (!bookmark.isRemoteDestination()) { pageSize = reader.getPageSize(bookmark.getPageNumber()); //} if (pageSize != null) { bookmark.setPageWidth(pageSize.getWidth()); bookmark.setPageHeight(pageSize.getHeight()); } String destType = dest.getPdfObject(1).toString(); PdfObject[] params = new PdfObject[dest.size()]; for (int k = 2; k < dest.size(); ++k) { params[k - 2] = dest.getPdfObject(k); } if (destType.equals("/XYZ")) { bookmark.setType(BookmarkType.TopLeftZoom); if (!params[0].isNull()) { bookmark.setLeft(((PdfNumber) params[0]).intValue()); } if (!params[1].isNull()) { bookmark.setTop(((PdfNumber) params[1]).intValue()); } if (pageSize != null) { bookmark.setThousandthsLeft(thousandthsHorizontal(bookmark.getLeft(), pageSize)); bookmark.setThousandthsTop(thousandthsVertical(bookmark.getTop(), pageSize)); } if (!params[2].isNull()) { bookmark.setZoom(((PdfNumber) params[2]).floatValue()); } } else if (destType.equals("/FitH")) { bookmark.setType(BookmarkType.FitWidth); if (!params[0].isNull()) { bookmark.setTop(((PdfNumber) params[0]).intValue()); } if (pageSize != null) { bookmark.setThousandthsTop(thousandthsVertical(bookmark.getTop(), pageSize)); } } else if (destType.equals("/FitV")) { bookmark.setType(BookmarkType.FitHeight); if (!params[0].isNull()) { bookmark.setLeft(((PdfNumber) params[0]).intValue()); } if (pageSize != null) { bookmark.setThousandthsLeft(thousandthsHorizontal(bookmark.getLeft(), pageSize)); } } else if (destType.equals("/FitBH")) { bookmark.setType(BookmarkType.FitContentWidth); if (!params[0].isNull()) { bookmark.setTop(((PdfNumber) params[0]).intValue()); } if (pageSize != null) { bookmark.setThousandthsTop(thousandthsVertical(bookmark.getTop(), pageSize)); } } else if (destType.equals("/FitBV")) { bookmark.setType(BookmarkType.FitContentHeight); if (!params[0].isNull()) { bookmark.setLeft(((PdfNumber) params[0]).intValue()); } if (pageSize != null) { bookmark.setThousandthsLeft(thousandthsHorizontal(bookmark.getLeft(), pageSize)); } } else if (destType.equals("/Fit")) { bookmark.setType(BookmarkType.FitPage); } else if (destType.equals("/FitB")) { bookmark.setType(BookmarkType.FitContent); } else if (destType.equals("/FitR")) { bookmark.setType(BookmarkType.FitRect); if (!params[0].isNull()) { bookmark.setLeft(((PdfNumber) params[0]).intValue()); } if (!params[1].isNull()) { bookmark.setBottom(((PdfNumber) params[1]).intValue()); } if (!params[2].isNull()) { bookmark.setRight(((PdfNumber) params[2]).intValue()); } if (!params[3].isNull()) { bookmark.setTop(((PdfNumber) params[3]).intValue()); } if (pageSize != null) { bookmark.setThousandthsLeft(thousandthsHorizontal(bookmark.getLeft(), pageSize)); bookmark.setThousandthsTop(thousandthsVertical(bookmark.getTop(), pageSize)); bookmark.setThousandthsRight(thousandthsHorizontal(bookmark.getRight(), pageSize)); bookmark.setThousandthsBottom(thousandthsVertical(bookmark.getBottom(), pageSize)); } } s.append(' ').append(dest.getPdfObject(1).toString().substring(1)); for (int k = 2; k < dest.size(); ++k) { s.append(' ').append(dest.getPdfObject(k).toString()); } return s.toString(); }
From source file:it.flavianopetrocchi.jpdfbookmarks.itextbookmarksconverter.iTextBookmarksConverter.java
License:Open Source License
public static int thousandthsVertical(int y, Rectangle pageRect) { int thousandths = -1; if (y != -1) { try {/* w ww .j a v a 2 s . c o m*/ thousandths = 1000 - Math.round(y * 1000.0f / pageRect.getHeight()); } catch (Exception exc) { } } return thousandths; }
From source file:it.flavianopetrocchi.jpdfbookmarks.itextbookmarksconverter.iTextBookmarksConverter.java
License:Open Source License
@Override public float getPageHeight(int pageNumber) { Rectangle pageSize = reader.getPageSize(pageNumber); return pageSize.getHeight(); }
From source file:jdbreport.model.io.pdf.itext2.PdfWriter.java
License:Apache License
private PdfPCell writeCell(ReportModel model, jdbreport.model.Cell srcCell, int row, int col) throws BadElementException, IOException, SaveReportException { CellStyle style = model.getStyles(srcCell.getStyleId()); java.awt.Rectangle rect = model.getCellRect(row, col, true, true); float h = Math.round((float) Units.PT.setYPixels((int) rect.getHeight())); float w = Math.round((float) Units.PT.setXPixels((int) rect.getWidth())); PdfPCell pdfCell = null;// w w w . ja va 2 s. c om if (srcCell.getPicture() != null) { java.awt.Image awtImage = srcCell.getPicture().getImage(); com.lowagie.text.Image image = awtImageToImage(awtImage, srcCell, w, h); pdfCell = new PdfPCell(image); } else { String text = null; if (srcCell.getValue() instanceof CellValue<?>) { StringWriter strWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(strWriter); if (!((CellValue<?>) srcCell.getValue()).write(printWriter, model, row, col, this, ReportBook.PDF)) { java.awt.Image awtImage = ((CellValue<?>) srcCell.getValue()).getAsImage(model, row, col); if (awtImage != null) { com.lowagie.text.Image image = awtImageToImage(awtImage, srcCell, w, h); pdfCell = new PdfPCell(image); } } else { text = strWriter.getBuffer().toString(); } } else { if (jdbreport.model.Cell.TEXT_HTML.equals(srcCell.getContentType())) { pdfCell = new PdfPCell(); writeHTMLText(model.getStyles(srcCell.getStyleId()), srcCell, pdfCell); } else { text = model.getCellText(srcCell); } } if (pdfCell == null) { pdfCell = new PdfPCell(); } if (text != null && text.length() > 0) { com.lowagie.text.Font font; if (fonts.containsKey(style.getId())) { font = fonts.get(style.getId()); } else { font = getFontMapper().styleToPdf(style); fonts.put(style.getId(), font); } Paragraph p; if (font != null) { p = new Paragraph(text, font); } else { p = new Paragraph(text); } pdfCell.setPhrase(p); pdfCell.setPadding(1); pdfCell.setLeading(0f, 1.1f); } else { pdfCell.setPadding(0); } } pdfCell.setFixedHeight(h); pdfCell.setBackgroundColor(style.getBackground()); pdfCell.setHorizontalAlignment(toPdfHAlignment(style.getHorizontalAlignment())); pdfCell.setVerticalAlignment(toPdfVAlignment(style.getVerticalAlignment())); if (style.getAngle() != 0) { pdfCell.setRotation(roundAngle(style.getAngle())); } assignBorders(style, pdfCell); pdfCell.setNoWrap(!style.isWrapLine()); if (srcCell.getColSpan() > 0) { pdfCell.setColspan(srcCell.getColSpan() + 1); } if (srcCell.getRowSpan() > 0) { pdfCell.setRowspan(srcCell.getRowSpan() + 1); } return pdfCell; }
From source file:mpv5.utils.export.PDFFile.java
License:Open Source License
private void setImage(PdfStamper stamper, String key, java.awt.Image oimg) { try {// w ww.j a va2 s.c o m Log.Debug(this, "Write Image.." + key); float[] photograph = acroFields.getFieldPositions(key); Rectangle rect = new Rectangle(photograph[1], photograph[2], photograph[3], photograph[4]); Image img = Image.getInstance(oimg, null); img.setAbsolutePosition(photograph[1] + (rect.getWidth() - img.getScaledWidth()) / 2, photograph[2] + (rect.getHeight() - img.getScaledHeight()) / 2); PdfContentByte cb = stamper.getOverContent((int) photograph[0]); cb.addImage(img); } catch (Exception iOException) { Log.Debug(iOException); } }
From source file:net.laubenberger.bogatyr.helper.HelperPdf.java
License:Open Source License
/** * Writes a PDF from multiple image files to a {@link File}. * //from w ww . ja v a 2 s. com * @param pageSize * of the PDF * @param scale * images to fit the page size * @param file * output as PDF * @param files * for the PDF * @throws DocumentException * @throws IOException * @see File * @see Rectangle * @since 0.9.2 */ public static void writePdfFromImages(final Rectangle pageSize, final boolean scale, final File file, final File... files) throws DocumentException, IOException { // $JUnit$ if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(pageSize, scale, file, files)); if (null == pageSize) { throw new RuntimeExceptionIsNull("pageSize"); //$NON-NLS-1$ } if (null == file) { throw new RuntimeExceptionIsNull("file"); //$NON-NLS-1$ } if (null == files) { throw new RuntimeExceptionIsNull("files"); //$NON-NLS-1$ } if (!HelperArray.isValid(files)) { throw new RuntimeExceptionIsEmpty("files"); //$NON-NLS-1$ } final Document document = new Document(pageSize); document.setMargins(0.0F, 0.0F, 0.0F, 0.0F); final FilterOutputStream fos = new BufferedOutputStream(new FileOutputStream(file)); try { PdfWriter.getInstance(document, fos); document.open(); for (final File inputFile : files) { if (null == inputFile) { throw new RuntimeExceptionIsNull("inputFile"); //$NON-NLS-1$ } final Image image = Image.getInstance(inputFile.getAbsolutePath()); if (scale) { image.scaleToFit(pageSize.getWidth(), pageSize.getHeight()); } document.add(image); document.newPage(); } } finally { document.close(); fos.close(); } if (log.isDebugEnabled()) log.debug(HelperLog.methodExit()); }
From source file:net.laubenberger.bogatyr.helper.HelperPdf.java
License:Open Source License
/** * Writes a PDF from multiple {@link java.awt.Image} to a {@link File}. * // www . j a v a 2s. co m * @param pageSize * of the PDF * @param scale * images to fit the page size * @param file * output as PDF * @param images * for the PDF * @throws DocumentException * @throws IOException * @see File * @see java.awt.Image * @see Rectangle * @since 0.9.2 */ public static void writePdfFromImages(final Rectangle pageSize, final boolean scale, final File file, final java.awt.Image... images) throws DocumentException, IOException { // $JUnit$ if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(pageSize, scale, file, images)); if (null == pageSize) { throw new RuntimeExceptionIsNull("pageSize"); //$NON-NLS-1$ } if (null == file) { throw new RuntimeExceptionIsNull("file"); //$NON-NLS-1$ } if (null == images) { throw new RuntimeExceptionIsNull("images"); //$NON-NLS-1$ } if (!HelperArray.isValid(images)) { throw new RuntimeExceptionIsEmpty("images"); //$NON-NLS-1$ } final Document document = new Document(pageSize); document.setMargins(0.0F, 0.0F, 0.0F, 0.0F); final FilterOutputStream fos = new BufferedOutputStream(new FileOutputStream(file)); try { PdfWriter.getInstance(document, fos); document.open(); for (final java.awt.Image tempImage : images) { if (null == tempImage) { throw new RuntimeExceptionIsNull("tempImage"); //$NON-NLS-1$ } final Image image = Image.getInstance(tempImage, null); if (scale) { image.scaleToFit(pageSize.getWidth(), pageSize.getHeight()); } document.add(image); document.newPage(); } } finally { document.close(); fos.close(); } if (log.isDebugEnabled()) log.debug(HelperLog.methodExit()); }
From source file:net.laubenberger.wichtel.helper.HelperPdf.java
License:Open Source License
/** * Writes a PDF from multiple image files to a {@link File}. * /*w ww .ja v a 2s . com*/ * @param pageSize * of the PDF * @param scale * images to fit the page size * @param file * output as PDF * @param files * for the PDF * @throws DocumentException * @throws IOException * @see File * @see Rectangle * @since 0.0.1 */ public static void writePdfFromImages(final Rectangle pageSize, final boolean scale, final File file, final File... files) throws DocumentException, IOException { // $JUnit$ if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(pageSize, scale, file, files)); if (null == pageSize) { throw new RuntimeExceptionIsNull("pageSize"); //$NON-NLS-1$ } if (null == file) { throw new RuntimeExceptionIsNull("file"); //$NON-NLS-1$ } if (null == files) { throw new RuntimeExceptionIsNull("files"); //$NON-NLS-1$ } if (!HelperArray.isValid(files)) { throw new RuntimeExceptionIsEmpty("files"); //$NON-NLS-1$ } final Document document = new Document(pageSize); document.setMargins(0.0F, 0.0F, 0.0F, 0.0F); try (FilterOutputStream fos = new BufferedOutputStream(new FileOutputStream(file))) { PdfWriter.getInstance(document, fos); document.open(); for (final File inputFile : files) { if (null == inputFile) { throw new RuntimeExceptionIsNull("inputFile"); //$NON-NLS-1$ } final Image image = Image.getInstance(inputFile.getAbsolutePath()); if (scale) { image.scaleToFit(pageSize.getWidth(), pageSize.getHeight()); } document.add(image); document.newPage(); } document.close(); } if (log.isDebugEnabled()) log.debug(HelperLog.methodExit()); }
From source file:net.laubenberger.wichtel.helper.HelperPdf.java
License:Open Source License
/** * Writes a PDF from multiple {@link java.awt.Image} to a {@link File}. * /*from w w w . ja va2s. c om*/ * @param pageSize * of the PDF * @param scale * images to fit the page size * @param file * output as PDF * @param images * for the PDF * @throws DocumentException * @throws IOException * @see File * @see java.awt.Image * @see Rectangle * @since 0.0.1 */ public static void writePdfFromImages(final Rectangle pageSize, final boolean scale, final File file, final java.awt.Image... images) throws DocumentException, IOException { // $JUnit$ if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(pageSize, scale, file, images)); if (null == pageSize) { throw new RuntimeExceptionIsNull("pageSize"); //$NON-NLS-1$ } if (null == file) { throw new RuntimeExceptionIsNull("file"); //$NON-NLS-1$ } if (null == images) { throw new RuntimeExceptionIsNull("images"); //$NON-NLS-1$ } if (!HelperArray.isValid(images)) { throw new RuntimeExceptionIsEmpty("images"); //$NON-NLS-1$ } final Document document = new Document(pageSize); document.setMargins(0.0F, 0.0F, 0.0F, 0.0F); try (FilterOutputStream fos = new BufferedOutputStream(new FileOutputStream(file))) { PdfWriter.getInstance(document, fos); document.open(); for (final java.awt.Image tempImage : images) { if (null == tempImage) { throw new RuntimeExceptionIsNull("tempImage"); //$NON-NLS-1$ } final Image image = Image.getInstance(tempImage, null); if (scale) { image.scaleToFit(pageSize.getWidth(), pageSize.getHeight()); } document.add(image); document.newPage(); } } if (log.isDebugEnabled()) log.debug(HelperLog.methodExit()); }
From source file:net.refractions.udig.printing.ui.internal.PdfPrintingEngine.java
License:Open Source License
public boolean printToPdf() { Dimension paperSize = page.getPaperSize(); Dimension pageSize = page.getSize(); float xScale = (float) paperSize.width / (float) pageSize.width; float yScale = (float) paperSize.height / (float) pageSize.height; Rectangle paperRectangle = new Rectangle(paperSize.width, paperSize.height); Document document = new Document(paperRectangle, 0f, 0f, 0f, 0f); try {// w w w . j a v a2 s . co m PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputPdfFile)); document.open(); PdfContentByte cb = writer.getDirectContent(); Graphics2D graphics = cb.createGraphics(paperRectangle.getWidth(), paperRectangle.getHeight()); // BufferedImage bI = new BufferedImage((int) paperRectangle.width(), (int) // paperRectangle // .height(), BufferedImage.TYPE_INT_ARGB); // Graphics graphics2 = bI.getGraphics(); List<Box> boxes = page.getBoxes(); for (Box box : boxes) { String id = box.getID(); System.out.println(id); Point boxLocation = box.getLocation(); if (boxLocation == null) continue; int x = boxLocation.x; int y = boxLocation.y; Dimension size = box.getSize(); if (size == null) continue; int w = size.width; int h = size.height; float newX = xScale * (float) x; float newY = yScale * (float) y; float newW = xScale * (float) w; float newH = yScale * (float) h; box.setSize(new Dimension((int) newW, (int) newH)); box.setLocation(new Point((int) newX, (int) newY)); Graphics2D boxGraphics = (Graphics2D) graphics.create((int) newX, (int) newY, (int) newW, (int) newH); BoxPrinter boxPrinter = box.getBoxPrinter(); boxPrinter.draw(boxGraphics, monitor); } graphics.dispose(); // ImageIO.write(bI, "png", new File("c:\\Users\\moovida\\Desktop\\test.png")); // graphics.drawImage(bI, null, 0, 0); document.newPage(); document.close(); writer.close(); } catch (DocumentException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } return true; }