Example usage for com.lowagie.text Document rightMargin

List of usage examples for com.lowagie.text Document rightMargin

Introduction

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

Prototype


public float rightMargin() 

Source Link

Document

Return the right margin.

Usage

From source file:org.activityinfo.server.report.renderer.itext.ItextPivotTableRenderer.java

License:Open Source License

private float[] calcColumnWidths(Document doc, PivotTableData data, List<PivotTableData.Axis> leafColumns) {
    float[] widths = new float[leafColumns.size() + 1];
    float tableWidth = doc.getPageSize().getWidth() - doc.leftMargin() - doc.rightMargin();
    float valueColumnWidth = 47f;

    widths[0] = tableWidth - (leafColumns.size() * valueColumnWidth);
    if (widths[0] < valueColumnWidth) {
        widths[0] = valueColumnWidth;//from   w ww  .j a v  a2s.  com
    }
    for (int i = 1; i != widths.length; ++i) {
        widths[i] = valueColumnWidth;
    }
    return widths;
}

From source file:org.cyberoam.iview.charts.Chart.java

License:Open Source License

/**
 * This Event handler Method adds Header and Footer in PDF File
 *//*from  w  w  w.j a  v  a 2s .co  m*/
public void onEndPage(PdfWriter writer, Document document) {
    try {
        if (document.getPageNumber() > 1) {
            String seperator = System.getProperty("file.separator");
            //String path=System.getProperty("catalina.home") +seperator+"webapps" +seperator+"ROOT" + seperator + "images" + seperator;
            String path = InitServlet.contextPath + seperator + "images" + seperator;
            Image imgHead = Image.getInstance(path + "iViewPDFHeader.JPG");
            Image imgFoot = Image.getInstance(path + "iViewPDFFooter.JPG");
            Rectangle page = document.getPageSize();

            PdfPTable head = new PdfPTable(1);
            head.addCell(imgHead);
            head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
            head.writeSelectedRows(0, -1, document.leftMargin() - 10,
                    page.getHeight() - document.topMargin() + head.getTotalHeight(), writer.getDirectContent());

            PdfPTable foot = new PdfPTable(1);
            foot.addCell(imgFoot);
            foot.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
            foot.writeSelectedRows(0, -1, document.leftMargin() - 10, document.bottomMargin() + 24,
                    writer.getDirectContent());
        }
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}

From source file:org.dgl.imgstopdf.Main.java

public static void main(String[] args) {
    Document document = new Document();
    ArrayList<String> input;
    String output = ".\\out.pdf";
    try {//from   w w w  . j a v a  2s.c om
        FileOutputStream fos = new FileOutputStream(output);
        PdfWriter pdfWriter = PdfWriter.getInstance(document, fos);
        Image image;
        JFileChooser fc = new JFileChooser(".\\");
        fc.setMultiSelectionEnabled(true);
        FileNameExtensionFilter filter = new FileNameExtensionFilter("IMAGES", "jpg", "jpeg", "gif", "png",
                "bmp");
        fc.setFileFilter(filter);
        int userOption = fc.showOpenDialog(null);
        if (userOption == JFileChooser.APPROVE_OPTION) {
            input = new ArrayList<String>();
            for (File f : fc.getSelectedFiles()) {
                input.add(f.getAbsolutePath());
            }
            if (fc.getSelectedFile() != null) {
                input.add(fc.getSelectedFile().getAbsolutePath());
            }
        } else {
            return;
        }
        if (input.size() == 0) {
            return;
        }
        pdfWriter.open();
        document.open();
        for (Object fileName : input.toArray()) {
            image = Image.getInstance((String) fileName);
            float scaler = ((document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin())
                    / image.getWidth()) * 100;
            image.scalePercent(scaler);
            document.add(image);
        }
        document.close();
        pdfWriter.close();
        JOptionPane.showMessageDialog(null, "OK", "", JOptionPane.INFORMATION_MESSAGE);
    } catch (Exception ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(null, ex.toString(), "", JOptionPane.ERROR_MESSAGE);
    }
}

From source file:org.drools.verifier.doc.DroolsDocsComponentFactory.java

License:Apache License

public void onEndPage(PdfWriter writer, Document document) {

    try {/*from   w  ww  . j  a va  2  s .  co m*/
        Image image = Image.getInstance(DroolsDocsBuilder.class.getResource("guvnor-webapp.png")); // TODO this image never existed
        image.setAlignment(Image.RIGHT);
        image.scaleAbsolute(100, 30);
        Rectangle page = document.getPageSize();
        PdfPTable head = new PdfPTable(2);

        PdfPCell cell1 = new PdfPCell(image);
        cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell1.setBorder(0);

        head.addCell(cell1);

        PdfPCell cell2 = new PdfPCell(new Phrase(currentDate, DroolsDocsComponentFactory.HEADER_FOOTER_TEXT));
        cell2.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell2.setBorder(0);

        head.addCell(cell2);

        head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
        head.writeSelectedRows(0, -1, document.leftMargin(),
                page.getHeight() - document.topMargin() + head.getTotalHeight(), writer.getDirectContent());

    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}

From source file:org.goodoldai.jeff.report.pdf.PDFImageChunkBuilder.java

License:Open Source License

/**
 * This method transforms an image explanation chunk into a PDF report
 * piece and writes this piece into the provided output stream which is, in 
 * this case, an instance of com.lowagie.text.Document. The method first
 * collects all general chunk data (context, rule, group, tags) and inserts 
 * them into the report, and then retrieves the chunk content. Since the 
 * content is, in this case, an ImageData instance, the image it relates to 
 * (caption and URL) gets inserted into the report. If the image caption is 
 * missing, it doesn't get inserted into the report.
 *
 * @param echunk image explanation chunk that needs to be transformed
 * @param stream output stream to which the transformed chunk will be
 * written as output (in this case com.lowagie.text.Document)
 * @param insertHeaders denotes if chunk headers should be inserted into the
 * report (true) or not (false)/*from  ww w .  j a va2 s .c  o  m*/
 *
 * @throws org.goodoldai.jeff.explanation.ExplanationException if any of the arguments are
 * null, if the entered chunk is not an ImageExplanationChunk instance or 
 * if the entered output stream type is not com.lowagie.text.Document
 */
public void buildReportChunk(ExplanationChunk echunk, Object stream, boolean insertHeaders) {
    if (echunk == null) {
        throw new ExplanationException("The entered chunk must not be null");
    }

    if (stream == null) {
        throw new ExplanationException("The entered stream must not be null");
    }

    if (!(echunk instanceof ImageExplanationChunk)) {
        throw new ExplanationException("The entered chunk must be an ImageExplanationChunk instance");
    }

    if (!(stream instanceof com.lowagie.text.Document)) {
        throw new ExplanationException("The entered stream must be a com.lowagie.text.Document instance");
    }

    com.lowagie.text.Document doc = ((com.lowagie.text.Document) stream);

    //Insert general chunk data
    if (insertHeaders)
        PDFChunkUtility.insertChunkHeader(echunk, doc);

    try {
        //Insert content - in this case an image
        ImageData imdata = (ImageData) (echunk.getContent());

        //Get image data
        Image img = Image.getInstance(getClass().getResource(imdata.getURL()));

        //Scale the image in order to fit the page
        img.scaleToFit(doc.getPageSize().getRight(doc.leftMargin() + doc.rightMargin()),
                doc.getPageSize().getTop(doc.topMargin() + doc.bottomMargin()));

        //Add image
        doc.add(img);

        //If a caption is present, insert it below the image
        if ((imdata.getCaption() != null) && (!imdata.getCaption().equals(""))) {
            doc.add(new Paragraph("IMAGE: " + imdata.getCaption()));
        }
    } catch (NullPointerException e) {
        throw new ExplanationException(
                "The image '" + ((ImageData) (echunk.getContent())).getURL() + "' could not be found");
    } catch (Exception e) {
        throw new ExplanationException(e.getMessage());
    }

}

From source file:org.goodoldai.jeff.report.pdf.RTFImageChunkBuilder.java

License:Open Source License

/**
 * This method transforms an image explanation chunk into a PDF report
 * piece and writes this piece into the provided output stream which is, in 
 * this case, an instance of com.lowagie.text.Document. The method first
 * collects all general chunk data (context, rule, group, tags) and inserts 
 * them into the report, and then retrieves the chunk content. Since the 
 * content is, in this case, an ImageData instance, the image it relates to 
 * (caption and URL) gets inserted into the report. If the image caption is 
 * missing, it doesn't get inserted into the report.
 *
 * @param echunk image explanation chunk that needs to be transformed
 * @param stream output stream to which the transformed chunk will be
 * written as output (in this case com.lowagie.text.Document)
 * @param insertHeaders denotes if chunk headers should be inserted into the
 * report (true) or not (false)//from  w w  w. ja v a  2 s  . com
 *
 * @throws org.goodoldai.jeff.explanation.ExplanationException if any of the arguments are
 * null, if the entered chunk is not an ImageExplanationChunk instance or 
 * if the entered output stream type is not com.lowagie.text.Document
 */
public void buildReportChunk(ExplanationChunk echunk, Object stream, boolean insertHeaders) {
    if (echunk == null) {
        throw new ExplanationException("The entered chunk must not be null");
    }

    if (stream == null) {
        throw new ExplanationException("The entered stream must not be null");
    }

    if (!(echunk instanceof ImageExplanationChunk)) {
        throw new ExplanationException("The entered chunk must be an ImageExplanationChunk instance");
    }

    if (!(stream instanceof com.lowagie.text.Document)) {
        throw new ExplanationException("The entered stream must be a com.lowagie.text.Document instance");
    }

    com.lowagie.text.Document doc = ((com.lowagie.text.Document) stream);

    //Insert general chunk data
    if (insertHeaders)
        RTFChunkUtility.insertChunkHeader(echunk, doc);

    try {
        //Insert content - in this case an image
        ImageData imdata = (ImageData) (echunk.getContent());

        //Get image data
        Image img = Image.getInstance(getClass().getResource(imdata.getURL()));

        //Scale the image in order to fit the page
        img.scaleToFit(doc.getPageSize().getRight(doc.leftMargin() + doc.rightMargin()),
                doc.getPageSize().getTop(doc.topMargin() + doc.bottomMargin()));

        //Add image
        doc.add(img);

        //If a caption is present, insert it below the image
        if ((imdata.getCaption() != null) && (!imdata.getCaption().equals(""))) {
            doc.add(new Paragraph("IMAGE: " + imdata.getCaption()));
        }
    } catch (NullPointerException e) {
        throw new ExplanationException(
                "The image '" + ((ImageData) (echunk.getContent())).getURL() + "' could not be found");
    } catch (Exception e) {
        throw new ExplanationException(e.getMessage());
    }

}

From source file:org.jbpm.designer.web.server.TransformerServlet.java

License:Apache License

public void scalePDFImage(Document document, Image image) {
    float scaler = ((document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin())
            / image.getWidth()) * 100;/*ww w.j av a 2 s  .co m*/

    image.scalePercent(scaler);
}

From source file:org.opentestsystem.delivery.testreg.rest.view.PdfReportPageEventHelper.java

License:Open Source License

@Override
public void onEndPage(final PdfWriter writer, final Document document) {
    PdfContentByte cb = writer.getDirectContent();
    if (document.getPageNumber() == 1) {
        ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase(""),
                (document.right() - document.left()) / 2 + document.leftMargin(), document.top() - 5, 0f);
    }/* www. j ava 2s.  co m*/
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(""), document.left(), document.bottom() - 15,
            0f);

    int pageN = writer.getPageNumber();
    String text = "Page " + pageN + " of ";
    cb.beginText();
    cb.setFontAndSize(helv, 11);
    cb.setTextMatrix(document.right() - document.rightMargin() - 10, document.bottom() - 15);
    cb.showText(text);
    cb.endText();
    cb.addTemplate(template, document.right(), document.bottom() - 15);

}

