Example usage for com.lowagie.text Chunk setUnderline

List of usage examples for com.lowagie.text Chunk setUnderline

Introduction

In this page you can find the example usage for com.lowagie.text Chunk setUnderline.

Prototype

public Chunk setUnderline(float thickness, float yPosition) 

Source Link

Document

Sets an horizontal line that can be an underline or a strikethrough.

Usage

From source file:fr.opensagres.odfdom.converter.pdf.internal.stylable.StylableParagraph.java

License:Open Source License

@SuppressWarnings("unchecked")
private void postProcessLineHeightAndBaseline() {
    // adjust line height and baseline
    Font font = getMostOftenUsedFont();
    if (font == null || font.getBaseFont() == null) {
        font = this.font;
    }//w w w  .j a v a 2 s.co m
    if (font != null && font.getBaseFont() != null) {
        // iText and open office computes proportional line height differently
        // [iText] line height = coefficient * font size
        // [open office] line height = coefficient * (font ascender + font descender + font extra margin)
        // we have to increase paragraph line height to generate pdf similar to open office document
        // this algorithm may be inaccurate if fonts with different multipliers are used in this paragraph
        float size = font.getSize();
        float ascender = font.getBaseFont().getFontDescriptor(BaseFont.AWT_ASCENT, size);
        float descender = -font.getBaseFont().getFontDescriptor(BaseFont.AWT_DESCENT, size); // negative value
        float margin = font.getBaseFont().getFontDescriptor(BaseFont.AWT_LEADING, size);
        float multiplier = (ascender + descender + margin) / size;
        if (multipliedLeading > 0.0f) {
            setMultipliedLeading(getMultipliedLeading() * multiplier);
        }

        // iText seems to output text with baseline lower than open office
        // we raise all paragraph text by some amount
        // again this may be inaccurate if fonts with different size are used in this paragraph
        float itextdescender = -font.getBaseFont().getFontDescriptor(BaseFont.DESCENT, size); // negative
        float textRise = itextdescender + getTotalLeading() - font.getSize() * multiplier;
        ArrayList<Chunk> chunks = getChunks();
        for (Chunk chunk : chunks) {
            Font f = chunk.getFont();
            if (f != null) {
                // have to raise underline and strikethru as well
                float s = f.getSize();
                if (f.isUnderlined()) {
                    f.setStyle(f.getStyle() & ~Font.UNDERLINE);
                    chunk.setUnderline(s * 1 / 17, s * -1 / 7 + textRise);
                }
                if (f.isStrikethru()) {
                    f.setStyle(f.getStyle() & ~Font.STRIKETHRU);
                    chunk.setUnderline(s * 1 / 17, s * 1 / 4 + textRise);
                }
            }
            chunk.setTextRise(chunk.getTextRise() + textRise);
        }
    }
}

From source file:fr.opensagres.poi.xwpf.converter.pdf.internal.FastPdfMapper.java

License:Open Source License

private Chunk createTextChunk(String text, boolean pageNumber, Font currentRunFont,
        UnderlinePatterns currentRunUnderlinePatterns, Color currentRunBackgroundColor) {
    Chunk textChunk = pageNumber ? new ExtendedChunk(pdfDocument, true, currentRunFont)
            : new Chunk(text, currentRunFont);

    if (currentRunUnderlinePatterns != null) {
        // underlined
        boolean singleUnderlined = false;
        switch (currentRunUnderlinePatterns) {
        case SINGLE:
            singleUnderlined = true;/*from   ww w  .j  av a 2 s .  c  om*/
            break;

        default:
            break;
        }
        if (singleUnderlined) {
            textChunk.setUnderline(1, -2);
        }
    }

    // background color
    if (currentRunBackgroundColor != null) {
        textChunk.setBackground(Converter.toAwtColor(currentRunBackgroundColor));
    }
    if (currentRunX != null) {
        this.currentRunX += textChunk.getWidthPoint();
    }
    return textChunk;
}

From source file:fr.opensagres.poi.xwpf.converter.pdf.internal.PdfMapper.java

License:Open Source License

