Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8776893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:09:47+00:00 2026-06-13T19:09:47+00:00

I’m having trouble to set underline and overline by using PdfContentByte in iText. I

  • 0

I’m having trouble to set underline and overline by using PdfContentByte in iText. I want to set underline to all field in sectionArea == 1 || section Area == 3 as mentioned in getFontForFormat. 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):

This problem can be solved by utilizing 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:09:49+00:00Added an answer on June 13, 2026 at 7:09 pm

    You’re making it yourself difficult by using PdfContentByte.showTextAligned(). Is there any reason why you don’t want to use ColumnText?

    With PdfContentByte, you have to handle the text state —beginText() and endText()—, the font —setFontAndSize()—, and you can only add String values. If you want to add lines (e.g. to underline), you need moveTo(), lineTo(), stroke() operations. 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. There’s some math involved.

    If you use ColumnText, you have the option of adding one line at a time using ColumnText.showTextAligned(). Or you can define a column using setSimpleColumn() and let iText take care of distributing the text over different lines. 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. In this case, iText does all the math for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.