I’m creating a PDF and everything works fine but one thing. I’m inserting textboxes that can be typed in by the user and am having trouble with width. If I use the following
builder.InsertTextInput("", TextFormFieldType.Regular, "", "", 0);
I get a very small, one space textbox. If I insert spaces in either of the double quoted paramaters it inserts spaces in the textbox. When the user clicks in the box and happens to click near the end it starts their text there and types into space that’s not visible. If I do the following instead
builder.InsertHtml(@"<input type='text' size='160' />");
I get a textbox but don’t seem to have any control over the width of it. I’ve tried different sizes (px, em, etc), and tried adding inline css instead.
Is there a way to insert a wider but empty and typeable textbox?
@jeffreyhamby
To insert a wider, empty, and typeable textbox in a PDF using Aspose.Words, you can utilize the InsertTextInput
method of the DocumentBuilder
class. This method allows you to specify the maximum length of the text input, which can help control the width of the textbox.
Here’s an example of how to create a wider textbox:
import com.aspose.words.*;
public class CreateTextInput {
public static void main(String[] args) throws Exception {
// Create a new document and a DocumentBuilder
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Write a prompt for the user
builder.write("Please enter text here: ");
// Insert a text input field with a specified maximum length
// Adjust the maxLength parameter to control the width
builder.insertTextInput("TextInput1", TextFormFieldType.REGULAR, "", "Placeholder text", 50);
// Save the document
doc.save("TextInputExample.docx");
}
}
In this example, the maxLength
parameter is set to 50
, which allows for a wider textbox. You can adjust this value to fit your needs. The placeholder text will be visible until the user starts typing, ensuring that they know where to enter their input.
If you find that the textbox is still not wide enough, consider adjusting the maxLength
value further or using CSS styles if you are inserting HTML directly. However, using the InsertTextInput
method is generally the recommended approach for creating form fields in Aspose.Words.
For more detailed examples and information, you can refer to the official Aspose documentation on form fields and text inputs.
Sources:
[1]: ExFormFields.java
Thank you for your response. Here’s the result of that for me:

Unfortunately that only makes the textbox as wide as the placeholder text used rather than the value in the last parameter. It also leads to the same issue as the spaces, when the user clicks in the textbox the text remains and their typing begins wherever the cursor landed.
I’m looking for an empty but wide textbox.
@jeffreyhamby I am afraid, there is no way to specify width of input text form field other than inserting some dummy text.