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:com.gtdfree.test.TableBorders.java

License:Open Source License

/**
 * Demonstrates different borderstyles./*from w ww  . ja  va 2  s  .  co m*/
 * 
 * @param args
 *            the number of rows for each table fragment.
 */
public static void main(String[] args) {

    System.out.println("Table Borders");
    // step1
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {
        // step2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TableBorders.pdf"));
        // step3
        document.open();
        // step4

        // page 1
        Font tableFont = FontFactory.getFont("Helvetica", 8, Font.BOLD, Color.BLACK);
        float padding = 0f;
        Rectangle border = new Rectangle(0f, 0f);
        border.setBorderWidthLeft(6f);
        border.setBorderWidthBottom(5f);
        border.setBorderWidthRight(4f);
        border.setBorderWidthTop(2f);
        border.setBorderColorLeft(Color.RED);
        border.setBorderColorBottom(Color.ORANGE);
        border.setBorderColorRight(Color.YELLOW);
        border.setBorderColorTop(Color.GREEN);
        makeTestPage(tableFont, border, writer, document, padding, true, true);
        Font font = FontFactory.getFont("Helvetica", 10);
        Paragraph p;
        p = new Paragraph("\nVarious border widths and colors\nuseAscender=true, useDescender=true", font);
        document.add(p);

        document.newPage();

        // page 2
        padding = 2f;
        border = new Rectangle(0f, 0f);
        border.setBorderWidthLeft(1f);
        border.setBorderWidthBottom(2f);
        border.setBorderWidthRight(1f);
        border.setBorderWidthTop(2f);
        border.setBorderColor(Color.BLACK);
        makeTestPage(tableFont, border, writer, document, padding, true, true);
        p = new Paragraph(
                "More typical use - padding of 2\nuseBorderPadding=true, useAscender=true, useDescender=true",
                font);
        document.add(p);

        document.newPage();

        // page 3
        padding = 0f;
        border = new Rectangle(0f, 0f);
        border.setBorderWidthLeft(1f);
        border.setBorderWidthBottom(2f);
        border.setBorderWidthRight(1f);
        border.setBorderWidthTop(2f);
        border.setBorderColor(Color.BLACK);
        makeTestPage(tableFont, border, writer, document, padding, false, true);
        p = new Paragraph("\nuseBorderPadding=true, useAscender=false, useDescender=true", font);
        document.add(p);

        document.newPage();

        // page 4
        padding = 0f;
        border = new Rectangle(0f, 0f);
        border.setBorderWidthLeft(1f);
        border.setBorderWidthBottom(2f);
        border.setBorderWidthRight(1f);
        border.setBorderWidthTop(2f);
        border.setBorderColor(Color.BLACK);
        makeTestPage(tableFont, border, writer, document, padding, false, false);
        p = new Paragraph("\nuseBorderPadding=true, useAscender=false, useDescender=false", font);
        document.add(p);

        document.newPage();

        // page 5
        padding = 0f;
        border = new Rectangle(0f, 0f);
        border.setBorderWidthLeft(1f);
        border.setBorderWidthBottom(2f);
        border.setBorderWidthRight(1f);
        border.setBorderWidthTop(2f);
        border.setBorderColor(Color.BLACK);
        makeTestPage(tableFont, border, writer, document, padding, true, false);
        p = new Paragraph("\nuseBorderPadding=true, useAscender=true, useDescender=false", font);
        document.add(p);
    } catch (Exception de) {
        de.printStackTrace();
    }
    // step5
    document.close();
}

From source file:com.ideaspymes.proyecttemplate.stock.web.ProductoConsultaBean.java

