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 7046739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:41:11+00:00 2026-05-28T02:41:11+00:00

I don’t know the width of the texts in the textblock beforehand, and I

  • 0

I don’t know the width of the texts in the textblock beforehand, and I want the the textblock to be aligned to the right like this, while still having the individual lines left-aligned:

                                Mr. Petersen      |
                                Elmstreet 9       |
                                888 Fantastic City|

(| donotes the right edge of the document)

It should be simple, but I can’t figure it out.

I’ve tried to put all the text in a paragraph and set paragraph.Alignment = Element.ALIGN_RIGHT, but this will rightalign the individual lines.

I’ve tried to put the paragraph in a cell inside a table, and rightalign the cell, but the cell just takes the full width of the table.

If I could just create a container that would take only the needed width, I could simply rightalign this container, so maybe that is really my question.

  • 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-05-28T02:41:12+00:00Added an answer on May 28, 2026 at 2:41 am

    Just set the Paragraph.Align property:

    using (Document document = new Document()) {
      PdfWriter.GetInstance(
        document, STREAM
      );
      document.Open();
      for (int i = 1; i < 11; ++i) {
        Paragraph p = new Paragraph(string.Format(
          "Paragraph {0}", i
        ));
        p.Alignment = Element.ALIGN_RIGHT;
        document.Add(p);
      } 
    }
    

    It even works with a long string like this:

    string longString = @"
    iText ® is a library that allows you to create and manipulate PDF documents. It enables developers looking to enhance web- and other applications with dynamic PDF document generation and/or manipulation.
    ";
    Paragraph pLong = new Paragraph(longString);
    pLong.Alignment = Element.ALIGN_RIGHT;
    document.Add(pLong);
    

    enter image description here

    EDIT:
    After looking at the “picture” you drew…

    It doesn’t match with the title. The only way you can align individual Paragraph objects like your picture is if the “paragraph” does NOT exceed the Document object’s “content” box (for a lack of a better term). In other words, you won’t be able to get that type of alignment if the amount of text exceeds that which will fit on a single line.

    With that said, if you want that type of alignment you need to:

    1. Calculate the widest value from the collection of strings you intend to use.
    2. Use that value to set a common left indentation value for the Paragraphs.

    Something like this:

    using (Document document = new Document()) {
      PdfWriter.GetInstance(
        document, STREAM
      );
      document.Open();
      List<Chunk> chunks = new List<Chunk>();
      float widest = 0f;
      for (int i = 1; i < 5; ++i) {
        Chunk c = new Chunk(string.Format(
          "Paragraph {0}", Math.Pow(i, 24)
        ));
        float w = c.GetWidthPoint();
        if (w > widest) widest = w;
        chunks.Add(c);
      } 
      float indentation = document.PageSize.Width
        - document.RightMargin
        - document.LeftMargin
        - widest
      ;
      foreach (Chunk c in chunks) {
        Paragraph p = new Paragraph(c);
        p.IndentationLeft = indentation;
        document.Add(p);
      }
    }
    

    enter image description here

    UPDATE 2:

    After reading your updated question, here’s another option that lets you add text to the left side of the “container”:

    string textBlock = @"
    Mr. Petersen
    Elmstreet 9
    888 Fantastic City  
    ".Trim();
    // get the longest line to calcuate the container width  
    var widest = textBlock.Split(
        new string[] {Environment.NewLine}
        , StringSplitOptions.None
      )
      .Aggregate(
        "", (x, y) => x.Length > y.Length ? x : y
      )
    ;
    // throw-away Chunk; used to set the width of the PdfPCell containing
    // the aligned text block
    float w = new Chunk(widest).GetWidthPoint();
    PdfPTable t = new PdfPTable(2);
    float pageWidth = document.PageSize.Width 
      - document.LeftMargin 
      - document.RightMargin
    ;
    t.SetTotalWidth(new float[]{ pageWidth - w, w });
    t.LockedWidth = true;  
    t.DefaultCell.Padding = 0;
    // you can add text in the left PdfPCell if needed
    t.AddCell("");
    t.AddCell(textBlock);
    document.Add(t);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't know if this is the right place to ask this, but I will
Don't know if this is possible, but I have some code like this: val
Don't know if I worded the question right, but basically what I want to
I don't know why, but this code worked for me a month ago... maybe
Don't know if I'm using the not selector incorrectly but I want everything clicked
Don't know why this is happening, but after submitting a form via JS (using
Don't know why this doesn't work. I can't do implicit animation for a newly
Don't know why, but sometimes LocationManager is still working also after closing application. I
Don't know what to do with this error. How to add data in SQL
Don't know if this is an eclipse specific problem but whenever I declare a

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.