Ashwani
Ashwani

Reputation: 81

How to bottom align text in a textbox control in pdf file using itextsharp

I need to bottom align text which I'm getting from TextBox in PDF file.

I'm using below code to get text from TextBox in pdf.

 for (int i = 1; i <= reader.NumberOfPages; i++)
        {

            iTextSharp.text.pdf.PdfArray array = reader.GetPageN(i).GetAsArray(iTextSharp.text.pdf.PdfName.ANNOTS);
            if (array == null) continue;
            for (int j = 0; j < array.Size; j++)
            {
                iTextSharp.text.pdf.PdfDictionary annot = array.GetAsDict(j);

                iTextSharp.text.pdf.PdfString text = annot.GetAsString(iTextSharp.text.pdf.PdfName.CONTENTS);

            }
        }

I'm using ItextSharp Library. Error screenshot

enter image description here

Upvotes: 1

Views: 2118

Answers (1)

Semil Sebastian
Semil Sebastian

Reputation: 39

PdfReader reader = new PdfReader(SOURCE); 
PdfStamper stamper = new PdfStamper(reader, TARGET); 

TextField tf = new TextField(stamper.getWriter(), new Rectangle(300, 400, 
500, 420), text);
stamper.addAnnotation(tf.getTextField(), 1);

PdfContentByte overContent = stamper.getOverContent(1);
BaseFont baseFont = BaseFont.createFont();
overContent.setFontAndSize(baseFont, 12);
overContent.beginText();
overContent.showTextAligned(PdfContentByte.ALIGN_BOTTOM, text, 300,
405, 0);
overContent.endText();
stamper.close ();

or try this also

using (PdfStamper stamper = new PdfStamper(new PdfReader(inputFile), File.Create(outputFile)))
{
TextField tf = new TextField(stamper.Writer, new iTextSharp.text.Rectangle(0, 0, 100, 300), "Vertical");
stamper.AddAnnotation(tf.GetTextField(), 1);
}

Upvotes: 1

Related Questions