public String createPdf() throws IOException, DocumentException {

    EtiquetaConf conf = etiquetaConfDAO.getEtiquetaConfDefault();

    if (hayParaImprimir() && conf != null) {

        HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance()
                .getExternalContext().getResponse();
        response.setContentType("application/x-pdf");
        response.setHeader("Content-Disposition", "attachment; filename=\"etiquetas.pdf\"");

        float ancho = Utilities.millimetersToPoints(conf.getAnchoHoja().floatValue());
        System.out.println("Ancho: " + ancho);
        float largo = Utilities.millimetersToPoints(conf.getLargoHoja().floatValue());
        System.out.println("Alto: " + largo);

        // step 1
        Document document = new Document(new Rectangle(ancho, largo));
        // step 2

        document.setMargins(0f, 0f, 0f, 0f);

        PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
        // step 3
        document.open();/* ww  w .jav a 2  s . c om*/

        // step 4
        PdfContentByte cb = writer.getDirectContent();

        for (Producto p : getLista()) {
            if (p.getCantidadEtiquetas() > 0) {

                PdfPTable table = new PdfPTable(2);
                table.setWidthPercentage(96);

                Font fontbold = FontFactory.getFont("Times-Roman", 8, Font.NORMAL);

                Chunk productTitle = new Chunk("HC", fontbold);

                Paragraph pTitile = new Paragraph(productTitle);
                pTitile.setAlignment(Element.ALIGN_LEFT);
                pTitile.setLeading(6, 0);

                PdfPCell cellTitle = new PdfPCell();
                cellTitle.setHorizontalAlignment(Element.ALIGN_LEFT);
                cellTitle.setVerticalAlignment(Element.ALIGN_TOP);
                cellTitle.setBorder(Rectangle.NO_BORDER);
                cellTitle.addElement(pTitile);

                Font font2 = FontFactory.getFont("Times-Roman", conf.getTamDescripcion().floatValue(),
                        Font.NORMAL);

                Chunk productName = new Chunk(p.getNombre(), font2);

                Paragraph pName = new Paragraph(productName);
                pName.setAlignment(Element.ALIGN_CENTER);
                //pTitile.setLeading(0, 1);

                cellTitle.addElement(pName);

                table.addCell(cellTitle);

                Barcode39 code39 = new Barcode39();
                code39.setCode(p.getCodigo());
                //code39.setCodeType(Barcode.EAN13);
                code39.setBarHeight(conf.getAltoCodBarra().floatValue());
                //codeEan.setX(0.7f);
                code39.setSize(conf.getTamDescripcion().floatValue());
                //code39.setAltText("HC - " + p.getNombre());

                PdfPCell cellBarcode = new PdfPCell();
                cellBarcode.setHorizontalAlignment(Element.ALIGN_CENTER);
                cellBarcode.setBorder(Rectangle.NO_BORDER);
                cellBarcode.addElement(code39.createImageWithBarcode(cb, null, Color.BLACK));

                table.addCell(cellBarcode);

                for (int i = 0; i < p.getCantidadEtiquetas(); i++) {
                    document.add(table);
                    document.newPage();
                }
            }
        }

        // step 5
        document.close();

        response.getOutputStream().flush();
        response.getOutputStream().close();
        FacesContext.getCurrentInstance().responseComplete();
    } else {
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_ERROR, "No hay nada que imprimir", ""));
    }
    return null;
}

From source file:com.isdemu.controller.TBS_UsuarioController.java

@RequestMapping(value = "/codigo_barra", method = RequestMethod.POST)
@ResponseBody/*  w w w  .  j  a v a 2s  . c o m*/
public String codigo(@RequestBody String codigos) throws FileNotFoundException, DocumentException {

    JSONObject array = new JSONObject(codigos);

    Document document = new Document();

    File file = new File(this.getClass().getResource("/codigoBarraIsdemu.pdf").getFile());

    String absolutePath = file.getAbsolutePath();

    absolutePath = absolutePath.replaceAll("%20", " ");

    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(absolutePath));

    Rectangle one = new Rectangle(76, 35);
    document.setPageSize(one);
    document.setMargins(2, 2, 2, 2);

    document.open();

    Barcode128 code128 = new Barcode128();
    code128.setGenerateChecksum(true);

    JSONArray object = array.getJSONArray("Inventario");
    for (int i = 0; i < object.length(); i++) {
        JSONObject object2 = object.getJSONObject(i);

        //JSONArray object = array.getJSONArray("Inventario");
        String id = object2.getString("idInv");

        code128.setCode(id);
        document.add(code128.createImageWithBarcode(writer.getDirectContent(), null, null));

        document.newPage();

    }

    document.close();

    return "Almacenado";

}

