1

iText で PdfContentByte を使用して下線と上線を設定するのに問題があります。sectionArea == 1 || のすべてのフィールドにアンダーラインを設定したい || getFontForFormat で述べたように、セクション エリア == 3。これまでのところ、太字のスタイルしかできず、下線と上線も必要です。コードは次のとおりです。

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;
}

前もって感謝します

編集(Bruno Lowagieが解決):

この問題は、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();
             }
    }

ありがとう

4

1 に答える 1