Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu May 27, 2021 9:38 am

Hi ,

I need to add form field like textbox into an existing pdf based on the text position exits on the pdf . please let me know can we find the location based on the particular text and can add the textbox above the highlighted underline without hardcoding the page no and size of the text box . please find the attached sample pdf and please do the needful. Thanks in advance .

File Information:
addedTextBoxbasedonthestaticlocation - I have added the textbox over the underline using the static location

//create the form filed in the correct location
PdfTextBoxField textbox = new PdfTextBoxField(page, "SertifiDate_1");
textbox.Text = Convert.ToString(DateTime.Today.ToShortDateString());
textbox.ToolTip = "Date";
textbox.Bounds = new RectangleF(118, 310, 195, 15);//xlocation,ylocation,width,height
textbox.BorderWidth = 0.55f;
textbox.BorderStyle = PdfBorderStyle.Solid;
pdf.Form.Fields.Add(textbox);

textbox = new PdfTextBoxField(page, "SertifiSStamp_1");
//textbox.Text = "signatureTextBox";
textbox.ToolTip = "(Type Name)";
textbox.Bounds = new RectangleF(Xlocation, Ylocation + 20, 195, 15);//xlocation,ylocation,width,height
textbox.BorderWidth = 0.55f;
textbox.BorderStyle = PdfBorderStyle.Solid;
pdf.Form.Fields.Add(textbox);

needtoAddbasedontheParticularText - please let me know can we find the location based on the particular text and can add the textbox above the highlighted underline without hardcoding the page no and size of the text box

JayanthiJaya
 
Posts: 22
Joined: Thu Jan 21, 2021 6:06 am

Thu May 27, 2021 10:54 am

Hello,

Thanks for your inquiry.

Yes, please refer to the following code to find the text and add form field according to the text location. If there is any question, please provide your input file for further investigation. Thanks in advance.
Code: Select all
            PdfDocument pdf = new PdfDocument("test.pdf");
            PdfPageBase page = pdf.Pages[0];

            PdfTextFind[] result = null;
            float x,y,width,height;

            //Find text
            result = page.FindText("Acrobat", TextFindParameter.None).Finds;
            foreach (PdfTextFind find in result)
            {
                PointF position = find.Position;
                x = position.X;
                y = position.Y;
                width = find.Size.Width;
                height = find.Size.Height;

                //create the form filed in the correct location
                PdfTextBoxField textbox = new PdfTextBoxField(page, "SertifiDate_1");     
                textbox.Text = Convert.ToString(DateTime.Today.ToShortDateString());
                PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold);
                SizeF sizeF = font.MeasureString(textbox.Text);
                textbox.ToolTip = "Date";
                textbox.Bounds = new RectangleF(x + width + 20, y, sizeF.Width + 20, sizeF.Height);//xlocation,ylocation,width,height
                textbox.BorderWidth = 0.55f;
                textbox.BorderStyle = PdfBorderStyle.Solid;
                pdf.Form.Fields.Add(textbox);
            }

            pdf.SaveToFile("result.pdf");

Sincerely,
Brian
E-iceblue support
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Tue Jun 01, 2021 10:23 am

Hello,

Greetings from E-iceblue!
Did the code we provided work for you? Any feedback will be greatly appreciated.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.PDF