Example usage for com.itextpdf.text Document right

List of usage examples for com.itextpdf.text Document right

Introduction

In this page you can find the example usage for com.itextpdf.text Document right.

Prototype


public float right() 

Source Link

Document

Returns the upper right x-coordinate.

Usage

From source file:com.vectorprint.report.itext.EventHelper.java

License:Open Source License

/**
 * prints debug info when property debug is true, calls renderHeader and renderFooter and
 * {@link Advanced#draw(com.itextpdf.text.Rectangle, java.lang.String) } with {@link Document#getPageSize() }
 * and null for {@link DefaultStylerFactory#PAGESTYLERS}.
 *
 * @param writer//w w w .j av  a  2  s  .com
 * @param document
 */
@Override
public final void onEndPage(PdfWriter writer, Document document) {
    super.onEndPage(writer, document);
    sanitize(writer);
    try {
        if (failuresHereAfter || debugHereAfter) {
            PdfContentByte bg = writer.getDirectContentUnder();
            Rectangle rect = writer.getPageSize();
            rect.setBackgroundColor(itextHelper
                    .fromColor(getSettings().getColorProperty(new Color(240, 240, 240), "legendbackground")));
            bg.rectangle(rect);
            bg.closePathFillStroke();
        } else {
            for (Advanced a : doForAllPages) {
                try {
                    if (a.shouldDraw(null)) {
                        a.draw(document.getPageSize(), null);
                    }
                } catch (VectorPrintException ex) {
                    throw new VectorPrintRuntimeException(ex);
                }
            }
        }
        if (!debugHereAfter && getSettings().getBooleanProperty(false, DEBUG)) {

            PdfContentByte canvas = writer.getDirectContent();

            Rectangle rect = new Rectangle(document.leftMargin(), document.bottomMargin(),
                    document.right() - document.rightMargin(), document.top() - document.topMargin());

            DebugHelper.debugRect(canvas, rect, new float[] { 10, 2 }, 0.3f, getSettings(),
                    stylerFactory.getLayerManager());

        }

        renderHeader(writer, document);
        maxTagForGenericTagOnPage = ((DefaultElementProducer) elementProducer).getAdvancedTag();
        if (getSettings().getBooleanProperty(Boolean.FALSE, ReportConstants.PRINTFOOTER)) {
            renderFooter(writer, document);
        } else {
            log.warning("not printing footer, if you want page footers set " + ReportConstants.PRINTFOOTER
                    + " to true");
        }
        maxTagForGenericTagOnPage = Integer.MAX_VALUE;
    } catch (VectorPrintException | DocumentException | InstantiationException | IllegalAccessException e) {
        throw new VectorPrintRuntimeException("failed to create the report header or footer: ", e);
    }
}

From source file:com.vectorprint.report.itext.EventHelper.java

License:Open Source License

/**
 * calls {@link #printTotalPages(com.itextpdf.text.pdf.PdfTemplate, float, float)  }
 * with {@link #PAGEFOOTERSTYLE a font from setup}, document.right() and the calculated bottom of the footertable.
 * Clears the layermanager. When applicable calls {@link #printFailureHeader(com.itextpdf.text.pdf.PdfTemplate, float, float)
 * }/* w  w w.  ja  va2s . co  m*/
 *
 * @param writer
 * @param document
 */
@Override
public void onCloseDocument(PdfWriter writer, Document document) {
    super.onCloseDocument(writer, document);
    if (getSettings().getBooleanProperty(Boolean.FALSE, ReportConstants.PRINTFOOTER)) {
        try {
            printTotalPages(template, document.right(), footerBottom);
        } catch (VectorPrintException | InstantiationException | IllegalAccessException ex) {
            throw new VectorPrintRuntimeException(ex);
        }
    }
    if (failuresHereAfter) {
        printFailureHeader(template, 10, document.top() - 10);
    }
}

From source file:com.vectorprint.report.itext.style.stylers.SimpleColumns.java

License:Open Source License

@Override
public void setDocument(Document document, com.itextpdf.text.pdf.PdfWriter writer) {
    super.setDocument(document, writer);
    setValue(LEFT, document.leftMargin());
    setValue(RIGHT, document.right() - document.rightMargin());
    setValue(BOTTOM, getDocument().bottomMargin());
    setValue(TOP, getDocument().top() - getDocument().topMargin());
}

From source file:com.vectorprint.report.itext.TocOutputStream.java

License:Open Source License

