5

I have created rectangle using PdfContentByte. Now I want to adda text inside this rectangle. How can I do this. If anybody have idea please share with me.My rectangle code is

 Document doc = new Document(new Rectangle(570, 924f));
 PdfWriter writer = PdfWriter.GetInstance(doc,Response.OutputStream);
 PdfContentByte cb = writer.DirectContent;
 cb.Rectangle(doc.PageSize.Width -90f, 830f, 50f,50f);
 cb.Stroke();
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
Semil Sebastian
  • 519
  • 2
  • 9
  • 24
  • If you download the [official documentation](http://pages.itextpdf.com/ebook-stackoverflow-questions.html) (it's free, so please do), you'll discover that your question was answered before: [How to fit a String inside a rectangle?](http://stackoverflow.com/questions/13526043/how-to-fit-a-string-inside-a-rectangle) [How can I truncate text within a bounding box?](http://stackoverflow.com/questions/13558135/how-can-i-truncate-text-within-a-bounding-box) and so on... – Bruno Lowagie Jul 01 '15 at 07:10
  • @ Bruno Lowagie, K let me check it once – Semil Sebastian Jul 01 '15 at 07:12
  • @ Bruno Lowagie, I checked that but I didint get how to apply here? – Semil Sebastian Jul 01 '15 at 07:14
  • Huh? Did you check the other examples in the documentation? I'll write you some code. – Bruno Lowagie Jul 01 '15 at 07:22
  • 1
    @Bruno Lowagie, If I buy only I can refer the Documentation. :) – Semil Sebastian Jul 01 '15 at 07:25
  • No, you can download the book for free. All you need to do, is to register. – Bruno Lowagie Jul 01 '15 at 07:26

1 Answers1

9

You are drawing a rectangle like this:

 PdfContentByte cb = writer.DirectContent;
 cb.Rectangle(doc.PageSize.Width -90f, 830f, 50f,50f);
 cb.Stroke();

This corresponds with this Rectangle:

Rectangle rect = new Rectangle(
    doc.PageSize.Width - 90f, 830f,
    doc.PageSize.Width - 40f, 880f);

You can add text inside this rectangle like this:

 ColumnText ct = new ColumnText(cb);
 ct.SetSimpleColumn(rect);
 ct.AddElement(new Paragraph("This is the text added in the rectangle"));
 ct.Go();
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165