Example usage for com.itextpdf.text Document top

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

Introduction

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

Prototype


public float top() 

Source Link

Document

Returns the upper right y-coordinate.

Usage

From source file:bouttime.report.boutsheet.BoutSheetReport.java

License:Open Source License

/**
 * Add the header string to the document.
 * @param writer PdfWriter object to use.
 * @param document Document object to add the header to.
 *///  w w  w. j  av  a2  s.c o  m
private void addHeader(PdfWriter writer, Document document) {
    if (this.headerString == null) {
        return;
    }

    PdfContentByte cb = writer.getDirectContent();

    float textBase = document.top() + 5;
    cb.beginText();
    cb.setFontAndSize(this.baseFont, 12);
    cb.setTextMatrix(document.left(), textBase);
    cb.showText(this.headerString);
    cb.endText();
}

From source file:co.com.realtech.mariner.util.files.PDFUtils.java

public static File agregarTexto(byte[] bytes, String text) {
    File temp = null;/*from www.  j a va  2  s. c om*/
    try {
        temp = File.createTempFile("archivo", ".pdf");
        OutputStream oos = new FileOutputStream(temp);
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, oos);
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        //InputStream targetStream = new FileInputStream(initialFile);
        // Load existing PDF
        PdfReader reader = new PdfReader(bis);
        PdfImportedPage page = writer.getImportedPage(reader, 1);
        // Copy first page of existing PDF into output PDF
        document.setPageSize(reader.getPageSize(1));
        document.newPage();
        cb.addTemplate(page, 0, 0);

        ColumnText ct = new ColumnText(cb);
        Phrase myText = new Phrase(text);
        Font fuente = new Font();
        fuente.setSize(6);
        myText.setFont(fuente);
        ct.setSimpleColumn(myText, 0, -1, document.right(), document.top(), -10, Element.ALIGN_RIGHT);
        ct.go();

        ColumnText ct2 = new ColumnText(cb);
        Phrase myText2 = new Phrase(text);
        Font fuente2 = new Font();
        fuente2.setSize(6);
        myText2.setFont(fuente);
        ct2.setSimpleColumn(myText, 0, -1, document.right(), document.top(), 248, Element.ALIGN_RIGHT);
        ct2.go();

        ColumnText ct3 = new ColumnText(cb);
        Phrase myText3 = new Phrase(text);
        Font fuente3 = new Font();
        fuente3.setSize(6);
        myText3.setFont(fuente);
        ct3.setSimpleColumn(myText, 0, -1, document.right(), document.top(), 505, Element.ALIGN_RIGHT);
        ct3.go();

        document.close();
    } catch (Exception e) {
        System.out.println("e = " + e);
    }
    return temp;
}

From source file:com.afrisoftech.lib.HeaderFooterHelper.java

public void onEndPage(PdfWriter writer, Document document) {
    PdfContentByte cb = writer.getDirectContent();
    // Phrase header = new Phrase("this is a header", ffont);
    // Phrase footer = new Phrase("this is a footer", ffont);
    ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, header,
            (document.right() - document.left()) / 2 + document.leftMargin(), document.top() + 10, 0);
    ColumnText.showTextAligned(cb, Element.ALIGN_CENTER,
            new Phrase(footer.getContent() + " " + String.valueOf(writer.getPageNumber())),
            (document.right() - document.left()) / 2 + document.leftMargin(), document.bottom() - 10, 0);
}

From source file:com.tomasz.drag.triballocommanderro.controller.MakerPDF.java

