Adding a form field to existing PDF using Aspose.PDF for .NET

Hi Aspose Forum,

I am trying to batch add a form and a few fields (bordered) to existing pdfs to support our grading process. While I have been able to create fields, now and again, using the sample code - I am not able to add fields as I would like them deliberately.

For example, this code fails to add a border (essential) and the font is arbitrary and super LARGE but it appears I need to crack open the facade library to change that - and truthfully that hasn’t worked either… no changes.

//Create a field
TextBoxField textBoxField = new TextBoxField(pdfDocument.Pages[2], new Aspose.Pdf.Rectangle(100, 200, 300, 300));
textBoxField.PartialName = “textbox1”;
textBoxField.Value = “Text Box”;

// border
Border border = new Border(textBoxField);
border.Width = 5;
border.Dash = new Dash(1, 1);
textBoxField.Value = “textbox1”;

//Add field to the document
pdfDocument.Form.Add(textBoxField, 1);

I haven’t been able to find much on the web so I suspect either there is a low need to do what I’m doing OR I’m way off base!

Would you advise me on how to get a form and 6 or so fields in an existing pdf?

kindly,
linda

Hi Linda,

Thanks for contacting support.

The above stated code is used to add form fields in PDF document and in order to control the size of text inside form field, please try using DefaultAppearance.FontSize property of TextBoxField class. For your reference, I have also attached the output generated with following code snippet.

[C#]

Document pdfDocument = new Document();

pdfDocument.Pages.Add();

//Create a field

TextBoxField textBoxField = new TextBoxField(pdfDocument.Pages[1], new Aspose.Pdf.Rectangle(100, 200, 300, 300));

textBoxField.PartialName = "textbox1";

textBoxField.Value = "Text Box";

textBoxField.DefaultAppearance.FontSize = 6;

// border

Border border = new Border(textBoxField);

border.Width = 5;

border.Dash = new Dash(1, 1);

textBoxField.Value = "textbox1";

//Add field to the document

pdfDocument.Form.Add(textBoxField, 1);

pdfDocument.Save(“c:/pdftest/PDF_With_FormField.pdf”);

Thank you so much for your reply!

The borders do not show upon creation - my work around is to re-open the file and use facades - in which case the borders appear correctly.

kindly,
linda

Hi Linda,


Thanks for sharing the details.

I have logged the issue regarding border not being rendered in our issue tracking system as PDFNET-41073. We will further look into the details of this problem and will keep you posted on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.

@lindadeefay

We added two lines to the code and from version 24.3 with this code snippet field border appears.

var pdfDocument = new Document();
pdfDocument.Pages.Add();
// Create a field
TextBoxField textBoxField = new TextBoxField(pdfDocument.Pages[1], new Aspose.Pdf.Rectangle(100, 200, 300, 300));
textBoxField.PartialName = "textbox1";
textBoxField.Value = "Text Box";
textBoxField.DefaultAppearance.FontSize = 6;

// border
var border = new Border(textBoxField);
border.Width = 5;
border.Dash = new Dash(1, 1);
textBoxField.Value = "textbox1";

textBoxField.Characteristics.Border = System.Drawing.Color.Black;  // <---------  This line added.
textBoxField.Border = border;  // <---------  This line added.

// Add field to the document
pdfDocument.Form.Add(textBoxField, 1);
pdfDocument.Save(dataDir + "41073.pdf");