简体   繁体   English

如何设置下划线到PdfContentByte - iText

[英]How to set underline to PdfContentByte — iText

I'm having trouble to set underline and overline by using PdfContentByte in iText. 我在使用iText中的PdfContentByte设置下划线和上线时遇到了麻烦。 I want to set underline to all field in sectionArea == 1 || 我想在sectionArea == 1 ||中为所有字段设置下划线 section Area == 3 as mentioned in getFontForFormat. 在getFontForFormat中提到的区域== 3。 So far i can only do bold style and i need it to be underlined and overlined too. 到目前为止,我只能做大胆的风格,我需要强调和强调。 Here is the code: 这是代码:

public void doOutputField(Field field) {
    String fieldAsString = field.toString();
    BaseFont baseFont = getFontForFormat(field);
    float fontSize = 11;

    Point bottomLeft = bottomLeftOfField(field, 11, baseFont);

    int align;

    align = PdfContentByte.ALIGN_LEFT;

    //PdfContentByte content
    content.beginText();
    content.setFontAndSize(baseFont, fontSize);

    content.setColorFill(Color.BLACK);

    double lineHeight = field.getOutputHeight();

    content.showTextAligned(align, fieldAsString, (float) bottomLeft.x,
                (float) bottomLeft.y, 0f);

    bottomLeft.y -= lineHeight;

    content.endText();
}

public BaseFont getFontForFormat(Field field) {

    try {
        if (field.getSection().getArea().getArea() == 1
                || field.getSection().getArea().getArea() == 3) {
            BaseFont bf = BaseFont.createFont(BaseFont.TIMES_BOLD,
                    BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            return bf;
        } else {
            BaseFont bf = BaseFont.createFont("Times-Roman",
                    BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            return bf;
        }
    } catch (Exception e) {
    }
    return null;
}

Thanks in advance 提前致谢

Edit (Solved by Bruno Lowagie): 编辑(由Bruno Lowagie解决):

This problem can be solved by utilizing ColumnText. 利用ColumnText可以解决这个问题。

  if (field.getSection().getArea().getArea() == 1
            || field.getSection().getArea().getArea() == 3) {

        Chunk chunk = new Chunk(fieldAsString);
        chunk.setUnderline(+1f, -2f);

        if (field.getSection().getArea().getArea() == 3) {
            chunk.setUnderline(+1f, (float) field.getBoundHeight());
        }

          Font font = new Font();
          font.setFamily("Times Roman");
          font.setStyle(Font.BOLD);
          font.setSize((float) 11);
          chunk.setFont(font);

          Paragraph p = new Paragraph();
          p.add(chunk);

         ColumnText ct = new ColumnText(content);
         ct.setSimpleColumn(p, (float)bottomLeft.x, (float)bottomLeft.y,
             (float)field.getBoundWidth() + (float)bottomLeft.x,
             (float)field.getBoundHeight() + (float)bottomLeft.y,
             (float)lineHeight, align);
             try {
             ct.go();
             } catch (DocumentException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
             }
    }

Thanks 谢谢

You're making it yourself difficult by using PdfContentByte.showTextAligned() . 使用PdfContentByte.showTextAligned() ,你自己很难PdfContentByte.showTextAligned() Is there any reason why you don't want to use ColumnText ? 你有什么理由不想使用ColumnText吗?

With PdfContentByte , you have to handle the text state — beginText() and endText() —, the font — setFontAndSize() —, and you can only add String values. 使用PdfContentByte ,您必须处理文本状态 - beginText()endText() - ,font - setFontAndSize() - ,并且您只能添加String值。 If you want to add lines (eg to underline), you need moveTo() , lineTo() , stroke() operations. 如果要添加行(例如,下划线),则需要moveTo()lineTo()stroke()操作。 These operators need coordinates, so you'll need to measure the size of the line using the BaseFont in combination with the String and the font size. 这些运算符需要坐标,因此您需要使用BaseFont结合String和字体大小来测量行的大小。 There's some math involved. 有一些数学涉及。

If you use ColumnText , you have the option of adding one line at a time using ColumnText.showTextAligned() . 如果使用ColumnText ,则可以选择使用ColumnText.showTextAligned()一次添加一行。 Or you can define a column using setSimpleColumn() and let iText take care of distributing the text over different lines. 或者,您可以使用setSimpleColumn()定义列,并让iText负责在不同的行上分发文本。 In both cases, you don't have to worry about handling the text state, nor about the font and size. 在这两种情况下,您都不必担心处理文本状态,也不必担心字体和大小。 ColumnText accepts Phrase objects, and these objects consists of Chunk objects for which you can define underline and overline values. ColumnText接受Phrase对象,这些对象由Chunk对象组成,您可以为其定义下划线和上划线值。 In this case, iText does all the math for you. 在这种情况下,iText会为您完成所有数学运算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM