3

How do I place text at a specific location on the pdf? I did a little bit of searching but didn't find anything too good. I have document.Add(new Paragraph("Date:" + DateTime.Now)); and I wanted to place that on a specific area on the pdf file.

My code:

   private void savePDF_Click(object sender, EventArgs e)
    {
        FileStream fileStream = new FileStream(nameTxtB.Text + "Repair.pdf", FileMode.Create, FileAccess.Write, FileShare.None);
        Document document = new Document();
        document.Open();
        iTextSharp.text.Rectangle rectangle = new iTextSharp.text.Rectangle(PageSize.LETTER);
        PdfWriter pdfWriter = PdfWriter.GetInstance(document, fileStream);

        iTextSharp.text.Image r3tsLogo = iTextSharp.text.Image.GetInstance("rt3slogo.PNG"); //creates r3ts logo
        iTextSharp.text.Image r3Info = iTextSharp.text.Image.GetInstance("R3 Information.PNG"); //creates r3 information text below r3ts logo

        r3tsLogo.SetAbsolutePosition(document.PageSize.Width - 375 - 0f, document.PageSize.Height - 130 - 0f); 
        r3Info.SetAbsolutePosition(document.PageSize.Width - 365 - 0f, document.PageSize.Height - 170 - 0f); //higher the number in height the lower the place of text on paper
                                   //less  number will result in text more to right in width

        //increase size of picture
        r3tsLogo.ScalePercent(120); 
        r3Info.ScalePercent(65);

//---------------adds all images to pdf file --------------------------------- 
        document.Add(r3tsLogo);
        document.Add(r3Info);
        document.Add(new Paragraph("Date:" + DateTime.Now));




        document.Close(); 
    }
Joris Schellekens
  • 8,483
  • 2
  • 23
  • 54
MSD
  • 87
  • 2
  • 5
  • 14

2 Answers2

13

Assuming that you know how to add images at an absolute position (see Joris' answer), but looking at how to add text, then the answer to your question is: use ColumnText.

If you only need to add a single line that doesn't need to be wrapped, you can use the ShowTextAligned() method:

ColumnText.showTextAligned(writer.DirectContent,
     Element.ALIGN_CENTER, new Phrase("single line"), x, y, rotation);

In this line of code, x and y are the coordinates for the middle of the text (other possible alignment values are ALIGN_LEFT and ALIGN_RIGHT). The rotation parameter defines a rotation in degrees. Note that the text "single line" won't be wrapped. You can add text that "falls off the page" this way if the text you're adding is too long.

If you want to add text inside a specific rectangle, then you need to define the column using a Rectangle object:

ColumnText ct = new ColumnText(writer.DirectContent);
ct.setSimpleColumn(new Rectangle(0, 0, 523, 50));
ct.addElement(new Paragraph("This could be a very long sentence that needs to be wrapped"));
ct.go();

If you provide more text than fits the rectangle, that text will not be rendered. However, it will still be available in the ct object so that you can add that remaining text at another position.

All of this has been asked and answered before:

Single line:

Multiple lines:

Did I have to search long for these examples? No, I found them on the official web site under Absolute Positioning of text.

Wisdom is there for those who search...

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • How to change Font in this one ColumnText.showTextAligned(writer.DirectContent, Element.ALIGN_CENTER, new Phrase("single line"), x, y, rotation); ? – rahularyansharma Jun 16 '21 at 05:24
1

This concept is thoroughly explained in the book 'iText in action'. Which can be found on the website.

http://developers.itextpdf.com/examples/itext-action-second-edition/chapter-3

Short code sample (check the site for other examples):

// step 1
Document document = new Document(PageSize.POSTCARD, 30, 30, 30, 30);

// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));

// step 3
document.open();

// step 4
// Create and add a Paragraph
Paragraph p = new Paragraph("Foobar Film Festival", new Font(FontFamily.HELVETICA, 22));
p.setAlignment(Element.ALIGN_CENTER);
document.add(p);

// Create and add an Image
Image img = Image.getInstance(RESOURCE);
img.setAbsolutePosition(
        (PageSize.POSTCARD.getWidth() - img.getScaledWidth()) / 2,
        (PageSize.POSTCARD.getHeight() - img.getScaledHeight()) / 2);
document.add(img);
Joris Schellekens
  • 8,483
  • 2
  • 23
  • 54
  • I think that the problem isn't positioning images, but positioning `"Date:" + DateTime.Now)` at an absolute position. Of course: this is also a question that has been answered many times before. – Bruno Lowagie Sep 05 '17 at 06:11