I am following some sample code to fill out an AcroForm and get "A PDF name must start with a slash (/)"
Here is my code that was taken from the sample code (the sample throws the same error too)
using System;
using PdfSharp.Pdf;
using PdfSharp.Pdf.AcroForms;
using PdfSharp.Pdf.IO;
Code:
namespace Sandbox.PDFSharp
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string filePath = @"H:\Projects\Sandbox\Sandbox\PDFSharp\template_5.pdf";
// Open the template
PdfDocument template = PdfReader.Open(filePath, PdfDocumentOpenMode.Modify);
// Get the root object of all interactive form fields
PdfAcroForm form = template.AcroForm;
// Get all form fields of the whole document
PdfAcroField.PdfAcroFieldCollection fields = form.Fields;
// Get all form fields of the whole document
string[] names = fields.Names;
names = fields.DescendantNames;
// Fill some value in each field
for (int i = 0; i < names.Length; i++)
{
string fqName = names[i];
PdfAcroField field = fields[fqName];
PdfTextField txtField;
if ((txtField = field as PdfTextField) != null)
txtField.Text = "Hello";
}
PdfDocument newDocument = new PdfDocument();
// Save the document...
const string filename = @"H:\Projects\Sandbox\Sandbox\PDFSharp\newDocument.pdf";
newDocument.Save(filename);
}
}
}
It fails on names = fields.DescendantNames; with "A PDF name must start with a slash (/)."
Any ideas on what is wrong?
Thanks!
Neil