From source file:com.mirth.connect.connectors.doc.DocumentDispatcher.java

License:Open Source License

private void createRTF(InputStream inputStream, OutputStream outputStream, DocumentDispatcherProperties props)
        throws Exception {
    com.lowagie.text.Document document = null;

    try {//from   w  w w . j  a va2 s.  co  m
        document = new com.lowagie.text.Document();
        //TODO verify the character encoding

        RtfWriter2.getInstance(document, outputStream);

        document.open();

        try {
            double width = Double.parseDouble(props.getPageWidth());
            double height = Double.parseDouble(props.getPageHeight());
            Unit unit = props.getPageUnit();

            /*
             * The version of iText being used only accepts points, so we need to convert to
             * twips first and then convert to points (1 point = 20 twips).
             */
            if (unit != Unit.TWIPS) {
                width = unit.convertTo(width, Unit.TWIPS);
                height = unit.convertTo(height, Unit.TWIPS);
                unit = Unit.TWIPS;
            }
            width = Math.max(width, 1);
            height = Math.max(height, 1);
            document.setPageSize(new Rectangle((float) (Math.round(width) / RtfBasicElement.TWIPS_FACTOR),
                    (float) (Math.round(height) / RtfBasicElement.TWIPS_FACTOR)));
        } catch (Exception e) {
        }

        HtmlParser parser = new HtmlParser();
        parser.go(document, inputStream);
    } finally {
        if (document != null) {
            document.close();
        }
    }
}

From source file:com.moss.check.us.CheckPdfRenderer.java

License:Open Source License

