Good Day
Hi Pieter,
Thanks for your inquiry. Please check the sample code snippet for adding a textbox field in table cells and how to fit it into the cell.Hopefully, it will help you to accomplish the task.
Document doc = new Document();
Aspose.Pdf.Page page = doc.Pages.Add();
Aspose.Pdf.Table table1 = new Aspose.Pdf.Table();
table1.ColumnWidths = "120 120 120";
page.Paragraphs.Add(table1);
Aspose.Pdf.Row row1 = table1.Rows.Add();
row1.FixedRowHeight = 15;
Aspose.Pdf.Cell cell1 = row1.Cells.Add("Name");
Aspose.Pdf.Cell cell2 = row1.Cells.Add("City");
Aspose.Pdf.Cell cell3 = row1.Cells.Add("State");
TextBoxField field = new TextBoxField();
field.PartialName = "TextBoxName";
field.Value = "John";
field.Width = 120;
field.Height = 15;
field.Multiline = true;
TextBoxField field2 = new TextBoxField();
field2.PartialName = "TextBoxCity";
field2.Value = "New York";
field2.Width = 120;
field2.Height = 15;
TextBoxField field3 = new TextBoxField();
field3.PartialName = "TextBoxState";
field3.Value = "NY";
field3.Width = 120;
field3.Height = 15;
Aspose.Pdf.Row row2 = table1.Rows.Add();
Aspose.Pdf.Cell cell4 = row2.Cells.Add();
Aspose.Pdf.Cell cell5 = row2.Cells.Add();
Aspose.Pdf.Cell cell6 = row2.Cells.Add();
cell4.Paragraphs.Add(field);
cell5.Paragraphs.Add(field2);
cell6.Paragraphs.Add(field3);
Aspose.Pdf.Row row3 = table1.Rows.Add();
row3.FixedRowHeight = 80;
// Aspose.Pdf.Cell commentTbCell = row3.Cells.Add();
Aspose.Pdf.Cell commentTbCell = new Aspose.Pdf.Cell();
commentTbCell.ColSpan = 3;
TextBoxField textBoxField11 = new TextBoxField();
textBoxField11.PartialName = "Comment_tb";
textBoxField11.Height = 70;
textBoxField11.Multiline = true;
textBoxField11.Width = 360;
commentTbCell.Paragraphs.Add(textBoxField11);
row3.Cells.Add(commentTbCell);
// Save modified PDF
doc.Save(myDir + "PDFFormOutput.pdf");
Please feel free to contact us for any further assistance.
Best Regards,
Thanks for your response. Greatly appreciated.
Hi Pieter,
Hello,
what if i dont know the height of the table row in which i want to add the Textboxfield, because content is written dynamically in that row, for example in a preceding cell? The Textbox size should be the same size then. How can i do that?
Thanks for contacting support.
You may add TextboxField without specifying height and width and it will be added with default dimensions. However, in case you are facing any issue while achieving your requirement, please share complete sample code snippet along with sample PDF document. We will test the scenario in our environment and share our feedback accordingly.
Aspose Support team,
I am using Aspose.pdf library for generting report. In my report, I have table which has more rows. So table is extending to second page. I have added texbox in one of the table cell. I followed sample code you mentioned in this email thread. If I have less record, then it is loading texbox successfully. When table is extending to second page if more record exist, then adding texbox in table cell on the first record of second page is throwing error. Also If height and width are not mentioned while creating textbox, then textbox field is not showing in table cell. I created below method to create texbox
Calling method
row = table.Rows.Add();
row.FixedRowHeight = 20;
cell = row.Cells.Add(“test”);
cell.ColSpan = 4;
cell.Border = new BorderInfo(BorderSide.All, 0F);
cell.Alignment = HorizontalAlignment.Right;
cell = row.Cells.Add();
cell.Paragraphs.Add(GetTextBoxField(document, 100, 15));
public TextBoxField GetTextBoxField(
Document document,
float width,
float height,
bool underlineRequired = false,
bool isBorder = false,
bool isMultiline = false
)
{
TextBoxField textBox = new TextBoxField(document);
if (underlineRequired)
textBox.Border = new Border(textBox) { Width = 1, Style = BorderStyle.Underline };
if (isBorder)
textBox.Border = new Border(textBox) { Width = 1, Style = BorderStyle.Solid };
textBox.Characteristics.Border = System.Drawing.Color.LightGray;
textBox.Height = height;
textBox.Width = width;
textBox.DefaultAppearance.FontSize = ReportDesignSystem.ReportFontSize;
textBox.Multiline = isMultiline;
return textBox;
}
Would you please try to use Document.ProcessParagraphs() method before adding textbox for the second page where Table is extended? In case issue still persists, we request a minimal code sample that we can use to replicate the issue in our environment and address it accordingly.