private void printToc(Document d, PdfWriter w, VectorPrintDocument vpd)
        throws VectorPrintException, InstantiationException, IllegalAccessException, DocumentException {
    DocumentStyler ds = outer.getStylerFactory().getDocumentStyler();
    if (ds.getValue(DocumentSettings.TOCAPPEND, Boolean.class)) {
        d.add(Chunk.NEXTPAGE);//w w  w. j  a v  a 2s.co  m
    }
    if (outer.isWasDebug()) {
        outer.getSettings().put(ReportConstants.DEBUG, Boolean.TRUE.toString());
        PdfContentByte canvas = w.getDirectContent();
        outer.startLayerInGroup(ReportConstants.DEBUG, canvas);
        BaseFont bf = DebugHelper.debugFont(canvas, outer.getSettings());
        canvas.showTextAligned(Element.ALIGN_RIGHT,
                "FOR DEBUG INFO IN THE DOCUMENT TURN OFF TOC (-DocumentSettings.toc=false)", d.right(),
                d.getPageSize().getHeight() - ItextHelper.getTextHeight("F", bf, 8), 0);
        canvas.endLayer();
    }
    ElementProducer ep = outer.getElementProducer();
    StylerFactory sf = outer.getStylerFactory();
    PdfPTable tocTable = ep.createElement(null, PdfPTable.class, sf.getStylers(DocumentSettings.TOCTABLEKEY));
    for (Map.Entry<Integer, List<Section>> e : vpd.getToc().entrySet()) {
        String link = null;
        for (Section s : e.getValue()) {
            if (ds.isParameterSet(DocumentSettings.TOCMAXDEPTH)
                    && ds.getValue(DocumentSettings.TOCMAXDEPTH, Integer.class) < s.getDepth()) {
                continue;
            }
            if (link == null) {
                link = s.getTitle().getContent();
            }
            Chunk c = ep.createElement(s.getTitle().getContent(), Chunk.class,
                    sf.getStylers(DocumentSettings.TOCTITLESTYLEKEY));
            if (ds.getValue(DocumentSettings.TOCDOTS, Boolean.class)) {
                float tw = ItextHelper.getTextWidth(c);
                float cw = tocTable.getAbsoluteWidths()[0];
                float dw = ItextHelper.getTextWidth(
                        ep.createElement(".", Chunk.class, sf.getStylers(DocumentSettings.TOCTITLESTYLEKEY)))
                        * 1.5f;
                int numDots = (int) ((cw > tw) ? (cw - tw) / dw : 0);
                char[] dots = new char[numDots];
                Arrays.fill(dots, '.');
                c = ep.createElement(s.getTitle().getContent() + "  " + String.valueOf(dots), Chunk.class,
                        sf.getStylers(DocumentSettings.TOCTITLESTYLEKEY));
            }
            c.setLocalGoto(link);
            tocTable.addCell(
                    ep.createElement(c, PdfPCell.class, sf.getStylers(DocumentSettings.TOCTITLESTYLEKEY)));
            c = ep.createElement(e.getKey(), Chunk.class, sf.getStylers(DocumentSettings.TOCNRSTYLEKEY));
            c.setLocalGoto(link);
            tocTable.addCell(
                    ep.createElement(c, PdfPCell.class, sf.getStylers(DocumentSettings.TOCNRSTYLEKEY)));
        }
    }
    d.add(tocTable);
}

From source file:dbedit.actions.ExportPdfAction.java

License:Open Source License

/**
 * Print page numbers on right bottom corner
 * @param writer//from  w w w . ja v  a 2s  .  c  om
 * @param document
 */
@Override
public void onEndPage(PdfWriter writer, Document document) {
    PdfContentByte cb = writer.getDirectContent();
    String text = String.format("Page %d of ", writer.getPageNumber());
    float textSize = BASE_FONT.getWidthPoint(text, 12);
    float textBase = document.bottom() - 20;
    cb.beginText();
    cb.setFontAndSize(BASE_FONT, 12);
    float adjust = BASE_FONT.getWidthPoint("000", 12);
    cb.setTextMatrix(document.right() - textSize - adjust, textBase);
    cb.showText(text);
    cb.endText();
    cb.addTemplate(pdfTemplate, document.right() - adjust, textBase);
}

From source file:eyeofthetiger.utils.PDFDossardGenerator.java

