Find converted Word Text Boxes

I have a word form that I used for templating, and it has text boxes on it. When the text boxes are converted to PDF I would like to replace them with signature fields named the same name as the text inside the box.  I have tried looping through the PdfDocument, and the PdfLoadedDocument form fields but I cannot find the text boxes. I have attached a sample conversion.


EDIT: How can I find these text boxes and replace them?


Attachment: pdfFromDocxSyncfusion_df7a93b9.zip


9 Replies

DS Dharanya Sakthivel Syncfusion Team June 4, 2024 05:54 PM UTC

Hi Tim,

Based on the information provided, we have determined that your requirement is to locate the text boxes and replace them with form fields in a converted PDF. This can achieve modifying in Word document before converting the Word document to PDF using Syncfusion DocIO. 

To learn more about our DocIO Library, please refer to the documentation provided below.

UG documentation

Overview of .NET Word (DocIO) library | Syncfusion

GitHub samples

SyncfusionExamples/DocIO-Examples: DocIO is a .NET Word library to create, read, edit and convert Word documents without Microsoft Word or interop. (github.com)

Online demo

ASP.NET Core Word (DocIO) library - Sales Invoice Example - Syncfusion Demos


We have added the code for finding a textbox and replacing it with a form field in a Word document, and then converting the Word document to PDF. You can find the below given code


using Syncfusion.DocIO.DLS;
using Syncfusion.DocIO;
using Syncfusion.DocToPDFConverter;
using Syncfusion.Pdf;

FileStream fileStream = new FileStream(Path.GetFullPath(@"../../../Data/InputDocument.docx"), FileMode.Open, FileAccess.Read);
WordDocument document = new WordDocument(fileStream, FormatType.Docx);
List<Entity> entities = document.FindAllItemsByProperty(EntityType.TextBox, null, null);
//Text form field
foreach (WTextBox textBox in entities)
{
WParagraph ownerPara = textBox.OwnerParagraph;
ownerPara.ChildEntities.Remove(textBox);
ownerPara.AppendTextFormField("Signature");
}
//Creates an instance of the DocToPDFConverter - responsible for Word to PDF conversion
DocToPDFConverter converter = new DocToPDFConverter();
//Sets true to preserve the Word document form field as editable PDF form field in PDF document
converter.Settings.PreserveFormFields = true;
//Converts Word document into PDF document
PdfDocument pdfDocument = converter.ConvertToPDF(document);
//Saves the PDF file to file system
pdfDocument.Save(Path.GetFullPath(@"../../../Data/Result.pdf"));


Regards,
Dharanya.



TI Tim June 4, 2024 07:32 PM UTC

Thanks  Dharanya, how do I get the text from inside the textbox? I want to use that for the 



TI Tim June 4, 2024 07:35 PM UTC

I think I figured it out, Maybe...


((Syncfusion.DocIO.DLS.WParagraph)textBox.TextBoxBody.ChildEntities[0]).Text


Is that correct?



TI Tim June 4, 2024 08:32 PM UTC

Also, is there any way to control the size of the newly added field and set it to the same SIZE AND POSITION as the original text box? 



DS Dharanya Sakthivel Syncfusion Team June 5, 2024 01:06 PM UTC

Tim, based on the information provided, we have found that your requirement is to retrieve the text that is present inside the textbox and add it to the text form field. Then, apply the size and position to the form field from text box’s size and position. It is not feasible to set size and position for the form field to match the actual size and position of the text box.

As per your last update, we can get the text which present inside the textbox, but it fails if the textbox contains more than a paragraph. So, we have created a sample to get all the text from the textbox by iterating all items in it. You can find the sample in the attachment below.


Attachment: Replacetextboxasformfield_92ac1d89.zip


TI Tim June 5, 2024 03:07 PM UTC

Sigh, without being able to situate the newly added field in the exact same spot and location we cannot use this methodology. 

I appreciate the help, but it is back to the drawing board.

Just in case you have any ideas, I will explain how we currently do it, and see if you have any suggestions. 

Currently we use a mustache tag {{signature:foobar}} where foobar is the name of the signature. We then replace the mustache with a signature field that matches bounds of the text field. 

However, we wanted to be able to control the width of the field, so we thought we would try a textbox as that is sizable, and we thought it would show up as a form field. When it did not I made this post. 

We essentially would like a sizable word control that we could inline with our text and replace in the exact same size and location with a PDF signature field.



DS Dharanya Sakthivel Syncfusion Team June 6, 2024 05:56 PM UTC

Tim, we are currently reviewing the reported issue and understand the mentioned requirement is to get a sizable word control that can be embedded within text and then replaced with a PDF signature field, keeping the same size and position. We will update you with more information on 10th June, 2024.



TI Tim replied to Dharanya Sakthivel June 6, 2024 08:21 PM UTC

TYVM Dharanya



SB Sneha Biju Syncfusion Team June 7, 2024 02:27 PM UTC

Tim, we have found that your requirement is to replace a textbox with a signature in a converted PDF. It is not feasible to find a textbox in a PDF, so to meet your needs, we recommend inserting a text form field in place of the textbox in the Word document. You can adjust the size of the text form field by increasing the font size. This will allow you to easily replace a text form field with a signature in the converted PDF.

We have created a Word document with text form fields instead of textboxes and have also provided a sample demonstrating how to replace the text form field with a signature in the converted PDF. The sample and input Word document are attached below.

In the sample, we followed the following steps:
1. Open an existing Word document that has text form field
2. Converted the Word document to PDF
3. Load the PDF as a PDFLoadedDocument
4. Retrieve the text form field from the PDF
5. Replaced the Signature field in place of the text form field
6. Sign all the signature field with the appropriate value


Attachment: ReplaceTextFormFieldwithSignatureinPDF_7f36213b.zip

Loader.
Up arrow icon