private Chunk createTextChunk(String text, boolean pageNumber, Font currentRunFont,
        UnderlinePatterns currentRunUnderlinePatterns, Color currentRunBackgroundColor) {
    // Chunk textChunk =
    // pageNumber ? new ExtendedChunk( pdfDocument, true, currentRunFont ) :
    // new Chunk( text, currentRunFont );

    Chunk textChunk = null;
    if (processingTotalPageCountField && expectedPageCount != null) {
        textChunk = new Chunk(String.valueOf(expectedPageCount), currentRunFont);
    } else {/*from   w  ww .  java 2s .com*/
        textChunk = pageNumber ? new ExtendedChunk(pdfDocument, true, currentRunFont)
                : new Chunk(text, currentRunFont);
    }
    if (currentRunUnderlinePatterns != null) {
        // underlined
        boolean singleUnderlined = false;
        switch (currentRunUnderlinePatterns) {
        case SINGLE:
            singleUnderlined = true;
            break;

        default:
            break;
        }
        if (singleUnderlined) {
            textChunk.setUnderline(1, -2);
        }
    }

    // background color
    if (currentRunBackgroundColor != null) {
        textChunk.setBackground(Converter.toAwtColor(currentRunBackgroundColor));
    }
    if (currentRunX != null) {
        this.currentRunX += textChunk.getWidthPoint();
    }
    return textChunk;
}

From source file:org.apache.poi.xwpf.converter.internal.itext.PDFMapper.java

License:Open Source License

@Override
protected void visitRun(XWPFRun run, IITextContainer pdfContainer) throws Exception {
    CTR ctr = run.getCTR();//from  w  w  w  .  ja va 2 s .c  o m
    // Get family name
    // Get CTRPr from style+defaults
    CTString rStyle = getRStyle(run);
    CTRPr runRprStyle = getRPr(super.getXWPFStyle(rStyle != null ? rStyle.getVal() : null));
    CTRPr rprStyle = getRPr(super.getXWPFStyle(run.getParagraph().getStyleID()));
    CTRPr rprDefault = getRPr(defaults);

    // Font family
    String fontFamily = getFontFamily(run, rprStyle, rprDefault);

    // Get font size
    float fontSize = run.getFontSize();

    // Get font style
    int fontStyle = Font.NORMAL;
    if (isBold(run, runRprStyle, rprStyle, rprDefault)) {
        fontStyle |= Font.BOLD;
    }
    if (isItalic(run, runRprStyle, rprStyle, rprDefault)) {
        fontStyle |= Font.ITALIC;
    }

    // Process color
    Color fontColor = null;
    String hexColor = getFontColor(run, runRprStyle, rprStyle, rprDefault);
    if (StringUtils.isNotEmpty(hexColor)) {
        if (hexColor != null && !"auto".equals(hexColor)) {
            fontColor = ColorRegistry.getInstance().getColor("0x" + hexColor);
        }
    }
    // Get font
    Font font = XWPFFontRegistry.getRegistry().getFont(fontFamily, options.getFontEncoding(), fontSize,
            fontStyle, fontColor);

    UnderlinePatterns underlinePatterns = run.getUnderline();

    boolean singleUnderlined = false;
    switch (underlinePatterns) {
    case SINGLE:
        singleUnderlined = true;
        break;

    default:
        break;
    }

    List<CTBr> brs = ctr.getBrList();
    for (@SuppressWarnings("unused")
    CTBr br : brs) {
        pdfContainer.addElement(Chunk.NEWLINE);
    }

    List<CTText> texts = run.getCTR().getTList();
    for (CTText ctText : texts) {

        Chunk aChunk = new Chunk(ctText.getStringValue(), font);
        if (singleUnderlined)
            aChunk.setUnderline(1, -2);

        pdfContainer.addElement(aChunk);
    }

    super.visitPictures(run, pdfContainer);

    // <w:lastRenderedPageBreak />
    List<CTEmpty> lastRenderedPageBreakList = ctr.getLastRenderedPageBreakList();
    if (lastRenderedPageBreakList != null && lastRenderedPageBreakList.size() > 0) {
        // IText Document#newPage must be called to generate page break.
        // But before that, CTSectPr must be getted to compute pageSize,
        // margins...
        // The CTSectPr <w:pPr><w:sectPr w:rsidR="00AA33F7"
        // w:rsidSect="00607077"><w:pgSz w:w="16838" w:h="11906"
        // w:orient="landscape" />...
        Stack<CTSectPr> sectPrStack = getSectPrStack();
        if (sectPrStack != null && !sectPrStack.isEmpty()) {
            CTSectPr sectPr = sectPrStack.pop();
            applySectPr(sectPr);
        }
        for (CTEmpty lastRenderedPageBreak : lastRenderedPageBreakList) {
            pdfDocument.newPage();
        }
    }
}

From source file:org.odftoolkit.odfdom.converter.internal.itext.stylable.StylableParagraph.java

License:Open Source License