public void createPdf(List<Participant> participants, OutputStream out) throws IOException, DocumentException {
    sortParticipants(participants);//from   w  w w  .j a  va 2s  . c om

    Document document = new Document(PageSize.A4.rotate());
    float margin = CentimeterToUserSpace(marginCm);
    document.setMargins(margin, margin, margin, margin);
    PdfWriter writer = PdfWriter.getInstance(document, out);
    document.open();
    PdfContentByte cb = writer.getDirectContent();

    PdfReader pdfBackgroundReader = null;
    PdfImportedPage backgroundPage = null;
    if (pdfBackground != null && (new File(pdfBackground)).exists() && (new File(pdfBackground)).isFile()) {
        pdfBackgroundReader = new PdfReader(pdfBackground);
        backgroundPage = writer.getImportedPage(pdfBackgroundReader, 1);
    }

    float documentTop = document.top();
    float documentBottom = document.bottom();
    float documentHeight = documentTop - documentBottom;
    float left = document.left();
    float right = document.right();
    float width = right - left;
    float height = documentTop - documentBottom;

    //cb.rectangle(left, documentBottom, width, documentHeight);
    //cb.stroke();

    boolean logoLeftExist = (new File(logoLeft)).exists() && (new File(logoLeft)).isFile();
    boolean logoRightExist = (new File(logoRight)).exists() && (new File(logoRight)).isFile();

    float imgLeftRight = left;
    float imgLeftBottom = documentTop;
    float imgRightLeft = right;
    float imgRighBottom = documentTop;
    Image imgLeft = null;
    Image imgRight = null;
    if (exportLogos) {
        if (logoLeftExist) {
            imgLeft = Image.getInstance(logoLeft);
            float h = imgLeft.getHeight();
            float w = imgLeft.getWidth();
            float nw = width * logoLeftWidth;
            float nh = (h / w) * nw;
            imgLeft.scaleAbsolute(nw, nh);
            //img.scaleAbsoluteHeight(img.getScaledWidth() / xyRatio);
            imgLeft.setAbsolutePosition(left, documentTop - imgLeft.getScaledHeight());
            //cb.addImage(img);   

            imgLeftRight = imgLeft.getAbsoluteX() + imgLeft.getScaledWidth();
            imgLeftBottom = imgLeft.getAbsoluteY();
        }

        if (logoRightExist) {
            imgRight = Image.getInstance(logoRight);
            float h = imgRight.getHeight();
            float w = imgRight.getWidth();
            float nw = width * logoRightWidth;
            float nh = (h / w) * nw;
            imgRight.scaleAbsolute(nw, nh);
            imgRight.setAbsolutePosition(right - imgRight.getScaledWidth(),
                    documentTop - imgRight.getScaledHeight());
            //cb.addImage(imgRight);
            imgRightLeft = imgRight.getAbsoluteX();
            imgRighBottom = imgRight.getAbsoluteY();
        }

    }

    float nameHeightPercent = 0.35f;
    float groupHeightPercent = 0.25f;

    float nameTop = documentTop;
    float nameBottom = nameTop;
    if (exportName) {
        nameBottom = nameTop - (documentHeight * nameHeightPercent);
    }
    float groupeTop = nameBottom;
    float groupeBottom = nameBottom;
    if (exportGroup) {
        groupeBottom = groupeTop - (documentHeight * groupHeightPercent);
    }
    float barcodeTop = groupeBottom;
    float barcodeBottom = documentBottom;

    ColumnText columnText;

    for (Participant participant : participants) {

        if (backgroundPage != null) {
            //cb.addTemplate(backgroundPage, 1f, 0, 0, 1, 0, 0); //TODO
            cb.addTemplate(backgroundPage, 0, 0);
        }

        float nameFontSize = 65f;
        float groupFontSize = 45f;
        float renseignementFontSize = 35f;

        if (imgLeft != null) {
            cb.addImage(imgLeft);
        }
        if (imgRight != null) {
            cb.addImage(imgRight);
        }

        if (exportName) {
            columnText = new ColumnText(cb);
            columnText.setAlignment(Rectangle.ALIGN_CENTER);

            if (imgLeftRight != -1 && imgLeftBottom != -1) {
                float[] leftBorder = null;
                if (imgLeftBottom < nameBottom) {
                    leftBorder = new float[] { imgLeftRight, nameTop, imgLeftRight, nameBottom, left,
                            nameBottom };
                } else {
                    leftBorder = new float[] { imgLeftRight, nameTop, imgLeftRight, imgLeftBottom, left,
                            imgLeftBottom, left, nameBottom };
                }

                float[] rightBorder = null;
                if (imgRighBottom < nameBottom) {
                    rightBorder = new float[] { imgRightLeft, nameTop, imgRightLeft, nameBottom, right,
                            nameBottom };
                } else {
                    rightBorder = new float[] { imgRightLeft, nameTop, imgRightLeft, imgRighBottom, right,
                            imgRighBottom, right, nameBottom };
                }

                columnText.setColumns(leftBorder, rightBorder);
            } else {
                columnText.setSimpleColumn(left, nameTop, right, nameBottom);
            }
            //cb.rectangle(left, nameBottom, width, (nameTop - nameBottom));
            //cb.stroke();

            columnText.setExtraParagraphSpace(0f);
            columnText.setAdjustFirstLine(false);
            columnText.setIndent(0);

            String txt = participant.getNom().toUpperCase() + " " + participant.getPrenom();

            float previousPos = columnText.getYLine();
            columnText.setLeading(nameFontSize);
            columnText.setText(createCleanPhrase(txt, nameFontSize, true));
            while (nameFontSize > 1 && ColumnText.hasMoreText(columnText.go(true))) {
                nameFontSize = nameFontSize - 0.5f;
                columnText.setLeading(nameFontSize);
                columnText.setText(createCleanPhrase(txt, nameFontSize, true));
                columnText.setYLine(previousPos);
            }

            columnText.setLeading(nameFontSize);
            columnText.setText(createCleanPhrase(txt, nameFontSize, true));
            columnText.setYLine(previousPos);
            columnText.go(false);

        }

        if (exportGroup) {
            columnText = new ColumnText(cb);
            columnText.setAlignment(Rectangle.ALIGN_CENTER);

            columnText.setSimpleColumn(document.left(), groupeTop, document.right(), groupeBottom);
            float groupeHeight = groupeTop - groupeBottom;
            //cb.rectangle(document.left(), groupeTop - groupeHeight, document.right() - document.left(), groupeHeight);
            //cb.stroke();

            columnText.setExtraParagraphSpace(0f);
            columnText.setAdjustFirstLine(false);
            columnText.setIndent(0);
            columnText.setFollowingIndent(0);

            String txt1 = participant.getGroupe();
            String txt2 = exportRenseignement ? "\n" + participant.getRenseignements() : null;

            float previousPos = columnText.getYLine();
            columnText.setText(null);
            columnText.setLeading(groupFontSize);
            columnText.addText(createCleanPhrase(txt1, groupFontSize, true));
            columnText.addText(createCleanPhrase(txt2, renseignementFontSize, false));
            while (groupFontSize > 1 && ColumnText.hasMoreText(columnText.go(true))) {
                groupFontSize = groupFontSize - 0.5f;
                renseignementFontSize = renseignementFontSize - 0.5f;
                columnText.setText(null);
                columnText.setLeading(groupFontSize);
                columnText.addText(createCleanPhrase(txt1, groupFontSize, true));
                columnText.addText(createCleanPhrase(txt2, renseignementFontSize, false));
                columnText.setYLine(previousPos);
            }

            columnText.setText(null);
            columnText.setLeading(groupFontSize);
            columnText.addText(createCleanPhrase(txt1, groupFontSize, true));
            columnText.addText(createCleanPhrase(txt2, renseignementFontSize, false));
            columnText.setYLine(previousPos);
            columnText.go(false);
        }

        {
            columnText = new ColumnText(cb);

            float topMargin = 12f;
            columnText.setSimpleColumn(left, barcodeTop - topMargin, right, barcodeBottom);
            float barcodeHeight = (barcodeTop - topMargin) - barcodeBottom;
            //cb.rectangle(left, barcodeTop - barcodeHeight, width, barcodeHeight);
            //cb.stroke();

            columnText.setExtraParagraphSpace(0f);
            columnText.setAdjustFirstLine(false);
            columnText.setIndent(0);

            float previousPos = columnText.getYLine();
            columnText.setText(null);
            columnText.addElement(createCleanBarcode(cb, participant.getNumero(), width, barcodeHeight));
            columnText.go(false);
        }

        document.newPage();

    }

    document.close();

    if (pdfBackgroundReader != null) {
        pdfBackgroundReader.close();
    }
}

