Here is the code in question starting at ProcessPdfFields and attached is a pdf we are trying to add fields too. Let me know if you need anything else.
public void ProcessPdfFields(string pdfPath)
{
Aspose.Pdf.Kit.License lic = new Aspose.Pdf.Kit.License();
lic.SetLicense(Licensing.AsposePDFKitLicense);
string copyFieldsFromPdf = pdfPath;
string outputPdf = Path.Combine(Path.GetDirectoryName(pdfPath), string.Format("{0}.pdf", Path.GetFileNameWithoutExtension(Path.GetRandomFileName())));
Kit.PdfFileInfo orriginalPdfFileInfo = new Kit.PdfFileInfo(copyFieldsFromPdf);
int orriginalNumberOfPages = orriginalPdfFileInfo.NumberofPages;
Kit.PdfFileInfo emptyPdfFileInfo = new Kit.PdfFileInfo(copyFieldsFromPdf);
int emptyNumberOfPages = emptyPdfFileInfo.NumberofPages;
Kit.PdfFileEditor pdfFileEditor = new Kit.PdfFileEditor();
MemoryStream[] orriginalPdfPageStreams = pdfFileEditor.SplitToPages(copyFieldsFromPdf);
Kit.FormEditor formEditor = new Kit.FormEditor(copyFieldsFromPdf, outputPdf);
List<PdfFieldInfo> positions = new List<PdfFieldInfo>();
List<string> requiredFields = new List<string>();
float standardHeight = 15;
List<string> controlsAdded = new List<string>();
for (int i = 0; i < orriginalNumberOfPages && i < emptyNumberOfPages; i++)
{
Aspose.Pdf.Kit.Form pdfForm = new Aspose.Pdf.Kit.Form(orriginalPdfPageStreams[i]);
foreach (string fieldName in pdfForm.FieldsNames)
{
string fullName = pdfForm.GetFullFieldName(fieldName);
Kit.FormFieldFacade facade = pdfForm.GetFieldFacade(fullName);
float x = facade.Box.Left;
float y = facade.Box.Top;
int pageNumber = i + 1;
string newFieldName = string.Empty;
formEditor.Items = null;
if (fieldName.StartsWith(DataMerge.RadioBtnPrefix))
{
//formEditor.RadioGap = 40;
newFieldName = fieldName.Contains(".") ? string.Format("q{0}", fieldName.Split('.')[0]) : fieldName; // string.Format("q{0}", fieldName);
//string[] items = new string[] { bool.TrueString, bool.FalseString };
//formEditor.Items = items;
formEditor.RemoveField(fullName);
//formEditor.AddField(Aspose.Pdf.Kit.FieldType.Radio, newFieldName, pageNumber, x + 20, y - 3, x + 32 , y + 9);
positions.Add(new PdfFieldInfo(x + 20, y - 3, pageNumber, PdfFieldInfo.FieldTypes.YesNoRadio, newFieldName));
requiredFields.Add(newFieldName);
}
else if (fieldName.StartsWith(DataMerge.SignatureFieldPrefix))
{
newFieldName = pdfForm.GetField(fullName);
newFieldName = newFieldName.Replace("{", "").Replace("}", "");
formEditor.AddField(Kit.FieldType.Text, newFieldName, pageNumber, x, y, x + 100, y + 12);
formEditor.RemoveField(fullName);
newFieldName = string.Empty; // signatures can be entered with the same name multiple times
}
else if (fieldName.StartsWith(DataMerge.TextBoxPrefix))
{
string value = pdfForm.GetField(fullName);
PdfControlInfo info = new PdfControlInfo(fieldName);
newFieldName = info.Name;
if (info.Width == 0) info.Width = 50;
formEditor.AddField(Kit.FieldType.MultiLineText, newFieldName, pageNumber, x, y - info.Height, x + info.Width, y + standardHeight);
if (info.Required) requiredFields.Add(newFieldName);
formEditor.RemoveField(fullName);
}
else if (fieldName.StartsWith(DataMerge.NumericTextBoxPrefix))
{
string value = pdfForm.GetField(fullName);
PdfControlInfo info = new PdfControlInfo(fieldName);
newFieldName = info.Name;
if (info.Width == 0) info.Width = 50;
formEditor.AddField(Kit.FieldType.Text, newFieldName, "0", pageNumber, x, y - info.Height, x + info.Width, y + standardHeight);
if (info.Required) requiredFields.Add(newFieldName);
formEditor.RemoveField(fullName);
}
if (controlsAdded.Contains(newFieldName) && !string.IsNullOrEmpty(newFieldName)) throw new Exception(string.Format("Control '{0}' has already been added to the document", newFieldName));
if (!string.IsNullOrEmpty(newFieldName)) controlsAdded.Add(newFieldName);
}
}
formEditor.Save();
Kit.PdfFileMend mend = new Kit.PdfFileMend(outputPdf, outputPdf);
foreach (PdfFieldInfo position in positions)
{
if (position.FieldType == PdfFieldInfo.FieldTypes.YesNoRadio)
{
mend.AddText(new Aspose.Pdf.Kit.FormattedText("Yes"), position.PageNumber, position.LeftX - 26, position.LeftY);
mend.AddText(new Aspose.Pdf.Kit.FormattedText("No"), position.PageNumber, position.LeftX + 18, position.LeftY);
}
}
mend.Close();
File.Copy(outputPdf, copyFieldsFromPdf, true);
File.Delete(outputPdf);
AddRadioButtons(pdfPath, positions);
SetRequiredFields(pdfPath, requiredFields);
}
public void AddRadioButtons(string pdfPath, List<PdfFieldInfo> fields)
{
Aspose.Pdf.Kit.License lic = new Aspose.Pdf.Kit.License();
lic.SetLicense(Licensing.AsposePDFKitLicense);
if (fields == null) return;
if (fields.Count == 0) return;
string copyFieldsFromPdf = pdfPath;
string outputPdf = Path.Combine(Path.GetDirectoryName(pdfPath), string.Format("{0}.pdf", Path.GetFileNameWithoutExtension(Path.GetRandomFileName())));
Kit.PdfFileInfo orriginalPdfFileInfo = new Kit.PdfFileInfo(copyFieldsFromPdf);
int orriginalNumberOfPages = orriginalPdfFileInfo.NumberofPages;
Kit.PdfFileInfo emptyPdfFileInfo = new Kit.PdfFileInfo(copyFieldsFromPdf);
int emptyNumberOfPages = emptyPdfFileInfo.NumberofPages;
Kit.PdfFileEditor pdfFileEditor = new Kit.PdfFileEditor();
MemoryStream[] orriginalPdfPageStreams = pdfFileEditor.SplitToPages(copyFieldsFromPdf);
Kit.FormEditor formEditor = new Kit.FormEditor(copyFieldsFromPdf, outputPdf);
formEditor.RadioGap = 40;
foreach (PdfFieldInfo field in fields)
{
if (field.FieldType == PdfFieldInfo.FieldTypes.YesNoRadio)
{
formEditor.Items = new string[] { bool.TrueString, bool.FalseString };
formEditor.AddField(Aspose.Pdf.Kit.FieldType.Radio, field.Name, field.PageNumber, field.LeftX, field.LeftY, field.LeftX + 16, field.LeftY + 18);
}
}
formEditor.Save();
File.Copy(outputPdf, copyFieldsFromPdf, true);
File.Delete(outputPdf);
}
public void SetRequiredFields(string pdfPath, List<string> requiredFields)
{
Aspose.Pdf.Kit.License lic = new Aspose.Pdf.Kit.License();
lic.SetLicense(Licensing.AsposePDFKitLicense);
if (requiredFields == null) return;
if (requiredFields.Count == 0) return;
string copyFieldsFromPdf = pdfPath;
string outputPdf = Path.Combine(Path.GetDirectoryName(pdfPath), string.Format("{0}.pdf", Path.GetFileNameWithoutExtension(Path.GetRandomFileName())));
Kit.PdfFileInfo orriginalPdfFileInfo = new Kit.PdfFileInfo(copyFieldsFromPdf);
int orriginalNumberOfPages = orriginalPdfFileInfo.NumberofPages;
Kit.PdfFileInfo emptyPdfFileInfo = new Kit.PdfFileInfo(copyFieldsFromPdf);
int emptyNumberOfPages = emptyPdfFileInfo.NumberofPages;
Kit.PdfFileEditor pdfFileEditor = new Kit.PdfFileEditor();
MemoryStream[] orriginalPdfPageStreams = pdfFileEditor.SplitToPages(copyFieldsFromPdf);
Kit.FormEditor formEditor = new Kit.FormEditor(copyFieldsFromPdf, outputPdf);
for (int i = 0; i < orriginalNumberOfPages && i < emptyNumberOfPages; i++)
{
Aspose.Pdf.Kit.Form pdfForm = new Aspose.Pdf.Kit.Form(orriginalPdfPageStreams[i]);
foreach (string fieldName in pdfForm.FieldsNames)
{
string fullName = pdfForm.GetFullFieldName(fieldName);
if (requiredFields.Contains(fullName) || requiredFields.Contains(fieldName)) formEditor.SetFieldAttribute(fullName, Aspose.Pdf.Kit.PropertyFlag.Required);
}
}
formEditor.Save();
File.Copy(outputPdf, copyFieldsFromPdf, true);
File.Delete(outputPdf);
}