@SuppressWarnings("unchecked")
public Element getElement() {
    if (!elementPostProcessed) {
        elementPostProcessed = true;/*from   w w w.  j a  va 2s . co  m*/

        // add space if this paragraph is empty
        // otherwise it's height will be zero
        boolean empty = true;
        ArrayList<Chunk> chunks = getChunks();
        for (Chunk chunk : chunks) {
            if (chunk.getImage() == null && chunk.getContent() != null && chunk.getContent().length() > 0) {
                empty = false;
                break;
            }
        }
        if (empty) {
            super.add(new Chunk("\u00A0")); // non breaking space
        }

        // adjust line height and baseline
        if (font != null && font.getBaseFont() != null) {
            // iText and open office computes proportional line height differently
            // [iText] line height = coefficient * font size
            // [open office] line height = coefficient * (font ascender + font descender + font extra margin)
            // we have to increase paragraph line height to generate pdf similar to open office document
            // this algorithm may be inaccurate if fonts with different multipliers are used in this paragraph
            float size = font.getSize();
            float ascender = font.getBaseFont().getFontDescriptor(BaseFont.AWT_ASCENT, size);
            float descender = -font.getBaseFont().getFontDescriptor(BaseFont.AWT_DESCENT, size); // negative value
            float margin = font.getBaseFont().getFontDescriptor(BaseFont.AWT_LEADING, size);
            float multiplier = (ascender + descender + margin) / size;
            if (multipliedLeading > 0.0f) {
                setMultipliedLeading(getMultipliedLeading() * multiplier);
            }

            // iText seems to output text with baseline lower than open office
            // we raise all paragraph text by some amount
            // again this may be inaccurate if fonts with different size are used in this paragraph
            float itextdescender = -font.getBaseFont().getFontDescriptor(BaseFont.DESCENT, size); // negative
            float textRise = itextdescender + getTotalLeading() - font.getSize() * multiplier;
            chunks = getChunks();
            for (Chunk chunk : chunks) {
                Font f = chunk.getFont();
                if (f != null) {
                    // have to raise underline and strikethru as well
                    float s = f.getSize();
                    if (f.isUnderlined()) {
                        f.setStyle(f.getStyle() & ~Font.UNDERLINE);
                        chunk.setUnderline(s * 1 / 17, s * -1 / 7 + textRise);
                    }
                    if (f.isStrikethru()) {
                        f.setStyle(f.getStyle() & ~Font.STRIKETHRU);
                        chunk.setUnderline(s * 1 / 17, s * 1 / 4 + textRise);
                    }
                }
                chunk.setTextRise(chunk.getTextRise() + textRise);
            }
        }

        // wrap this paragraph into a table if necessary
        if (wrapperCell != null) {
            // background color or borders were set
            wrapperCell.addElement(this);
            wrapperTable = createTable(wrapperCell);
            if (getIndentationLeft() > 0.0f || getIndentationRight() > 0.0f || getSpacingBefore() > 0.0f
                    || getSpacingAfter() > 0.0f) {
                // margins were set, have to wrap the cell again
                PdfPCell outerCell = createCell();
                outerCell.setPaddingLeft(getIndentationLeft());
                setIndentationLeft(0.0f);
                outerCell.setPaddingRight(getIndentationRight());
                setIndentationRight(0.0f);
                outerCell.setPaddingTop(getSpacingBefore());
                setSpacingBefore(0.0f);
                outerCell.setPaddingBottom(getSpacingAfter());
                setSpacingAfter(0.0f);
                outerCell.addElement(wrapperTable);
                wrapperTable = createTable(outerCell);
            }
        }
    }
    return wrapperTable != null ? wrapperTable : this;
}

From source file:org.pz.platypus.plugin.pdf.PdfOutfile.java

License:Open Source License

/**
 * Writes text to the PDF file. Because text can be written either as a Chunk or a Phrase
 * in iText, this method has to manage both entities within the larger context of a Paragraph.
 *
 * @param s the text to be written// w w w  .  j  a va  2  s.c  om
 */
public void emitText(String s) {
    assert (s != null) : "s parameter null in PdfOutfile.emitText()";

    // check if we are in an existing paragraph. If not, create a new one.
    if (iTPara == null) {
        startNewParagraph();
    }

    // now create a Chunk containing the text in String s using the font in pdfData
    final Chunk chunk = new Chunk(s, pdfData.getFont().getItextFont());

    // if strikethrough is on, then set it here for this chunk
    if (pdfData.getStrikethru()) {
        float lineLocation = pdfData.getFontSize() / DefaultValues.FONT_SIZE_TO_STRIKETHRU_RATIO;
        chunk.setUnderline(DefaultValues.UNDERLINE_THICKNESS, lineLocation);
    }

    if (pdfData.getUnderline().isInEffect()) {
        Underline ul = pdfData.getUnderline();
        chunk.setUnderline(ul.geTthickness(), ul.getPosition());
    }

    iTPara.add(chunk);
}