From source file:org.revager.export.PDFPageEventHelper.java

License:Open Source License

@Override
public void onEndPage(PdfWriter writer, Document document) {
    int columnNumber;

    try {//from w w w . j ava2 s.c  om
        Rectangle page = document.getPageSize();
        float pageWidth = page.getWidth() - document.leftMargin() - document.rightMargin();

        /*
         * Write marks
         */
        setMarks(writer, document);

        /*
         * Define fonts
         */
        headBaseFont = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
        Font headFont = new Font(headBaseFont, headFontSize);

        footBaseFont = BaseFont.createFont(BaseFont.HELVETICA_OBLIQUE, BaseFont.CP1252, BaseFont.EMBEDDED);
        Font footFont = new Font(footBaseFont, footFontSize);

        /*
         * Cell fill for space between head/foot and content
         */
        PdfPCell cellFill = new PdfPCell();
        cellFill.setMinimumHeight(PDFTools.cmToPt(0.8f));
        cellFill.setBorderWidth(0);

        /*
         * Write head
         */
        if (headLogoPath != null) {
            columnNumber = 2;
        } else {
            columnNumber = 1;
        }

        PdfPTable head = new PdfPTable(columnNumber);

        Phrase phraseTitle = new Phrase(headTitle, headFont);

        PdfPCell cellTitle = new PdfPCell(phraseTitle);
        cellTitle.setHorizontalAlignment(Element.ALIGN_LEFT);
        cellTitle.setVerticalAlignment(Element.ALIGN_BOTTOM);
        cellTitle.setPaddingTop(0);
        cellTitle.setPaddingBottom(PDFTools.cmToPt(0.2f));
        cellTitle.setPaddingLeft(0);
        cellTitle.setPaddingRight(0);
        cellTitle.setBorderWidthTop(0);
        cellTitle.setBorderWidthBottom(0.5f);
        cellTitle.setBorderWidthLeft(0);
        cellTitle.setBorderWidthRight(0);

        head.addCell(cellTitle);

        if (headLogoPath != null) {
            Image headLogo = Image.getInstance(headLogoPath);
            headLogo.scaleToFit(PDFTools.cmToPt(5.0f), PDFTools.cmToPt(1.1f));

            PdfPCell cellLogo = new PdfPCell(headLogo);
            cellLogo.setHorizontalAlignment(Element.ALIGN_RIGHT);
            cellLogo.setVerticalAlignment(Element.ALIGN_BOTTOM);
            cellLogo.setPaddingTop(0);
            cellLogo.setPaddingBottom(PDFTools.cmToPt(0.15f));
            cellLogo.setPaddingLeft(0);
            cellLogo.setPaddingRight(0);
            cellLogo.setBorderWidthTop(0);
            cellLogo.setBorderWidthBottom(0.5f);
            cellLogo.setBorderWidthLeft(0);
            cellLogo.setBorderWidthRight(0);

            head.addCell(cellLogo);

            head.addCell(cellFill);
        }

        head.addCell(cellFill);

        head.setTotalWidth(pageWidth);
        head.writeSelectedRows(0, -1, document.leftMargin(),
                page.getHeight() - document.topMargin() + head.getTotalHeight(), writer.getDirectContent());

        /*
         * Write foot
         */
        if (footText == null) {
            footText = " ";
        }

        PdfPTable foot = new PdfPTable(1);

        foot.addCell(cellFill);

        PdfPCell cellFootText = new PdfPCell(new Phrase(footText, footFont));
        cellFootText.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cellFootText.setVerticalAlignment(Element.ALIGN_TOP);
        cellFootText.setPaddingTop(PDFTools.cmToPt(0.15f));
        cellFootText.setPaddingBottom(0);
        cellFootText.setPaddingLeft(0);
        cellFootText.setPaddingRight(0);
        cellFootText.setBorderWidthTop(0.5f);
        cellFootText.setBorderWidthBottom(0);
        cellFootText.setBorderWidthLeft(0);
        cellFootText.setBorderWidthRight(0);

        foot.addCell(cellFootText);

        /*
         * Print page numbers
         */
        PdfContentByte contentByte = writer.getDirectContent();
        contentByte.saveState();

        String text = MessageFormat.format(translate("Page {0} of") + " ", writer.getPageNumber());

        float textSize = footBaseFont.getWidthPoint(text, footFontSize);
        float textBase = document.bottom() - PDFTools.cmToPt(1.26f);
        contentByte.beginText();
        contentByte.setFontAndSize(footBaseFont, footFontSize);

        float adjust;
        if (footText.trim().equals("")) {
            adjust = (pageWidth / 2) - (textSize / 2) - footBaseFont.getWidthPoint("0", footFontSize);
        } else {
            adjust = 0;
        }

        contentByte.setTextMatrix(document.left() + adjust, textBase);
        contentByte.showText(text);
        contentByte.endText();
        contentByte.addTemplate(template, document.left() + adjust + textSize, textBase);

        contentByte.stroke();
        contentByte.restoreState();

        foot.setTotalWidth(pageWidth);
        foot.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(),
                writer.getDirectContent());
    } catch (Exception e) {
        /*
         * Not part of unit testing because this exception is only thrown if
         * an internal error occurs.
         */
        throw new ExceptionConverter(e);
    }
}

