Search code examples
itextsharp

adding a textbox to the right corner of the existing pdf using ITextSharp in C#


I tiied to add a TextBox to the right corner of the existing pdf using c#, but im unable to get it done. I have wrote the following code,but it is not helping in solving the problem, can any body please suggest me

using (MemoryStream stream = new MemoryStream())
{
        PdfReader reader = new PdfReader(bytes);               
        PdfReader.unethicalreading = true;
        Paragraph p = new Paragraph();
        Document doc = new Document();

        using (PdfStamper stamper = new PdfStamper(reader, stream))
        {
            PdfContentByte canvas = stamper.GetOverContent(1);
            iTextSharp.text.Rectangle size = reader.GetPageSizeWithRotation(1);


            //PdfContentByte cb = null;
            //PdfImportedPage page;
            int pages = reader.NumberOfPages;                    
            for (int i = 1; i <= pages; i++)
            {
                var size1 = reader.GetPageSize(i);
                w = size1.Width;
                h = size1.Height;
                stamper.FormFlattening = true;

                TextField tf = new TextField(stamper.Writer, new iTextSharp.text.Rectangle(0, 0, 300, 100), displaytext);
                //Change the orientation of the text
                tf.Rotation = 0;
                stamper.AddAnnotation(tf.GetTextField(), i);
            }
        }
        bytes = stream.ToArray();
}
File.WriteAllBytes(str, bytes);

Solution

  • As the OP clarified in comments to the question, he wants

    • to add the text as a page content in the right bottom corner of the page and
    • the page content previously existing there to be removed.

    A simple implementation of this would include

    • first covering the existing page content with a filled rectangle and
    • then writing text there.

    These tasks can be achieved with these helper methods:

    void EmptyTextBoxSimple(PdfStamper stamper, int pageNumber, Rectangle boxArea, BaseColor fillColor)
    {
        PdfContentByte canvas = stamper.GetOverContent(pageNumber);
        canvas.SaveState();
        canvas.SetColorFill(fillColor);
        canvas.Rectangle(boxArea.Left, boxArea.Bottom, boxArea.Width, boxArea.Height);
        canvas.Fill();
        canvas.RestoreState();
    }
    

    and

    ColumnText GenerateTextBox(PdfStamper stamper, int pageNumber, Rectangle boxArea)
    {
        PdfContentByte canvas = stamper.GetOverContent(pageNumber);
        ColumnText columnText = new ColumnText(canvas);
        columnText.SetSimpleColumn(boxArea);
        return columnText;
    }
    

    E.g. like this:

    using (PdfReader reader = new PdfReader(source))
    using (PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create)))
    {
        Rectangle cropBox = reader.GetCropBox(1);
        Rectangle bottomRight = new Rectangle(cropBox.GetRight(216), cropBox.Bottom, cropBox.Right, cropBox.GetBottom(146));
        EmptyTextBoxSimple(stamper, 1, bottomRight, BaseColor.WHITE);
        ColumnText columnText = GenerateTextBox(stamper, 1, bottomRight);
        columnText.AddText(new Phrase("Some test text to draw into a text box in the lower right corner of the first page"));
        columnText.Go();
    }
    

    For this source page

    source page

    the sample code generates this

    result page

    Addendum

    In a comment the OP indicated

    it is working for all files but for some pdf files it is displaying in the middle

    Eventually he supplied a sample file for which the issue occurs. And indeed, with this file the issue could be reproduced.

    The cause for the issue is that the pages in the sample file use page rotation, something that iText (only) partially allows users to ignore. In particular iText automatically rotates text to be upright after rotation and transforms coordinates, but when retrieving the cropbox of a page, one still has to apply rotation before making use of it coordinates. Thus, a more complete example would be like this:

    using (PdfReader reader = new PdfReader(source))
    using (PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create)))
    {
        Rectangle cropBox = reader.GetCropBox(1);
        int rotation = reader.GetPageRotation(1);
        while (rotation > 0)
        {
            cropBox = cropBox.Rotate();
            rotation -= 90;
        }
        Rectangle bottomRight = new Rectangle(cropBox.GetRight(216), cropBox.Bottom, cropBox.Right, cropBox.GetBottom(146));
        EmptyTextBoxSimple(stamper, 1, bottomRight, BaseColor.WHITE);
        ColumnText columnText = GenerateTextBox(stamper, 1, bottomRight);
        columnText.AddText(new Phrase("Some test text to draw into a text box in the lower right corner of the first page"));
        columnText.Go();
    }