Search code examples
c#winformspositionitext

How to place paragraphs in specific place using iTextSharp


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(); 
    }

Solution

  • 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...