From source file:fll.util.SimpleFooterHandler.java

License:Open Source License

@Override
public void onEndPage(final PdfWriter writer, final Document document) {
    final PdfContentByte cb = writer.getDirectContent();
    cb.saveState();//from ww  w.ja va  2 s  .c  o  m

    // compose the footer
    final String text = "Page " + writer.getPageNumber() + " of ";
    final float textSize = _headerFooterFont.getWidthPoint(text, 12);
    final float textBase = document.bottom() - 20;
    cb.beginText();
    cb.setFontAndSize(_headerFooterFont, 12);

    final float adjust = _headerFooterFont.getWidthPoint("0", 12);
    cb.setTextMatrix(document.right() - textSize - adjust, textBase);
    cb.showText(text);
    cb.endText();
    cb.addTemplate(_tpl, document.right() - adjust, textBase);

    cb.restoreState();
}

From source file:fll.web.report.ReportPageEventHandler.java

License:Open Source License

@Override
// initialization of the header table
public void onEndPage(final PdfWriter writer, final Document document) {
    final PdfPTable header = new PdfPTable(2);
    final Phrase p = new Phrase();
    final Chunk ck = new Chunk(_challengeTitle + "\n" + _reportTitle, _font);
    p.add(ck);/*w  w w . j av a 2s .c  o  m*/
    header.getDefaultCell().setBorderWidth(0);
    header.addCell(p);
    header.getDefaultCell().setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_RIGHT);
    header.addCell(new Phrase(new Chunk("Tournament: " + _tournament + "\nDate: " + _formattedDate, _font)));
    final PdfPCell blankCell = new PdfPCell();
    blankCell.setBorder(0);
    blankCell.setBorderWidthTop(1.0f);
    blankCell.setColspan(2);
    header.addCell(blankCell);

    final PdfContentByte cb = writer.getDirectContent();
    cb.saveState();
    header.setTotalWidth(document.right() - document.left());
    header.writeSelectedRows(0, -1, document.left(), document.getPageSize().getHeight() - 10, cb);
    cb.restoreState();
}

