I'm trying to put signature form fields in a given position at specific pages using iText for .NET (v7.0.4). The code I'm working on is the following:
public static void test()
{
using (PdfDocument pdfDoc = new PdfDocument(new PdfWriter(@"c:\temp\pippo.pdf")))
{
//Add some blank pages
pdfDoc.AddNewPage();
pdfDoc.AddNewPage();
pdfDoc.AddNewPage();
//Instantiate a Signature Form Field using factory
PdfSignatureFormField sgnField =
PdfFormField.CreateSignature(pdfDoc, new Rectangle(100, 100, 200, 100));
//setting name and page
sgnField.SetFieldName("pluto");
sgnField.SetPage(1);
//Adding to AcroForm
PdfAcroForm.GetAcroForm(pdfDoc, true).AddField(sgnField);
}
}
The output document (pippo.pdf) has the signature field in first page and that's the expected behavior. The issue is that I can see the signature field even in the last page (the third page, in this case).
Moreover, if I remove the last page, by calling pdfDoc.RemovePage(3);
, the signature field disappear even from the first page.
The question is: how to make signature form fields not replicated in the last page? Any suggestion is really well accepted!
The method AddField(PdfFormField field)
is documented as
* This method adds the field to the last page in the document.
* If there's no pages, creates a new one.
Thus, you first assign your field to the first page using
sgnField.SetPage(1)
and then also to the last one using
PdfAcroForm.GetAcroForm(pdfDoc, true).AddField(sgnField);
You should use AddField(PdfFormField field, PdfPage page)
instead:
PdfAcroForm.GetAcroForm(pdfDoc, true).AddField(sgnField, pdfDoc.GetFirstPage());
@iText DEV: When going for PDF2, this should be prevented.