Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Fri Apr 12, 2019 7:54 pm

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
Thank You

[email protected]
 
Posts: 57
Joined: Tue Mar 19, 2019 6:41 pm

Mon Apr 15, 2019 9:16 am

Hi,

Thanks for your inquiry and sorry for late reply as weekend.
In your code, the javascript string is only for the first textbox named "Today" in the first page. But the names of textbox fields in other pages are not the "Today", so you can see that the today's date didn't take effect in other pages . Please correct your code as follows to get rid of the issue. If there is any question, please feel free to write back.
Code: Select all
            //Load your Pdf file from disk
            string input = "Sample.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;
            PdfJavaScriptAction action = null;
            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"+i.ToString());
                textbox.Bounds = new RectangleF(x, y, 150, 20);
                textbox.BorderWidth = 0.75f;
                textbox.BorderStyle = PdfBorderStyle.Solid;
                //Create the Javascript for different textbox
                string script = "var d = new Date();var name='Today" + i.ToString() + "';this.getField(name).value=d;";
                PdfJavaScriptAction action = new PdfJavaScriptAction(script);
                //Add calculation
                textbox.Actions.Calculate = action;
                newPdf.Form.Fields.Add(textbox);
            }
            //Execute the action once opening the PDF
            newPdf.AfterOpenAction = action;
            newPdf.SaveToFile("result.pdf", Spire.Pdf.FileFormat.PDF);


Sincerely,
Nancy
E-iceblue support team
User avatar

nancy.yang
 
Posts: 184
Joined: Wed Apr 03, 2019 2:33 am

Return to Spire.PDF

cron