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.

Wed Apr 10, 2019 4:51 pm

Hello,

I'm developing a Windows App in C# to stamp PDF files with a text note and it also have to have a field that every time somebody opens the PDF should show today's date, could you guys please help to implement it with SpirePDF?

Thank You
Thank You

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

Thu Apr 11, 2019 10:00 am

Hi,

Thanks for your inquiry.
You could add a textbox field and add calculation script to achieve the effect. Sample code is shown as follows. If there is any question, please feel free to write back.
Code: Select all
String script = "var time = Date.now();var d = new Date(time);this.getField(\"TextBox\").value=d;";
PdfJavaScriptAction action = new PdfJavaScriptAction(script);
//Load your Pdf file from disk
string input = "Sample.pdf";
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile(input);
pdf.AllowCreateForm = (pdf.Form == null) ? true : false;
//Get the first page
PdfPageBase page = pdf.Pages[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(page, "TextBox");
textbox.Bounds = new RectangleF(x, y, 100, 20);
textbox.BorderWidth = 0.75f;
textbox.BorderStyle = PdfBorderStyle.Solid;
//Add calculation
textbox.Actions.Calculate = action;
pdf.Form.Fields.Add(textbox);

//Execute the action once opening the PDF
pdf.AfterOpenAction = action;
pdf.SaveToFile("result.pdf", Spire.Pdf.FileFormat.PDF);



Sincerely,
Nancy
E-iceblue support team
Last edited by nancy.yang on Mon Apr 15, 2019 2:01 am, edited 1 time in total.
User avatar

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

Fri Apr 12, 2019 9:06 pm

Hi Nancy,

I got this code working, Thank You very much.

Now I'm facing with another error when I'm trying to stamp files with more then one page, the API stamps the first page correctly but in the other pages is showing a textbox with error(see attachment).

Thank You
Thank You

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

Fri Apr 12, 2019 9:26 pm

Just Upload my code.
Thank You

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

Mon Apr 15, 2019 9:32 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;";
                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
Last edited by nancy.yang on Tue Apr 16, 2019 2:13 am, edited 1 time in total.
User avatar

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

Mon Apr 15, 2019 4:00 pm

It works, Thank you very much.
Thank You

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

Tue Apr 16, 2019 9:01 am

Hi,

Thanks for your feedback. If you need further assistance, please feel free to contact us. Wish you all the best!

Sincerely,
Nancy
E-iceblue support team
User avatar

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

Return to Spire.PDF