I'm developing a C# application to stamp PDf files with automate textbox (via Javascript) what shows a today's date, I'm able to stamp the first page with no problem but all other pages I got a textbox showing an error (See attachment).
Bellow is my code:
namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
string script = "var d = new Date();" +
"this.getField(\"Today\").value=d;";
PdfJavaScriptAction action = new PdfJavaScriptAction(script);
//Load your Pdf file from disk
string input = @"C:\Repository\Developments\Stamper\InBox\NotStamped.pdf";
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile(input);
//Create a new pdf
PdfDocument newPdf = new PdfDocument();
pdf.AllowCreateForm = (pdf.Form == null) ? true : false;
int pgNumber = pdf.Pages.Count;
for ( int i = 0; i < pgNumber; i++)
{
//PdfPageBase page = pdf.Pages[i];
var page = pdf.Pages[i];
PdfPageBase newPage;
if (page.Rotation == PdfPageRotateAngle.RotateAngle90)
newPage = newPdf.Pages.Add(new SizeF(page.Size.Height, page.Size.Width), new PdfMargins(0));
else if (page.Rotation == PdfPageRotateAngle.RotateAngle270)
newPage = newPdf.Pages.Add(new SizeF(page.Size.Height, page.Size.Width), new PdfMargins(0));
else
newPage = newPdf.Pages.Add(page.Size, new PdfMargins(0));
page.CreateTemplate().Draw(newPage, new PointF(0, 0));
//Create a textbox field
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 13f);
PdfBrush brush = PdfBrushes.Black;
float x = 100;
float y = 100;
PdfTextBoxField textbox = new PdfTextBoxField(newPage, "Today");
textbox.Bounds = new RectangleF(x, y, 100, 20);
textbox.BorderWidth = 0.75f;
textbox.BorderStyle = PdfBorderStyle.Solid;
//Add calculation
textbox.Actions.Calculate = action;
newPdf.Form.Fields.Add(textbox);
}
//Execute the action once opening the PDF
newPdf.AfterOpenAction = action;
newPdf.SaveToFile(@"C:\Repository\Developments\Stamper\Outbox\result.pdf", Spire.Pdf.FileFormat.PDF);
}
}
}
Thank You