Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Just set the <code>Paragraph.Align</code> property:</p> <pre><code>using (Document document = new Document()) { PdfWriter.GetInstance( document, STREAM ); document.Open(); for (int i = 1; i &lt; 11; ++i) { Paragraph p = new Paragraph(string.Format( "Paragraph {0}", i )); p.Alignment = Element.ALIGN_RIGHT; document.Add(p); } } </code></pre> <p>It even works with a long string like this:</p> <pre><code>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); </code></pre> <p><img src="https://i.stack.imgur.com/Sw8ZP.png" alt="enter image description here"></p> <p><strong>EDIT</strong>: After looking at the "picture" you drew...</p> <p>It doesn't match with the title. The <strong>only</strong> way you can align individual <code>Paragraph</code> objects like your picture is if the "paragraph" does <strong>NOT</strong> exceed the <code>Document</code> 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 <strong>will</strong> fit on a single line.</p> <p>With that said, if you want that type of alignment you need to:</p> <ol> <li>Calculate the widest value from the collection of strings you intend to use.</li> <li>Use that value to set a common left indentation value for the <code>Paragraph</code>s.</li> </ol> <p>Something like this:</p> <pre><code>using (Document document = new Document()) { PdfWriter.GetInstance( document, STREAM ); document.Open(); List&lt;Chunk&gt; chunks = new List&lt;Chunk&gt;(); float widest = 0f; for (int i = 1; i &lt; 5; ++i) { Chunk c = new Chunk(string.Format( "Paragraph {0}", Math.Pow(i, 24) )); float w = c.GetWidthPoint(); if (w &gt; 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); } } </code></pre> <p><img src="https://i.stack.imgur.com/ArqXS.png" alt="enter image description here"></p> <p><strong>UPDATE 2</strong>:</p> <p>After reading your updated question, here's another option that lets you add text to the left side of the "container":</p> <pre><code>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) =&gt; x.Length &gt; 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); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload