Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Tue Sep 06, 2022 5:46 pm

I have added form field to an existing PDF using "PdfPageBase page = doc.Pages[0];" . Then i try to open created pdf for setting form field values from database but i am not able to read form field
But when i added form field to PDF created by " PdfPageBase page = doc.Pages.Add()" i am able to read form field.
I am using FreeSpire.pdf .net library - version 8.6.0

Below is my code
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("Blank.pdf");
//Add a page
//PdfPageBase page = doc.Pages.Add();

doc.AllowCreateForm = (doc.Form == null) ? true : false;
PdfPageBase page = doc.Pages[0];
SizeF size = page.Size;
PdfPageBase newPage;

//Initialize x and y coordinates
float baseX = 100;
float baseY = 30;
//Create two brush objects
PdfSolidBrush brush1 = new PdfSolidBrush(new PdfRGBColor(Color.Blue));
PdfSolidBrush brush2 = new PdfSolidBrush(new PdfRGBColor(Color.Black));
//Create a font
PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Regular);
//Add a textbox
page.Canvas.DrawString("TextBox:", font, brush1, new PointF(10, baseY));
RectangleF tbxBounds = new RectangleF(baseX, baseY, 150, 15);
PdfTextBoxField textBox = new PdfTextBoxField(page, "textbox");
textBox.Bounds = tbxBounds;
textBox.Text = "Hello World";
textBox.Font = font;
doc.Form.Fields.Add(textBox);
baseY += 25;
//Save to file

MemoryStream stream = new MemoryStream();
doc.SaveToStream(stream);
// pdfTemplate = stream.ToArray();
doc.SaveToFile("FillableForms.pdf", FileFormat.PDF);

//Fill Form
PdfDocument doc1 = new PdfDocument();
//doc1.LoadFromFile(@"FillableForms.pdf");
doc1.LoadFromBytes(stream.ToArray());
PdfFormWidget form = (PdfFormWidget)doc1.Form;
PdfFormFieldWidgetCollection formWidgetCollection = form.FieldsWidget;
for (int i = 0; i < formWidgetCollection.Count; i++)
{
PdfField field = formWidgetCollection[i];
if (field is PdfTextBoxFieldWidget)
{
if (field.Name == "textbox")
{
PdfTextBoxFieldWidget textBoxField = (PdfTextBoxFieldWidget)field;
textBoxField.Text = "Kaila Smith";
}
}
}
PdfForm form1 = (PdfForm)doc1.Form;
PdfFormFieldCollection formCollection = form1.Fields;
for (int i = 0; i < formCollection.Count; i++)
{
PdfField field = formCollection[i];
if (field is PdfTextBoxField)
{
if (field.Name == "textbox")
{
PdfTextBoxField textBoxField = (PdfTextBoxField)field;
textBoxField.Text = "Kaila Smith";
}
}
}
if (doc1.Form is PdfForm formWidget)
{
foreach (PdfField field in formWidget.Fields.List)
{
if (field is PdfTextBoxField)
{
var textBoxField = field as PdfTextBoxField;
textBoxField.Text = "tulesh";
}
}
}

tuleshkubde
 
Posts: 8
Joined: Tue Sep 06, 2022 5:35 pm

Wed Sep 07, 2022 9:17 am

Hello,

Thanks for your inquiry.
I created a console project with .Net Framework4.0 and simulated a PDF file to test your scenario, after executing your code and saving the result document, and the content of text box is “Kaila Smith” in the result document when I test your two scenarios, as shown in the screenshot below. To help us reproduce your issue and work out a solution for you, please offer the following message, thanks for your assistance in advance.

1) Your Pdf file.
2) Your Application type, such as Console App, .NET Framework 4.8.
3) Your test environment, such as OS info (E.g. Windows 7, 64-bit) and region setting (E.g. China, Chinese).

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 860
Joined: Tue Mar 08, 2022 2:02 am

Thu Sep 08, 2022 6:10 am

1) Your Pdf file. - You can use any pdf with name "Blank.pdf"
2) Your Application type, such as Console App, .NET Framework 4.8. - Console Application(.Net Core 3.1)
3) Your test environment, such as OS info (E.g. Windows 7, 64-bit) and region setting (E.g. China, Chinese). - Windows 10(64bit) and Region setting(United Kingdom)
4) Below is exact Code