From source file:net.digitstar.vanadio.AbstractReportPdf.java

License:Apache License

protected PdfPTable createHeader(Document document, int page, int pageTotal, ReportOptions reportOptions) {
    PdfPTable header = new PdfPTable(3);
    header.setTotalWidth(document.right() - document.left());
    PdfPCell def = header.getDefaultCell();
    String left = "";
    String center = "";
    String right = "";

    if (reportOptions.isShowPageNumberIfOnePage()
            || (!reportOptions.isShowPageNumberIfOnePage() && pageTotal > 1)) {
        right = reportOptions.isShowPageTotal() ? Labels.PAGE_TOTAL.getLabelFormatted(page, pageTotal)
                : Labels.PAGE.getLabelFormatted(page);
    }/*  ww  w  . j  a  v  a  2  s  .com*/

    def.setFixedHeight(computeHeight(FontType.HEADERFOOTER.getFont()));

    def.setBorder(CellStyle.Border.NONE.getValue());
    if (reportOptions.isUseHrRuler())
        def.setBorder(CellStyle.Border.BOTTOM.getValue());

    def.setHorizontalAlignment(CellStyle.Align.LEFT.getValue());
    header.addCell(new Phrase(left, FontType.HEADERFOOTER.getFont()));
    def.setHorizontalAlignment(CellStyle.Align.CENTER.getValue());
    header.addCell(new Phrase(center, FontType.HEADERFOOTER.getFont()));
    def.setHorizontalAlignment(CellStyle.Align.RIGHT.getValue());
    header.addCell(new Phrase(right, FontType.HEADERFOOTER.getFont()));
    return header;
}

From source file:net.digitstar.vanadio.AbstractReportPdf.java

License:Apache License

protected PdfPTable createFooter(Document document, int page, int pageTotal, ReportOptions reportOptions) {
    PdfPTable footer = new PdfPTable(3);
    footer.setTotalWidth(document.right() - document.left());
    PdfPCell def = footer.getDefaultCell();
    String left = "";
    String center = "";
    String right = "";

    def.setFixedHeight(computeHeight(FontType.HEADERFOOTER.getFont()));
    def.setBorder(CellStyle.Border.NONE.getValue());
    if (reportOptions.isUseHrRuler())
        def.setBorder(CellStyle.Border.TOP.getValue());

    if (reportOptions.isShowPrintDate())
        right = Labels.PRINTED.getLabelFormatted(getDateTimeFormat().format(new Date()));

    def.setHorizontalAlignment(CellStyle.Align.LEFT.getValue());
    footer.addCell(new Phrase(left, FontType.HEADERFOOTER.getFont()));
    def.setHorizontalAlignment(CellStyle.Align.CENTER.getValue());
    footer.addCell(new Phrase(center, FontType.HEADERFOOTER.getFont()));
    def.setHorizontalAlignment(CellStyle.Align.RIGHT.getValue());
    footer.addCell(new Phrase(right, FontType.HEADERFOOTER.getFont()));
    return footer;
}