I have a PDF Form (with form fields as textbox, radiobutton, same textbox on different pages and so on.) Now I would like to exchange for example page no. 2 with page no.2 of a different pdf. But the current form fields existing on page 2 should stay as they are. The code below works fine for "simple" textfields. But I find no example in the web for radiobuttons and textfields with the same name on different pages...
Can anybody help?
using (PdfReader read = new(resultFilename))
using (PdfWriter write = new(fileTemp))
using (PdfDocument pdfDoc = new(read, write))
using (PdfReader replaceRead = new(replaceFilename))
using (PdfDocument replacePdfDoc = new(replaceRead))
{
Document document = new(pdfDoc);
int resultPages = pdfDoc.GetNumberOfPages();
int replacePages = replacePdfDoc.GetNumberOfPages();
replacePdfDoc.CopyPagesTo(1, resultPages, pdfDoc);
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);
form.SetGenerateAppearance(true);
var fields = form.GetFormFields();
foreach (var field in fields)
{
PdfFormField ff = field.Value;
if (ff == null)
continue;
IList<iText.Kernel.Pdf.Annot.PdfWidgetAnnotation> pdfWidgetAnnotations = ff.GetWidgets();
PdfPage page = pdfWidgetAnnotations[0].GetPage();
int number = pdfDoc.GetPageNumber(page);
if (number <= resultPages)
number += resultPages;
if (ff.GetParent() != null)
continue;
pdfDoc.GetPage(number).AddAnnotation(pdfWidgetAnnotations[0]);
}
for (int i = 1; i <= resultPages; i++)
{
pdfDoc.RemovePage(1);
}
}