Example usage for com.lowagie.text Rectangle Rectangle

List of usage examples for com.lowagie.text Rectangle Rectangle

Introduction

In this page you can find the example usage for com.lowagie.text Rectangle Rectangle.

Prototype

public Rectangle(float urx, float ury) 

Source Link

Document

Constructs a Rectangle -object starting from the origin (0, 0).

Usage

From source file:org.locationtech.udig.project.ui.wizard.export.image.Image2Pdf.java

License:Open Source License

/**
 * writes a buffered image to pdf at a given resolution
 *
 *
 * @param image//w ww .j av a 2s.c o  m
 *            the image to write
 * @param pdfPath
 *            the path to the pdf document to create
 * @param paper
 *            the paper type
 * @param widthBorder
 *            border in pixels to use on the x-axis
 * @param heightBorder
 *            border in pixels to use on the y-axis
 * @param lanscape
 *            true if the document should be in landscape mode
 * @param dpi the output dpi
 */
public static void write(BufferedImage image, String pdfPath, Paper paper, int widthBorder, int heightBorder,
        boolean landscape, int dpi) {
    Dimension printPageSize = null;
    printPageSize = new Dimension(paper.getPixelWidth(landscape, dpi), paper.getPixelHeight(landscape, dpi));

    // step 1: creation of a document-object
    Document document = new Document(new Rectangle(printPageSize.width, printPageSize.height));

    try {

        // step 2:
        // we create a writer that listens to the document
        // and directs a PDF-stream to a file
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfPath));

        // step 3: we open the document
        document.open();

        // step 4: we create a template and a Graphics2D object that
        // corresponds with it
        int w = printPageSize.width;
        int h = printPageSize.height;
        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate tp = cb.createTemplate(w, h);
        Graphics2D g2 = tp.createGraphics(w, h);
        tp.setWidth(w);
        tp.setHeight(h);

        g2.drawImage(image, null, widthBorder, heightBorder);

        g2.dispose();
        cb.addTemplate(tp, 0, 0);

    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }

    // step 5: we close the document
    document.close();
}

From source file:org.openswing.swing.export.java.ExportToPDF14.java

License:Open Source License

/**
 * @param opt export options/*from  w w w .j  a  v a 2 s  . co  m*/
 * @return Excel document, as byte array
 */
public byte[] getDocument(ExportOptions opt) throws Throwable {

    //    Document document = new Document(new Rectangle(total+30,PageSize.A4.height()));
    Document document = new Document(new Rectangle(PageSize.A4.width(), PageSize.A4.height()));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter w = PdfWriter.getInstance(document, baos);
    document.open();

    Object obj = null;
    for (int i = 0; i < opt.getComponentsExportOptions().size(); i++) {
        obj = opt.getComponentsExportOptions().get(i);
        processComponent(null, 0, document, opt, obj);
    }

    document.close();
    return baos.toByteArray();
}

From source file:org.openswing.swing.export.java.ExportToPDF15.java

License:Open Source License

/**
 * @param opt export options// w  ww  .  jav  a  2s  .  c o  m
 * @return Excel document, as byte array
 */
public byte[] getDocument(ExportOptions opt) throws Throwable {

    //    Document document = new Document(new Rectangle(total+30,PageSize.A4.height()));
    Document document = new Document(new Rectangle(PageSize.A4.getWidth(), PageSize.A4.getHeight()));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter w = PdfWriter.getInstance(document, baos);
    document.open();

    Object obj = null;
    for (int i = 0; i < opt.getComponentsExportOptions().size(); i++) {
        obj = opt.getComponentsExportOptions().get(i);
        processComponent(null, 0, document, opt, obj);
    }

    document.close();
    return baos.toByteArray();
}

From source file:org.oss.pdfreporter.pdf.Document.java

License:Open Source License

public Document(String fileName, int width, int height) throws DocumentException {
    this(new com.lowagie.text.Document(new Rectangle(width, height)), fileName);
}

From source file:org.oss.pdfreporter.pdf.Document.java

License:Open Source License

