Hi Friends. I am trying to write text to my loaded Pdf fields. I am assuming I will use this code just not sure where I need to place it...
if (fieldCollection.TryGetField("Name", out loadedField))
{
(loadedField as PdfLoadedTextBoxField).Text = "Jim Simpson";
}
I got a little confused with an example I was trying to use because it opens streams and closes streams.
Below is the code I use to load an existing pdf form and add fields to it.
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace PDFFormSample
{
class Program
{
static void Main(string[] args)
{
//Load the existing PDF document.
string sourceFilePath = "../Data/W3C.pdf";
byte[] pdfBytes = File.ReadAllBytes(sourceFilePath);
PdfLoadedDocument existingDocument = new PdfLoadedDocument(pdfBytes);
//Create the form if the form does not exist in the loaded document
if (existingDocument.Form == null)
existingDocument.CreateForm();
//Load the Page
PdfPage newPage = (PdfPage)existingDocument.Pages.Add();
// Create a graphics object to draw on the new page
PdfGraphics graphics = newPage.Graphics;
// Add content to the new page (e.g., text, images, etc.)
//Set the standard font
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 16);
//Draw the string
newPage.Graphics.DrawString("Job Application", font, PdfBrushes.Black, new PointF(250, 30));
font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
newPage.Graphics.DrawString("Name", font, PdfBrushes.Black, new PointF(30, 50));
//Create a text box field for name
PdfTextBoxField textBoxField1 = new PdfTextBoxField(newPage, "Name");
textBoxField1.Bounds = new System.Drawing.RectangleF(30, 70, 200, 20);
textBoxField1.ToolTip = "Name";
existingDocument.Form.Fields.Add(textBoxField1);
newPage.Graphics.DrawString("Email address", font, PdfBrushes.Black, new PointF(30, 110));
//Create a text box field for email address
PdfTextBoxField textBoxField2 = new PdfTextBoxField(newPage, "Email address");
textBoxField2.Bounds = new RectangleF(30, 130, 200, 20);
textBoxField2.ToolTip = "Email address";
existingDocument.Form.Fields.Add(textBoxField2);
newPage.Graphics.DrawString("Gender", font, PdfBrushes.Black, new PointF(30, 180));
//Create radio button for gender
PdfRadioButtonListField employeesRadioList = new PdfRadioButtonListField(newPage, "Gender");
existingDocument.Form.Fields.Add(employeesRadioList);
newPage.Graphics.DrawString("Male", font, PdfBrushes.Black, new PointF(60, 200));
PdfRadioButtonListItem radioButtonItem1 = new PdfRadioButtonListItem("Male");
radioButtonItem1.Bounds = new RectangleF(30, 200, 20, 20);
newPage.Graphics.DrawString("Female", font, PdfBrushes.Black, new PointF(160, 200));
PdfRadioButtonListItem radioButtonItem2 = new PdfRadioButtonListItem("Female");
radioButtonItem2.Bounds = new RectangleF(130, 200, 20, 20);
employeesRadioList.Items.Add(radioButtonItem1);
employeesRadioList.Items.Add(radioButtonItem2);
newPage.Graphics.DrawString("Which position you are applying for?", font, PdfBrushes.Black, new PointF(30, 240));
//Create combo box for position
PdfComboBoxField comboBox = new PdfComboBoxField(newPage, "JobTitle");
comboBox.Bounds = new RectangleF(30, 260, 100, 20);
comboBox.BorderColor = new PdfColor(Color.Gray);
comboBox.ToolTip = "Job Title";
comboBox.Items.Add(new PdfListFieldItem("Development", "Development"));
comboBox.Items.Add(new PdfListFieldItem("Support", "Support"));
comboBox.Items.Add(new PdfListFieldItem("Documentation", "Documentation"));
existingDocument.Form.Fields.Add(comboBox);
string sourceFilePath1 = "../Form.pdf";
//Save the document
using (FileStream fileStream = new FileStream(sourceFilePath1, FileMode.Open, FileAccess.Write))
{
existingDocument.Save(fileStream);
}
//close the document
existingDocument.Close(true);
//This will open the PDF file so, the result will be seen in default PDF Viewer
Process.Start("C:\\Samples\\This works write form fields to existing pdf file\\PDFFormSample\\PDFFormSample\\bin\\Form.pdf");
}
}
}
Hi Ron,
Kindly use the following UG documentation to know more about creating, filling and flattening forms (AcroForms and XFA).
To create new form field on PDF page: https://help.syncfusion.com/file-formats/pdf/working-with-forms#creating-a-new-pdf-form
To modify existing form fields in PDF page: https://help.syncfusion.com/file-formats/pdf/working-with-forms#filling-form-fields-in-an-existing-pdf-document
Regards,
Karmegam