public void render(CheckModel model, OutputStream out) throws Exception {

    Document document = new Document();
    document.setPageSize(new Rectangle(PAGE_WIDTH, PAGE_HEIGHT));

    PdfWriter writer = PdfWriter.getInstance(document, out);
    document.open();// w  w w. j  av  a  2 s  .  c  o m

    PdfContentByte cb = writer.getDirectContent();

    Check check = new Check();
    check.defaultFont = BaseFont.createFont("Helvetica", BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    check.defaultFontSize = 8;
    check.defaultFontLeading = 10;
    check.largeFontSize = 9;
    check.largeFontLeading = 12;
    check.fixedWidthFont = createFixedFont();
    check.fixedWidthFontSize = 8;
    check.voidFont = BaseFont.createFont("Helvetica-Bold", BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    check.voidFontSize = 14;
    check.micrFont = createMicrFont();
    check.micrFontSize = 12;
    check.model = model;
    check.x = 0;
    check.y = 0;
    check.renderMode = CheckRenderMode.CHECK;

    check.render(cb);

    if (StubPrintMode.CHECK_DUPLICATE == model.stubPrintMode) {
        check.renderMode = CheckRenderMode.STUB;
        check.y = document.top() - (8.2f * POINTS_IN_A_CM);
        check.render(cb);
    } else if (StubPrintMode.CUSTOM == model.stubPrintMode) {
        PdfReader reader = new PdfReader(model.customStubPdf);
        PdfImportedPage customPage = writer.getImportedPage(reader, 1);
        cb.addTemplate(customPage, 0f, 0f);
    } else {
        throw new RuntimeException("Unknown stub print mode: " + model.stubPrintMode);
    }

    document.close();
}

From source file:com.rapidminer.gui.actions.export.ImageExporter.java

License:Open Source License

private void exportVectorGraphics(String formatName, File outputFile) throws ImageExportException {
    Component component = printableComponent.getExportComponent();
    int width = component.getWidth();
    int height = component.getHeight();
    try (FileOutputStream fs = new FileOutputStream(outputFile)) {
        switch (formatName) {
        case PDF:
            // create pdf document with slightly increased width and height
            // (otherwise the image gets cut off)
            Document document = new Document(new Rectangle(width + 5, height + 5));
            PdfWriter writer = PdfWriter.getInstance(document, fs);
            document.open();//from ww  w. j a  v a2 s.co  m
            PdfContentByte cb = writer.getDirectContent();
            PdfTemplate tp = cb.createTemplate(width, height);
            Graphics2D g2 = tp.createGraphics(width, height, new DefaultFontMapper());
            component.print(g2);
            g2.dispose();
            cb.addTemplate(tp, 0, 0);
            document.close();
            break;
        case SVG:
            exportFreeHep(component, fs, new SVGGraphics2D(fs, new Dimension(width, height)));
            break;
        case EPS:
            exportFreeHep(component, fs, new PSGraphics2D(fs, new Dimension(width, height)));
            break;
        default:
            // cannot happen
            break;
        }
    } catch (Exception e) {
        throw new ImageExportException(
                I18N.getMessage(I18N.getUserErrorMessagesBundle(), "error.image_export.export_failed"), e);
    }
}

From source file:com.umlet.control.io.GenPdf.java

License:Open Source License

public void createPdfToStream(OutputStream ostream, DiagramHandler handler) {
    try {/* w w w.  ja  va  2s.c  om*/
        // We get the Rectangle of our DrawPanel
        java.awt.Rectangle bounds = handler.getDrawPanel().getContentBounds(Constants.PRINTPADDING);
        // and create an iText specific Rectangle from (0,0) to (width,height) in which we draw the diagram
        Rectangle drawSpace = new Rectangle((float) bounds.getWidth(), (float) bounds.getHeight());

        // Create document in which we write the pdf
        Document document = new Document(drawSpace);
        PdfWriter writer = PdfWriter.getInstance(document, ostream);
        document.open();

        PdfContentByte cb = writer.getDirectContent();
        Graphics2D graphics2d = cb.createGraphics(drawSpace.getWidth(), drawSpace.getHeight());

        // We shift the diagram to the upper left corner, so we shift it by (minX,minY) of the contextBounds
        Dimension trans = new Dimension((int) bounds.getMinX(), (int) bounds.getMinY());
        graphics2d.translate(-trans.getWidth(), -trans.getHeight());

        handler.getDrawPanel().paintEntitiesIntoGraphics2D(graphics2d);

        graphics2d.dispose();
        document.close();

    } catch (Exception e) {
        System.out.println("UMLet: Error: Exception in outputPdf: " + e);
    }
}

From source file:cz.incad.kramerius.pdf.utils.pdf.DocumentUtils.java

License:Open Source License

public static Document createDocument(PreparedDocument document) {
    int width = document.getWidth();
    int height = document.getHeight();
    Document doc = new Document(new Rectangle(width, height));
    return doc;//from  ww  w. ja v a2 s . co  m
}

From source file:datasoul.servicelist.ServiceListExporterSlides.java

License:Open Source License

public ServiceListExporterSlides(String filename, int width, int height)
        throws FileNotFoundException, DocumentException {
    document = new Document();
    deleteOnDispose = new LinkedList<String>();
    slideCount = 0;/*  w  w  w  .j av a  2  s . co m*/

    // ensure file do not exist to avoid garbage at the end of the file
    File f = new File(filename);
    if (f.exists()) {
        f.delete();
    }
    PdfWriter.getInstance(document, new FileOutputStream(filename));

    document.setMargins(0, 0, 0, 0);

    document.setPageSize(new Rectangle(width, height));
    document.open();
    document.addCreator("Datasoul " + DatasoulMainForm.getVersion());
    document.addCreationDate();

    render = new ExporterContentRender(width, height, new DummyContentDisplay());

}

From source file:de.atomfrede.tools.evalutation.tools.plot.util.PlotUtil.java

License:Open Source License

protected static void writeChartAsPDF(OutputStream out, JFreeChart chart, int width, int height,
        FontMapper mapper) throws IOException {
    Rectangle pagesize = new Rectangle(width, height);
    Document document = new Document(pagesize, 50, 50, 50, 50);
    try {/*www. j av a 2  s.c  o  m*/
        PdfWriter writer = PdfWriter.getInstance(document, out);
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate tp = cb.createTemplate(width, height);
        Graphics2D g2 = tp.createGraphics(width, height, mapper);
        Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height);
        chart.draw(g2, r2D);
        g2.dispose();
        cb.addTemplate(tp, 0, 0);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    }
    document.close();
}