Align Two Chunks to Left and Right on Same Line in iTextsharp

Introduction

During work I faced one requirement where I had to bind multiple chunks into a single phrase and show these chunks at both the ends; i.e., left and right end, of the same line. So here I will explain how I completed that point.

Here I take one Phrase, ph1, and I have two strings, Project name, which contain the name of the Project and Date. I want to display the Project name at the left side and Date at the right side in a single line.

  1. Document doc = new document()    
  2. Paragraph para = new Paragraph();    
  3. doc.open()    
  4. Chunk glue = new Chunk(new VerticalPositionMark());    
  5. Phrase ph1 = new Phrase();    
  6. ph1.Add(new Chunk(Environment.NewLine));    
  7. string projectname = "Project Name: " + dr["ProjectName"].ToString();    
  8. string date = dr["StartDate"].ToString() + "-" + dr["EndDate"].ToString();    
  9. Paragraph main = new Paragraph();    
  10. ph1.Add(new Chunk(projectname)); // Here I add projectname as a chunk into Phrase.    
  11. ph1.Add(glue); // Here I add special chunk to the same phrase.    
  12. ph1.Add(new Chunk('('+date +')')); // Here I add date as a chunk into same phrase.    
  13. main.Add(ph1);    
  14. para.Add(main);    
  15. doc.Add(para);    
  16. doc.close();   
Output



Summary

In this blog we saw how we can add multiple chunks into a single phrase, and how we can add two chunks at different locations using a single phrase.