From source file:org.sigmah.server.report.renderer.itext.ItextChartRenderer.java

License:Open Source License

public void render(DocWriter writer, Document doc, PivotChartElement element) {

    try {/*from   w  w  w .  jav  a2 s  .  c  o m*/
        doc.add(ThemeHelper.elementTitle(element.getTitle()));
        ItextRendererHelper.addFilterDescription(doc, element.getContent().getFilterDescriptions());

        float width = doc.getPageSize().getWidth() - doc.rightMargin() - doc.leftMargin();
        float height = (doc.getPageSize().getHeight() - doc.topMargin() - doc.bottomMargin()) / 3f;

        if (writer instanceof PdfWriter) {

            // We can render the chart directly as vector graphics
            // in the PDF file

            PdfWriter pdfWriter = (PdfWriter) writer;
            PdfContentByte cb = pdfWriter.getDirectContent();
            Graphics2D g2d = cb.createGraphics(width, height);
            chartRenderer.render(element, false, g2d, (int) width, (int) height, 72);
            g2d.dispose();

        } else {

            // For RTF/Html we embed as a GIF

            width = width / 72f * RESOLUTION;
            height = height / 72f * RESOLUTION;

            BufferedImage chartImage = chartRenderer.renderImage(element, false, (int) width, (int) height,
                    RESOLUTION);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(chartImage, "GIF", baos);

            Image image = Image.getInstance(baos.toByteArray());
            image.scalePercent(72f / RESOLUTION * 100f);

            doc.add(image);

        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}