We know that the dynamic stamp, because the time in the stamp, the date can be generated dynamically, and thus has a strong timeliness. In this article, you will find a way to draw a dynamic stamp in a PDF by using C # programming, which automatically obtains the current system login user name, date and time information, and generates a stamp.
Using tools
Note: After downloading the installation, note Add Reference Spire.PDF.dll (DLL file can be obtained in the Bin folder under installation path) in the program
C # code sample (for reference)
Step 1: Add a using directive
using spire.pdf; using Spire.Pdf.Annotations; using Spire.Pdf.Annotations.Appearance; using Spire.Pdf.Graphics; using System; using System.Drawing;
Step 2: Create the document, load the test file
// Create a Pdfdocument object New pdfdocument (); // load an existing PDF document Doc. LoadFromFile ("sample.pdf");
Step 3: Get the page where you want to add a dynamic stamp
Pdfpagebase page = doc. pages[1];
Step 4: Create stamp templates, fonts, brushes, etc.
//Creating template ObjectsPdftemplate template =NewPdftemplate ( -, -);//Creating FontsPdfcjkstandardfont font1 =NewPdfcjkstandardfont (Pdfcjkfontfamily.sinotypesonglight, 16f, Pdffontstyle.bold |pdffontstyle.italic); Pdftruetypefont Font2=NewPdftruetypefont (NewFont ("Song Body", 10f),true);//create a monochrome brush and a gradient brushPdfsolidbrush brush =NewPdfsolidbrush (color.red); RectangleF rect=NewRectangleF (NewPointF (0,0), template. Size); Pdflineargradientbrush Gradientbrush=NewPdflineargradientbrush (Rect, Color.White, Color.White, pdflineargradientmode.horizontal);//Create rounded Rectangle pathintCornerradius =Ten; Pdfpath Path=NewPdfpath ();p Ath. AddArc (template. GetBounds (). X, template. GetBounds (). Y, Cornerradius, Cornerradius, the, -);p Ath. AddArc (template. GetBounds (). X+ template. Width-cornerradius, template. GetBounds (). Y, Cornerradius, Cornerradius, the, -);p Ath. AddArc (template. GetBounds (). X+ template. Width-cornerradius, template. GetBounds (). Y + template. Height-cornerradius, Cornerradius, Cornerradius,0, -);p Ath. AddArc (template. GetBounds (). X, template. GetBounds (). Y+ template. Height-cornerradius, Cornerradius, Cornerradius, -, -);p Ath. AddLine (template. GetBounds (). X, template. GetBounds (). Y+ template. Height-cornerradius, template. GetBounds (). X, template. GetBounds (). Y + Cornerradius/2);
Step 5: Apply the template
// draws a rounded rectangular path on a template and fills it with a gradient color template. Graphics.drawpath (Gradientbrush, path); // draw a rounded rectangular path on the template and fill the path with red . Graphics.drawpath (pdfpens.red, path);
Step 6: Draw the text on the seal, user name, current date time, etc.
" reviewed \ n " " administrative office \ n" + DateTime.Now.ToString ("F" New PointF (55new PointF (2));
Step 7: Add a stamp to the PDF page to specify the location
//Create a Pdfrubberstampannotation object and specify its location and sizePdfrubberstampannotation stamp =NewPdfrubberstampannotation (NewRectangleF (NewPointF (page. Actualsize.width- -,380), template. Size));//Create a Pdfapperance object and apply the template as a general statePdfappearance apprearance =Newpdfappearance (stamp); apprearance. Normal=template;//Apply a Pdfapperance object (that is, a style) on a stampStamp. Appearance =apprearance;//add a stamp to the Pdfannotation collectionPage. Annotationswidget.add (stamp);
Step 8: Save and open the document
Doc. SaveToFile ("output.pdf", fileformat.pdf); System.Diagnostics.Process.Start ("output.pdf");
After you complete the steps above, debug the Run program and generate the document. In the resulting document, a dynamic stamp has been added at the end of the text, as shown in:
All code:
usingspire.pdf;usingSpire.Pdf.Annotations;usingSpire.Pdf.Annotations.Appearance;usingSpire.Pdf.Graphics;usingSystem;usingSystem.Drawing;namespacePDF Dynamic Stamp {classProgram {Static voidMain (string[] args) { //Create a Pdfdocument objectPdfdocument doc =Newpdfdocument (); //loading an existing PDF documentDoc. LoadFromFile ("sample.pdf"); //get a page to add a dynamic stampPdfpagebase page = doc. pages[1]; //Creating template ObjectsPdftemplate template =NewPdftemplate ( -, -); //Creating FontsPdfcjkstandardfont font1 =NewPdfcjkstandardfont (Pdfcjkfontfamily.sinotypesonglight, 16f, Pdffontstyle.bold |pdffontstyle.italic); Pdftruetypefont Font2=NewPdftruetypefont (NewFont ("Song Body", 10f),true); //create a monochrome brush and a gradient brushPdfsolidbrush brush =NewPdfsolidbrush (color.red); RectangleF rect=NewRectangleF (NewPointF (0,0), template. Size); Pdflineargradientbrush Gradientbrush=NewPdflineargradientbrush (Rect, Color.White, Color.White, pdflineargradientmode.horizontal); //Create rounded Rectangle path intCornerradius =Ten; Pdfpath Path=NewPdfpath (); Path. AddArc (template. GetBounds (). X, template. GetBounds (). Y, Cornerradius, Cornerradius, the, -); Path. AddArc (template. GetBounds (). X+ template. Width-cornerradius, template. GetBounds (). Y, Cornerradius, Cornerradius, the, -); Path. AddArc (template. GetBounds (). X+ template. Width-cornerradius, template. GetBounds (). Y + template. Height-cornerradius, Cornerradius, Cornerradius,0, -); Path. AddArc (template. GetBounds (). X, template. GetBounds (). Y+ template. Height-cornerradius, Cornerradius, Cornerradius, -, -); Path. AddLine (template. GetBounds (). X, template. GetBounds (). Y+ template. Height-cornerradius, template. GetBounds (). X, template. GetBounds (). Y + Cornerradius/2); //draws a rounded rectangular path on a template and fills it with a gradient colortemplate. Graphics.drawpath (Gradientbrush, path); //draw a rounded rectangular path on the template and fill the path with redtemplate. Graphics.drawpath (pdfpens.red, path); //Draw stamp text, System user name, date on the templateString S1 ="reviewed \ n"; String S2= System.Environment.UserName +"Administrative office \ n"+ DateTime.Now.ToString ("F"); Template. Graphics.DrawString (S1, font1, Brush,NewPointF (5,5)); Template. Graphics.DrawString (S2, Font2, Brush,NewPointF (2, -)); //Create a Pdfrubberstampannotation object and specify its location and sizePdfrubberstampannotation stamp =NewPdfrubberstampannotation (NewRectangleF (NewPointF (page. Actualsize.width- -,380), template. Size)); //Create a Pdfapperance object and apply the template as a general statePdfappearance apprearance =Newpdfappearance (stamp); Apprearance. Normal=template; //Apply a Pdfapperance object (that is, a style) on a stampStamp. Appearance =apprearance; //add a stamp to the Pdfannotation collectionpage. Annotationswidget.add (stamp); //Save DocumentDoc. SaveToFile ("output.pdf", fileformat.pdf); System.Diagnostics.Process.Start ("output.pdf"); } }}
View Code
This is a description of how C # paints a dynamic stamp in a PDF document, and in the previous article it describes how to add a stamp to a PDF document, as well as a reference to the document.
Thanks for reading.
(End of this article)