@Override
public IPage newPage(PageOrientation orientation, int width, int height) {
    return newPage(orientation, new Rectangle(width, height));
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfDocumentWriter.java

License:Open Source License

public void processPhysicalPage(final PageGrid pageGrid, final LogicalPageBox logicalPage, final int row,
        final int col, final PhysicalPageKey pageKey) throws DocumentException {
    final PhysicalPageBox page = pageGrid.getPage(row, col);
    if (page == null) {
        return;//from   w  ww .j av  a2 s .c o  m
    }

    final float width = (float) StrictGeomUtility.toExternalValue(page.getWidth());
    final float height = (float) StrictGeomUtility.toExternalValue(page.getHeight());

    final Rectangle pageSize = new Rectangle(width, height);

    final float marginLeft = (float) StrictGeomUtility.toExternalValue(page.getImageableX());
    final float marginRight = (float) StrictGeomUtility
            .toExternalValue(page.getWidth() - page.getImageableWidth() - page.getImageableX());
    final float marginTop = (float) StrictGeomUtility.toExternalValue(page.getImageableY());
    final float marginBottom = (float) StrictGeomUtility
            .toExternalValue(page.getHeight() - page.getImageableHeight() - page.getImageableY());

    final Document document = getDocument();
    document.setPageSize(pageSize);
    document.setMargins(marginLeft, marginRight, marginTop, marginBottom);

    if (awaitOpenDocument) {
        document.open();
        awaitOpenDocument = false;
    }

    final PdfContentByte directContent = writer.getDirectContent();
    final Graphics2D graphics = new PdfGraphics2D(directContent, width, height, metaData);
    final PdfLogicalPageDrawable logicalPageDrawable = createLogicalPageDrawable(logicalPage, page);
    final PhysicalPageDrawable drawable = createPhysicalPageDrawable(logicalPageDrawable, page);
    drawable.draw(graphics, new Rectangle2D.Double(0, 0, width, height));

    graphics.dispose();

    document.newPage();
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfDocumentWriter.java

License:Open Source License

public void processLogicalPage(final LogicalPageKey key, final LogicalPageBox logicalPage)
        throws DocumentException {

    final float width = (float) StrictGeomUtility.toExternalValue(logicalPage.getPageWidth());
    final float height = (float) StrictGeomUtility.toExternalValue(logicalPage.getPageHeight());

    final Rectangle pageSize = new Rectangle(width, height);

    final Document document = getDocument();
    document.setPageSize(pageSize);//from w w  w .  j a va  2s. c o  m
    document.setMargins(0, 0, 0, 0);

    if (awaitOpenDocument) {
        document.open();
        awaitOpenDocument = false;
    }

    final Graphics2D graphics = new PdfGraphics2D(writer.getDirectContent(), width, height, metaData);
    // and now process the box ..
    final PdfLogicalPageDrawable logicalPageDrawable = createLogicalPageDrawable(logicalPage, null);
    logicalPageDrawable.draw(graphics, new Rectangle2D.Double(0, 0, width, height));

    graphics.dispose();

    document.newPage();
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.table.rtf.helper.RTFPrinter.java

License:Open Source License

/**
 * @noinspection IOResourceOpenedButNotSafelyClosed
 *///  w w  w  .  j  av a  2 s  .  com
public void print(final LogicalPageKey logicalPageKey, final LogicalPageBox logicalPage,
        final TableContentProducer contentProducer, final RTFOutputProcessorMetaData metaData,
        final boolean incremental) throws ContentProcessingException {
    final int startRow = contentProducer.getFinishedRows();
    final int finishRow = contentProducer.getFilledRows();
    if (incremental && startRow == finishRow) {
        return;
    }

    if (document == null) {
        this.cellBackgroundProducer = new CellBackgroundProducer(
                metaData.isFeatureSupported(AbstractTableOutputProcessor.TREAT_ELLIPSE_AS_RECTANGLE),
                metaData.isFeatureSupported(OutputProcessorFeature.UNALIGNED_PAGEBANDS));

        final PhysicalPageBox pageFormat = logicalPage.getPageGrid().getPage(0, 0);
        final float urx = (float) StrictGeomUtility.toExternalValue(pageFormat.getWidth());
        final float ury = (float) StrictGeomUtility.toExternalValue(pageFormat.getHeight());

        final float marginLeft = (float) StrictGeomUtility.toExternalValue(pageFormat.getImageableX());
        final float marginRight = (float) StrictGeomUtility.toExternalValue(
                pageFormat.getWidth() - pageFormat.getImageableWidth() - pageFormat.getImageableX());
        final float marginTop = (float) StrictGeomUtility.toExternalValue(pageFormat.getImageableY());
        final float marginBottom = (float) StrictGeomUtility.toExternalValue(
                pageFormat.getHeight() - pageFormat.getImageableHeight() - pageFormat.getImageableY());
        final Rectangle pageSize = new Rectangle(urx, ury);

        document = new Document(pageSize, marginLeft, marginRight, marginTop, marginBottom);
        imageCache = new RTFImageCache(resourceManager);

        // rtf does not support PageFormats or other meta data...
        final PatchRtfWriter2 instance = PatchRtfWriter2.getInstance(document,
                new NoCloseOutputStream(outputStream));
        instance.getDocumentSettings().setAlwaysUseUnicode(true);

        final String author = config
                .getConfigProperty("org.pentaho.reporting.engine.classic.core.modules.output.table.rtf.Author");
        if (author != null) {
            document.addAuthor(author);
        }

        final String title = config
                .getConfigProperty("org.pentaho.reporting.engine.classic.core.modules.output.table.rtf.Title");
        if (title != null) {
            document.addTitle(title);
        }

        document.addProducer();
        document.addCreator(RTFPrinter.CREATOR);

        try {
            document.addCreationDate();
        } catch (Exception e) {
            RTFPrinter.logger.debug("Unable to add creation date. It will have to work without it.", e);
        }

        document.open();
    }

    // Start a new page.
    try {
        final SheetLayout sheetLayout = contentProducer.getSheetLayout();
        final int columnCount = contentProducer.getColumnCount();
        if (table == null) {
            final int rowCount = contentProducer.getRowCount();
            table = new Table(columnCount, rowCount);
            table.setAutoFillEmptyCells(false);
            table.setWidth(100); // span the full page..
            // and finally the content ..

            final float[] cellWidths = new float[columnCount];
            for (int i = 0; i < columnCount; i++) {
                cellWidths[i] = (float) StrictGeomUtility.toExternalValue(sheetLayout.getCellWidth(i, i + 1));
            }
            table.setWidths(cellWidths);
        }

        // logger.debug ("Processing: " + startRow + " " + finishRow + " " + incremental);

        for (int row = startRow; row < finishRow; row++) {
            for (short col = 0; col < columnCount; col++) {
                final RenderBox content = contentProducer.getContent(row, col);
                final CellMarker.SectionType sectionType = contentProducer.getSectionType(row, col);

                if (content == null) {
                    final RenderBox backgroundBox = contentProducer.getBackground(row, col);
                    final CellBackground background;
                    if (backgroundBox != null) {
                        background = cellBackgroundProducer.getBackgroundForBox(logicalPage, sheetLayout, col,
                                row, 1, 1, true, sectionType, backgroundBox);
                    } else {
                        background = cellBackgroundProducer.getBackgroundAt(logicalPage, sheetLayout, col, row,
                                true, sectionType);
                    }
                    if (background == null) {
                        // An empty cell .. ignore
                        final PatchRtfCell cell = new PatchRtfCell();
                        cell.setBorderWidth(0);
                        cell.setMinimumHeight(
                                (float) StrictGeomUtility.toExternalValue(sheetLayout.getRowHeight(row)));
                        table.addCell(cell, row, col);
                        continue;
                    }

                    // A empty cell with a defined background ..
                    final PatchRtfCell cell = new PatchRtfCell();
                    cell.setBorderWidth(0);
                    cell.setMinimumHeight(
                            (float) StrictGeomUtility.toExternalValue(sheetLayout.getRowHeight(row)));
                    updateCellStyle(cell, background);
                    table.addCell(cell, row, col);
                    continue;
                }

                if (content.isCommited() == false) {
                    throw new InvalidReportStateException("Uncommited content encountered");
                }

                final long contentOffset = contentProducer.getContentOffset(row, col);
                final long colPos = sheetLayout.getXPosition(col);
                final long rowPos = sheetLayout.getYPosition(row);
                if (content.getX() != colPos || (content.getY() + contentOffset) != rowPos) {
                    // A spanned cell ..
                    continue;
                }

                final int colSpan = sheetLayout.getColSpan(col, content.getX() + content.getWidth());
                final int rowSpan = sheetLayout.getRowSpan(row,
                        content.getY() + content.getHeight() + contentOffset);

                final CellBackground realBackground = cellBackgroundProducer.getBackgroundForBox(logicalPage,
                        sheetLayout, col, row, colSpan, rowSpan, false, sectionType, content);

                final PatchRtfCell cell = new PatchRtfCell();
                cell.setRowspan(rowSpan);
                cell.setColspan(colSpan);
                cell.setBorderWidth(0);
                cell.setMinimumHeight((float) StrictGeomUtility.toExternalValue(sheetLayout.getRowHeight(row)));
                if (realBackground != null) {
                    updateCellStyle(cell, realBackground);
                }

                computeCellStyle(content, cell);

                // export the cell and all content ..
                final RTFTextExtractor etx = new RTFTextExtractor(metaData);
                etx.compute(content, cell, imageCache);

                table.addCell(cell, row, col);
                content.setFinishedTable(true);
                // logger.debug("set Finished to cell (" + col + ", " + row + "," + content.getName() + ")");
            }

        }

        if (incremental == false) {
            document.add(table);
            table = null;
        }
    } catch (DocumentException e) {
        throw new ContentProcessingException("Failed to generate RTF-Document", e);
    }
}

From source file:org.posterita.core.CrossTabReportGenerator.java

License:Open Source License

protected Rectangle getDocumentDimension() {
    Document document = new Document(PAGE_SIZE, MARGIN, MARGIN, MARGIN, MARGIN);

    String longestText = "";
    int columnCount = 0;
    Object[] obj = null;//from  w w w .j  a  v  a 2 s .  com

    //Getting column count
    obj = (Object[]) dataSource.get(0);
    columnCount = obj.length;

    //Getting longest text
    Iterator iter = dataSource.iterator();

    while (iter.hasNext()) {
        obj = (Object[]) iter.next();

        String header = obj[0].toString();

        if (header.length() > longestText.length()) {
            longestText = header;
        }
    }

    //setting the table width
    Chunk dataChk = new Chunk("9999", DATA_FONT);
    Chunk headerChk = new Chunk(longestText, HEADER_FONT);

    float dataChkLength = dataChk.getWidthPoint() + 2 * CELLPADDING;
    float headerChkLength = headerChk.getWidthPoint() + 2 * CELLPADDING;

    float tableWidth = headerChkLength + dataChkLength * columnCount;

    float actualTableWidth = document.getPageSize().getWidth() - 2 * MARGIN;
    //float actualTableHeight = document.getPageSize().height() - 2*MARGIN;

    //if the table size is greater than that of the page we should 
    //scale the page

    if (tableWidth > actualTableWidth) {
        float documentWidth = document.getPageSize().getWidth();
        float documentHeight = document.getPageSize().getHeight();

        float newDocumentWidth = tableWidth + 2 * MARGIN;
        float newDocumentHeight = (documentHeight * newDocumentWidth) / documentWidth;

        return new Rectangle(newDocumentWidth, newDocumentHeight);
    }

    return document.getPageSize();
}

From source file:org.posterita.core.SimpleReportGenerator.java

License:Open Source License

protected Rectangle getDocumentDimension() {
    if (dataSource == null)
        return PAGE_SIZE;

    Iterator iter = dataSource.iterator();

    int columnCount = 0;

    Object[] obj = null;//from  ww  w  . ja v  a2 s  .  c o m

    if (iter.hasNext()) {
        obj = (Object[]) iter.next();

        columnCount = obj.length;
        columnWidth = new float[columnCount];

        for (int i = 0; i < columnCount; i++) {
            columnWidth[i] = new Chunk(obj[i].toString(), HEADER_FONT).getWidthPoint();
        }
    }

    while (iter.hasNext()) {
        obj = (Object[]) iter.next();

        columnCount = obj.length;

        for (int i = 0; i < columnCount; i++) {
            if (obj[i] == null)
                obj[i] = "";

            float dataWidth = new Chunk(obj[i].toString(), DATA_FONT).getWidthPoint();

            if (dataWidth > columnWidth[i])
                columnWidth[i] = dataWidth;
        }
    }

    float tableWidth = 0.0f;

    for (int j = 0; j < columnWidth.length; j++) {
        tableWidth += (columnWidth[j] + 2 * CELLPADDING);
    }

    float actualTableWidth = PAGE_SIZE.getWidth() - 2 * MARGIN;
    //float actualTableHeight = PAGE_SIZE.height() - 2*MARGIN;

    //if the table size is greater than that of the page we should 
    //scale the page

    if (tableWidth > actualTableWidth) {
        float documentWidth = PAGE_SIZE.getWidth();
        float documentHeight = PAGE_SIZE.getHeight();

        float newDocumentWidth = tableWidth + 2 * MARGIN;
        float newDocumentHeight = (documentHeight * newDocumentWidth) / documentWidth;

        return new Rectangle(newDocumentWidth, newDocumentHeight);
    } else {
        float scaleFactor = (actualTableWidth * tableWidth) / tableWidth;

        for (int k = 0; k < columnWidth.length; k++) {
            tableWidth *= scaleFactor;
        }

        return PAGE_SIZE;
    }

}