static void Main(string[] args)
{
//Create a PdfDocument object from existing pdf
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("Blank.pdf");
doc.AllowCreateForm = (doc.Form == null) ? true : false;

//Add a page
PdfPageBase page = doc.Pages[0];
SizeF size = page.Size;

//Initialize x and y coordinates
float baseX = 100;
float baseY = 30;

//Create two brush objects
PdfSolidBrush brush1 = new PdfSolidBrush(new PdfRGBColor(Color.Blue));
PdfSolidBrush brush2 = new PdfSolidBrush(new PdfRGBColor(Color.Black));

//Create a font
PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Regular);

//Add a textbox
page.Canvas.DrawString("TextBox:", font, brush1, new PointF(10, baseY));
RectangleF tbxBounds = new RectangleF(baseX, baseY, 150, 15);
PdfTextBoxField textBox = new PdfTextBoxField(page, "textbox");
textBox.Bounds = tbxBounds;
textBox.Text = "Hello World";
textBox.Font = font;
doc.Form.Fields.Add(textBox);
baseY += 25;

//Save to file

MemoryStream stream = new MemoryStream();
doc.SaveToStream(stream);
// pdfTemplate = stream.ToArray();
doc.SaveToFile("FillableForms.pdf", FileFormat.PDF);

//Code to Fill Form created using existing existing pdf
PdfDocument doc1 = new PdfDocument();
//doc1.LoadFromFile(@"FillableForms.pdf");
doc1.LoadFromBytes(stream.ToArray());
doc1.AllowCreateForm = true;
PdfFormWidget form = (PdfFormWidget)doc1.Form;
PdfFormFieldWidgetCollection formWidgetCollection = form.FieldsWidget;
for (int i = 0; i < formWidgetCollection.Count; i++)
{
PdfField field = formWidgetCollection[i];
if (field is PdfTextBoxFieldWidget)
{
if (field.Name == "textbox")
{
PdfTextBoxFieldWidget textBoxField = (PdfTextBoxFieldWidget)field;
textBoxField.Text = "Kaila Smith";
}
}
}

//Save to file
doc1.SaveToFile("FillFormFields.pdf", FileFormat.PDF);

}

tuleshkubde
 
Posts: 8
Joined: Tue Sep 06, 2022 5:35 pm

Thu Sep 08, 2022 10:28 am

Hello,

Thanks for your more sharing.
I created Console project Console Application (.Net Core 3.1) with your code to test your scenario, I also didn’t reproduce your issue, I put my test project to our google cloud for your reference, you can download from the following link. If it doesn’t help you, please offer simple project that can reproduce your issue.

https://drive.google.com/file/d/1Ni0bZ3 ... sp=sharing

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 860
Joined: Tue Mar 08, 2022 2:02 am

Thu Sep 08, 2022 11:20 am

To Reproduce issue,
Can you please try with "Blank.pdf" that i have attached?

tuleshkubde
 
Posts: 8
Joined: Tue Sep 06, 2022 5:35 pm

Fri Sep 09, 2022 7:43 am

Hello,

Thanks for your more sharing.
I did reproduce your issue with the FreeSpire.pdf8.8.6, but I didn’t reproduce this issue with the latest commercial version of Spire.Pdf 8.8.6. Due to we unregularly maintain the free version of Spire.Pdf, I recommend you to use the latest commercial version of Spire.Pdf. To help you remove red warning message and functions’ limitations, we are willing to provide a temporary license (one month free). If you need it, you can contact our sales team (sales@e-iceblue.com).

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 860
Joined: Tue Mar 08, 2022 2:02 am

Fri Sep 09, 2022 12:29 pm

Can you please let me know when this feature will be available in FreeSpire.pdf version?

tuleshkubde
 
Posts: 8
Joined: Tue Sep 06, 2022 5:35 pm

Tue Sep 13, 2022 10:29 am

Hello,

Thanks for your inquiry.
Our development team will update the FreeSpire.pdf when they have the time and energy to spare. Therefore, the FreeSpire.pdf will be maintained unregularly and I can’t give you an accurate fixing time for this issue. I recommend you use our the latest commercial version of Spire.Pdf 8.8.6, for the commercial version of Spire.Pdf, our development team will maintain regularly it.

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 860
Joined: Tue Mar 08, 2022 2:02 am

Return to Spire.PDF