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
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 |
|
|
GitHub samples |
|
|
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.
Thanks Dharanya, how do I get the text from inside the textbox? I want to use that for the
I think I figured it out, Maybe...
((Syncfusion.DocIO.DLS.WParagraph)textBox.TextBoxBody.ChildEntities[0]).Text
Is that correct?
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?
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.
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.
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.
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