public static void printToPDF(ArrayList<Person> workers, EventGig event) throws IOException, DocumentException {
    String filename = event.getName() + ".pdf";
    String path = event.getData() + " " + event.getName() + "//";

    Document document = new Document();
    // step 2/*  ww w.j ava 2s . co m*/
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path + filename));
    // step 3
    document.open();
    // step 4
    ColumnText column = new ColumnText(writer.getDirectContent());

    float[][] x = { { document.left(), document.left() + 230 }, { document.right() - 230, document.right() } };
    for (Person person : workers) {
        column.addElement(MakerPDF.createTable(person, event));
    }
    int count = 0;
    float height = 0;
    int status = ColumnText.START_COLUMN;
    while (ColumnText.hasMoreText(status)) {
        column.setSimpleColumn(x[count][0], document.bottom(), x[count][1], document.top() - height - 10);
        // render as much content as possible
        status = column.go();
        // go to a new page if you've reached the last column
        if (++count > 1) {
            count = 0;
            document.newPage();
        }
    }
    document.newPage();
    document.close();
}

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/*from  w w  w  . j  a v  a2 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

/**
 * prints a failure and / or a debug header when applicable.
 *
 * @see #getTemplateImage(com.itextpdf.text.pdf.PdfTemplate)
 * @param writer/*from  w  ww. ja v a  2 s  .  c o  m*/
 * @param document
 * @throws DocumentException
 * @throws VectorPrintException
 */
private final void renderHeader(PdfWriter writer, Document document)
        throws DocumentException, VectorPrintException {
    if ((!debugHereAfter && getSettings().getBooleanProperty(false, DEBUG))
            || (!failuresHereAfter && !getSettings().getBooleanProperty(false, DEBUG))) {

        writer.getDirectContent().addImage(getTemplateImage(template));

        if (getSettings().getBooleanProperty(false, DEBUG)) {
            ArrayList a = new ArrayList(2);
            a.add(PdfName.TOGGLE);
            a.add(elementProducer.initLayerGroup(DEBUG, writer.getDirectContent()));
            PdfAction act = PdfAction.setOCGstate(a, true);
            Chunk h = new Chunk("toggle debug info",
                    DebugHelper.debugFontLink(writer.getDirectContent(), getSettings())).setAction(act);

            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, new Phrase(h), 10,
                    document.top() - 15, 0);
            Font f = DebugHelper.debugFontLink(writer.getDirectContent(), getSettings());
            //            act = PdfAction.gotoLocalPage("debugpage", true);

            elementProducer.startLayerInGroup(DEBUG, writer.getDirectContent());

            h = new Chunk(getSettings().getProperty("go to debug legend", "debugheader"), f)
                    .setLocalGoto(BaseReportGenerator.DEBUGPAGE);
            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, new Phrase(h), 10,
                    document.top() - 3, 0);

            writer.getDirectContent().endLayer();

        }
    }
}

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)
 * }//from   w  w  w. j  a  va 2s.c  o  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:eyeofthetiger.utils.PDFDossardGenerator.java

public void createPdf(List<Participant> participants, OutputStream out) throws IOException, DocumentException {
    sortParticipants(participants);/*  w ww  .  j a v  a2s.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:net.digitstar.vanadio.AbstractReportPdf.java

License:Apache License

protected void writeHeaderFooter(Document document, PdfReader reader, PdfStamper writer, int pag,
        int totalPages, ReportOptions reportOptions) {
    if (reportOptions.isShowHeader()) {
        if (pag != 1 || reportOptions.isShowHeaderOnFirstPage()) {
            PdfPTable header = createHeader(document, pag, totalPages, reportOptions);
            header.writeSelectedRows(0, -1, document.left(), document.top() + header.getTotalHeight(),
                    writer.getOverContent(pag));
        }/*from  w w  w  .  java 2s. co  m*/
    }
    if (reportOptions.isShowFooter()) {
        if (pag != 1 || reportOptions.isShowFooterOnFirstPage()) {
            PdfPTable footer = createFooter(document, pag, totalPages, reportOptions);
            footer.writeSelectedRows(0, -1, document.left(), document.bottom() - footer.getTotalHeight(),
                    writer.getOverContent(pag));
        }
    }
}

From source file:utilities.Footer.java

@Override
public void onEndPage(PdfWriter writer, Document document) {
    PdfContentByte cb = writer.getDirectContent();
    Phrase header = new Phrase("ZLECENIE SERWISOWE", ffont);

    Phrase footer = new Phrase("ZLECENIE SERWISOWE", ffont);
    ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, footer,
            (document.right() - document.left()) / 2 + document.leftMargin(), 20, 0);

    ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, header,
            (document.right() - document.left()) / 2 + document.leftMargin(), document.top() + 10, 0);

}