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
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);
//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);