How to stamp an existing PDF with custom design using C# and VB.NET?
Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can stamp an existing PDF with custom design using RubberStampAnnotation in C# and VB.NET. Rubber stamp annotation displays text or graphics intended to look like it is stamped on the page with a rubber stamp.
Steps to stamp an existing PDF with custom design programmatically:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Interactive; using Syncfusion.Pdf.Parsing; using System.Drawing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics Imports Syncfusion.Pdf.Interactive Imports Syncfusion.Pdf.Parsing Imports System.Drawing
- Use the following code snippet to stamp an existing PDF document with custom design.
C#
//Load the PDF document PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf"); //Get the page from PDF document PdfLoadedPage page = loadedDocument.Pages[0] as PdfLoadedPage; //Creates a new PDF rubber stamp annotation RectangleF rectangle = new RectangleF(350, 20, 200, 80); PdfRubberStampAnnotation rubberStampAnnotation = new PdfRubberStampAnnotation(rectangle); //Draw rounded rectangle PdfLinearGradientBrush brush = new PdfLinearGradientBrush(new PointF(0, 0), new PointF(0, 80), new PdfColor(Color.LightBlue), new PdfColor(Color.Gray)); PdfPath path = RoundedRect(new Rectangle(0, 0, 200, 80), 20); rubberStampAnnotation.Appearance.Normal.Graphics.DrawPath(brush, path); //Draw string PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold); rubberStampAnnotation.Appearance.Normal.Graphics.DrawString("REVIEWED", font, PdfBrushes.Black, new PointF(10, 10)); rubberStampAnnotation.Appearance.Normal.Graphics.DrawString("By John" + DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss zzz"), font, PdfBrushes.Black, new PointF(10, 30)); rubberStampAnnotation.Text = "Custom Stamp"; //Add rubber stamp annotation to the page page.Annotations.Add(rubberStampAnnotation); //Save the document loadedDocument.Save("CustomStamp.pdf"); //Close the document loadedDocument.Close(true); //This will open the PDF file so, the result will be seen in default PDF Viewer Process.Start("CustomStamp.pdf");
VB.NET
'Load the PDF document Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("Input.pdf") 'Get the page from PDF document Dim page As PdfLoadedPage = TryCast(loadedDocument.Pages(0), PdfLoadedPage) 'Creates a new PDF rubber stamp annotation Dim rectangle As RectangleF = New RectangleF(350, 20, 200, 80) Dim rubberStampAnnotation As PdfRubberStampAnnotation = New PdfRubberStampAnnotation(rectangle) 'Draw rounded rectangle Dim brush As PdfLinearGradientBrush = New PdfLinearGradientBrush(New PointF(0, 0), New PointF(0, 80), New PdfColor(Color.LightBlue), New PdfColor(Color.Gray)) Dim path As PdfPath = RoundedRect(New Rectangle(0, 0, 200, 80), 20) rubberStampAnnotation.Appearance.Normal.Graphics.DrawPath(brush, path) 'Draw string Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold) rubberStampAnnotation.Appearance.Normal.Graphics.DrawString("REVIEWED", font, PdfBrushes.Black, New PointF(10, 10)) rubberStampAnnotation.Appearance.Normal.Graphics.DrawString("By John" & DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss zzz"), font, PdfBrushes.Black, New PointF(10, 30)) rubberStampAnnotation.Text = "Custom Stamp" 'Add rubber stamp annotation to the page page.Annotations.Add(rubberStampAnnotation) 'Save the document loadedDocument.Save("CustomStamp.pdf") 'Close the document loadedDocument.Close(True) 'This will open the PDF file so, the result will be seen in default PDF Viewer Process.Start("CustomStamp.pdf")
- Use the following code in RoundedRect method to get the path of the rectangle.
C#
public PdfPath RoundedRect(Rectangle bounds, int radius) { int diameter = radius * 2; Size size = new Size(diameter, diameter); Rectangle arc = new Rectangle(bounds.Location, size); PdfPath path = new PdfPath(); if (radius == 0) { path.AddRectangle(bounds); return path; } //Top left arc path.AddArc(arc, 180, 90); //Top right arc arc.X = bounds.Right - diameter; path.AddArc(arc, 270, 90); //Bottom right arc arc.Y = bounds.Bottom - diameter; path.AddArc(arc, 0, 90); //Bottom left arc arc.X = bounds.Left; path.AddArc(arc, 90, 90); path.CloseFigure(); return path; }
VB.NET
Public Function RoundedRect(ByVal bounds As Rectangle, ByVal radius As Integer) As PdfPath Dim diameter As Integer = radius * 2 Dim size As Size = New Size(diameter, diameter) Dim arc As Rectangle = New Rectangle(bounds.Location, size) Dim path As PdfPath = New PdfPath() If radius = 0 Then path.AddRectangle(bounds) Return path End If 'Top left arc path.AddArc(arc, 180, 90) 'Top Right arc arc.X = bounds.Right - diameter path.AddArc(arc, 270, 90 'Bottom right arc arc.Y = bounds.Bottom - diameter path.AddArc(arc, 0, 90) //Bottom left arc arc.X = bounds.Left path.AddArc(arc, 90, 90) path.CloseFigure() Return path End Function
A complete working sample can be downloaded from RubberStampAnnotationSample.zip.
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation, where you can find other options like adding annotation to PDF, flatten annotation, modifying annotation, and removing annotation with code examples.
Refer here to learn more about PDF annotation.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.
Conclusion
I hope you enjoyed learning about how to stamp an existing PDF with custom design using C# and VB.NET.
You can refer to our WinForms PDF feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms PDF documentation to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!