Example usage for com.itextpdf.text.pdf ColumnText setYLine

List of usage examples for com.itextpdf.text.pdf ColumnText setYLine

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf ColumnText setYLine.

Prototype

public void setYLine(final float yLine) 

Source Link

Document

Sets the yLine.

Usage

From source file:com.ideationdesignservices.txtbook.pdf.TxtBookPdf.java

public void doAddNextConversation(Document document, Cursor cursor, ColumnText ct, String senderString,
        String contentString, Bitmap contentImage, Boolean isVideo, Boolean isMe) {
    float[][] COLUMNS;
    if (COLUMNS_REG[0][0] == Txtbook.leftMargin(this.writer, document, this.settings.addFrontCover)) {
        COLUMNS = COLUMNS_REG;/*www. ja v  a 2  s  .com*/
    } else {
        COLUMNS = COLUMNS_ALT;
    }
    String dateString = PdfObject.NOTHING;
    if (this.settings.useTimestamps.booleanValue()) {
        Long date = Long.valueOf(cursor.getLong(cursor.getColumnIndex("normalized_date")));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM dd, yyyy h:mm a", Locale.US);
        simpleDateFormat.setTimeZone(TimeZone.getDefault());
        dateString = simpleDateFormat.format(new Date(date.longValue()));
    }
    try {
        this.currentY = ct.getYLine();
        addConversationPart(ct, this.currentColumn, dateString, senderString, contentString, contentImage,
                isVideo, isMe);
        if (ColumnText.hasMoreText(ct.go(true))) {
            this.currentColumn = (this.currentColumn + 1) % 2;
            if (this.currentColumn == 0) {
                document.newPage();
                if (COLUMNS_REG[0][0] == Txtbook.leftMargin(this.writer, document,
                        this.settings.addFrontCover)) {
                    COLUMNS = COLUMNS_REG;
                } else {
                    COLUMNS = COLUMNS_ALT;
                }
            }
            ct.setSimpleColumn(COLUMNS[this.currentColumn][0], COLUMNS[this.currentColumn][1],
                    COLUMNS[this.currentColumn][2], COLUMNS[this.currentColumn][3]);
            this.currentY = COLUMNS[this.currentColumn][3];
        }
        ct.setYLine(this.currentY);
        ct.setText(null);
        float width = addConversationPart(ct, this.currentColumn, dateString, senderString, contentString,
                contentImage, isVideo, isMe);
        int status = ct.go(false);
        Document document2 = document;
        ColumnText columnText = ct;
        addConversationBackground(document2, columnText, this.currentColumn, this.currentY, width,
                this.currentY - ct.getYLine(), isMe);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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  v  a  2s .c  o  m

    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:mkl.testarea.itext5.pdfcleanup.StrictPdfCleanUpProcessor.java

License:Open Source License

private void drawOverlayText(PdfContentByte canvas, List<Rectangle> textRectangles, PdfString overlayText,
        PdfString otDA, PdfNumber otQ, PdfBoolean otRepeat) throws DocumentException, IOException {
    ColumnText ct = new ColumnText(canvas);
    ct.setLeading(0, 1.2F);/*from w ww . j a v  a  2 s .c o  m*/
    ct.setUseAscender(true);

    String otStr = overlayText.toUnicodeString();

    canvas.saveState();
    Map<String, List> parsedDA = parseDAParam(otDA);

    Font font = null;

    if (parsedDA.containsKey(STROKE_COLOR)) {
        List strokeColorArgs = parsedDA.get(STROKE_COLOR);
        setStrokeColor(canvas, strokeColorArgs);
    }

    if (parsedDA.containsKey(FILL_COLOR)) {
        List fillColorArgs = parsedDA.get(FILL_COLOR);
        setFillColor(canvas, fillColorArgs);
    }

    if (parsedDA.containsKey("Tf")) {
        List tfArgs = parsedDA.get("Tf");
        font = retrieveFontFromAcroForm((PdfName) tfArgs.get(0), (PdfNumber) tfArgs.get(1));
    }

    for (Rectangle textRect : textRectangles) {
        ct.setSimpleColumn(textRect);

        if (otQ != null) {
            ct.setAlignment(otQ.intValue());
        }

        Phrase otPhrase;

        if (font != null) {
            otPhrase = new Phrase(otStr, font);
        } else {
            otPhrase = new Phrase(otStr);
        }

        float y = ct.getYLine();

        if (otRepeat != null && otRepeat.booleanValue()) {
            int status = ct.go(true);

            while (!ColumnText.hasMoreText(status)) {
                otPhrase.add(otStr);
                ct.setText(otPhrase);
                ct.setYLine(y);
                status = ct.go(true);
            }
        }

        ct.setText(otPhrase);
        ct.setYLine(y);
        ct.go();
    }

    canvas.restoreState();
}