|
C# PDF Stamp Library
How to add stamp to PDF file in C#.net ASP.NET, Windows application
C#.NET Tutorial for How to Add a Stamp Annotation to PDF Page with Visual C# Language in .NET Project.
ASP.NET Annotate PDF function is based on C# PDF Annotate SDK.
In this C# tutorial, you learn how to stamp a PDF document using C#.
- Create new stamp templates and register to PDFStampTemplateMgr
- Add a stamp with dynamic data: Name, Company, Date
- Extract the stamp from an existing PDF file
How to add, edit Stamp on existing PDF file using C#
- A best application for annotation PDF document in Visual C#.NET application and ASP.NET WebForm project
- Support .NET WinForms, ASP.NET MVC in IIS, ASP.NET Ajax, Azure cloud service, DNN (DotNetNuke), SharePoint
- Evaluation library and components enable users to annotate PDF without adobe PDF reader control installed
- Able to add notes to PDF using C# source code in Visual Studio .NET framework
- Allow users to add comments online in ASPX webpage
- Able to change font size in PDF comment box
- Able to save and print stamp notes in PDF file
Stamp Annotation is a necessary feature in PDF annotation, which bring users quick and efficient working with PDF Document. RasterEdge XDoc.PDF SDK is a multifunctional PDF document processing tool, which can perform various PDF annotation works in easy ways. Using this .NET PDF annotation control, C# developers can add a stamp to any position on PDF page.
You apply a stamp to a PDF in the C#, in much the same way you apply a rubber stamp to a paper document.
You can choose from a list of predefined stamps, or you can create your own stamps.
Here are the basic workflow to create and add stamp annotations to new or existing PDF files in C#.
- Create new Stamp templates
- Register new stamp templates to PDFStampTemplateMgr
- Add new stamp annotations from templates to PDF pages
C# prepare a stamp source file (PDF file)
The following C# code explains how to prepare, create stamp templates and save to PDF files in C#. And you apply these stamps on other PDF documents.
String outputFilePath = Program.RootPath + "\\" + "StampTemplateBg1.pdf";
// initial a region (1 inch in both width and height) in the context for designing a template background
// Region Boundary: [0, 0, 96, 96] (in 96 dpi)
PDFContext ctx = new PDFContext();
ctx.SetWidth(new RELength(1, Units.IN));
ctx.SetHeight(new RELength(1, Units.IN));
// draw the outer circle
ctx.DrawEllipse(new REPen(Color.Red, 6F), 3, 3, 90, 90);
// draw the inner circle
ctx.DrawEllipse(new REPen(Color.Red, 1F), 8, 8, 80, 80);
// draw the seperater line
ctx.DrawLine(new REPen(Color.Red, 1F), 8, 48, 88, 48);
// output to a file
ctx.SaveToFile(outputFilePath);
String outputFilePath = Program.RootPath + "\\" + "StampTemplateBg2.pdf";
// initial a region in the context for designing a template background
// Region Boundary: [0, 0, 192, 96] (in 96 dpi)
PDFContext ctx = new PDFContext();
ctx.SetWidth(new RELength(2, Units.IN));
ctx.SetHeight(new RELength(1, Units.IN));
// draw an outer rectangle
ctx.DrawRectangle(new REPen(Color.Red, 6F), 3, 3, 186, 90);
// draw an inner rectangle
ctx.DrawRectangle(new REPen(Color.Red, 1F), 8, 8, 176, 80);
// output to a file
ctx.SaveToFile(outputFilePath);
C# create a new stamp annotation template from an exist PDF file
String sourceFilePath = Program.RootPath + "\\" + "StampTemplateBg1.pdf";
int pageIndex = 0;
// create template by the specified page
PDFStampTemplate template = PDFStampTemplate.Create(sourceFilePath, pageIndex);
// to add optional dynamic fields for the template ...
C# create a new stamp annotation template by using PDF Context
PDFContext ctx = new PDFContext();
ctx.SetWidth(new RELength(1F, Units.IN));
ctx.SetHeight(new RELength(1F, Units.IN));
// draw shapes to context
ctx.DrawEllipse(new REPen(Color.Red, 6F), 3, 3, 90, 90);
ctx.DrawEllipse(new REPen(Color.Red, 1F), 8, 8, 80, 80);
ctx.DrawLine(new REPen(Color.Red, 1F), 8, 48, 88, 48);
// create template by context
PDFStampTemplate template = PDFStampTemplate.Create(ctx);
// to add optional dynamic fields for the template ...
C# create a stamp annotation template with dynamic fields
You can also add a dynamic stamp on PDF document in C#, which allows you to indicate name, date and time information on the stamp.
PDFContext ctx = new PDFContext();
ctx.SetWidth(new RELength(1F, Units.IN));
ctx.SetHeight(new RELength(1F, Units.IN));
// draw shapes to context
ctx.DrawEllipse(new REPen(Color.Red, 6F), 3, 3, 90, 90);
ctx.DrawEllipse(new REPen(Color.Red, 1F), 8, 8, 80, 80);
ctx.DrawLine(new REPen(Color.Red, 1F), 8, 48, 88, 48);
// create template by context
PDFStampTemplate template = PDFStampTemplate.Create(ctx);
// define a dynamic text field for User Info - Name
PDFStampTemplateField field0 = new PDFStampTemplateField();
// dynamic text field region in the context
field0.Boundary = new RectangleF(0, 18, 100, 20);
field0.FieldType = PDFStampTemplateFieldType.Name;
field0.TextColor = Color.FromArgb(255, 0, 0);
field0.TextFont = new Font("Times New Roman", 10, FontStyle.Regular);
// add the filed
template.AddField(field0);
// define a dynamic text field for User Info - Company
PDFStampTemplateField field1 = new PDFStampTemplateField();
field1.Boundary = new RectangleF(0, 62, 100, 20);
field1.FieldType = PDFStampTemplateFieldType.Company;
field1.TextColor = Color.FromArgb(255, 0, 0);
field1.TextFont = new Font("Times New Roman", 8, FontStyle.Regular);
template.AddField(field1);
// define a dynamic text field for User Info - Current Date Time
PDFStampTemplateField field2 = new PDFStampTemplateField();
field2.Boundary = new RectangleF(0, 40, 100, 20);
field2.FieldType = PDFStampTemplateFieldType.CurrertDateTime;
field2.TextColor = Color.FromArgb(255, 0, 0);
field2.TextFont = new Font("Times New Roman", 6, FontStyle.Regular);
template.AddField(field2);
C# add a new stamp annotation template
// …create a stamp annotation template …
PDFStampTemplate template = PDFStampTemplate.Create("", 0);
// template ID must be unique in the SDK
String templateID = "Template 1";
// add new template to the SDK
PDFStampTemplateMgr.AddTemplate(templateID, template, false);
if (PDFStampTemplateMgr.IsTemplateExist(templateID))
{
Console.WriteLine("Template Exist");
// get preview of a template with a given size
Bitmap preview = PDFStampTemplateMgr.GetTemplatePreview(templateID, new Size(300, 300));
}
else
{
Console.WriteLine("Template Dost Not Exist");
}
C# add a stamp annotation to page
String inputFilePath = Program.RootPath + "\\" + "1.pdf";
String outputFilePath = Program.RootPath + "\\" + "1_Annots.pdf";
// Create a stamp annotation
PDFAnnotStamp annot = new PDFAnnotStamp();
// select stamp template by the template ID
String templateID = @"Template 1";
annot.StampTemplateID = templateID;
// set annotation position and size
annot.Boundary = new RectangleF(300, 300, 50, 50);
// set user info
annot.UserInfo.Company = "XYZ Ltd.";
annot.UserInfo.FamilyName = "Kitty";
annot.UserInfo.GivenName = "Hello";
// other annotation properties
annot.Opacity = 1.0;
annot.PageIndex = 0;
// add annotation
PDFAnnotHandler.AddAnnotation(inputFilePath, annot, outputFilePath);
C# retrieve annotation popup note window's properties
String inputFilePath = Program.RootPath + "\\" + "Annot_1.pdf";
PDFDocument doc = new PDFDocument(inputFilePath);
List<IPDFAnnot> annots = PDFAnnotHandler.GetAllAnnotations(doc);
foreach (IPDFAnnot annot in annots)
{
if (annot is IPDFPopupAnnot)
{
Console.WriteLine("Annotation has popup window");
IPDFPopupAnnot obj = (IPDFPopupAnnot)annot;
Console.WriteLine("Is open in the begin: " + obj.Popup.IsOpen);
Console.WriteLine("Popup window boundary: " + obj.Popup.Boundary.ToString());
}
else
{
Console.WriteLine("Annotation has no popup window");
}
}
C# retrieve annotation flags
String inputFilePath = Program.RootPath + "\\" + "Annot_1.pdf";
PDFDocument doc = new PDFDocument(inputFilePath);
List<IPDFAnnot> annots = PDFAnnotHandler.GetAllAnnotations(doc);
foreach (IPDFAnnot annot in annots)
{
Console.WriteLine("Annotation Flags");
Console.WriteLine(" Invisible: " + annot.IsInvisible);
Console.WriteLine(" Hidden: " + annot.IsHidden);
Console.WriteLine(" Print: " + annot.IsPrint);
Console.WriteLine(" No Zoom: " + annot.NoZoom);
Console.WriteLine(" No Rotate: " + annot.NoRotate);
Console.WriteLine(" No View: " + annot.NoView);
Console.WriteLine(" Read Only: " + annot.IsReadOnly);
Console.WriteLine(" Locked: " + annot.IsLocked);
Console.WriteLine(" Toggle No View: " + annot.IsToggleNoView);
Console.WriteLine(" Locked Contents: " + annot.IsLockedContents);
}
C# set annotation flags
String inputFilePath = Program.RootPath + "\\" + "2.pdf";
String outputFilePath = Program.RootPath + "\\" + "Annot_1.pdf";
// create the annotation
PDFAnnotDeleteLine annot = new PDFAnnotDeleteLine();
annot.StartPoint = new PointF(100F, 200F);
annot.EndPoint = new PointF(300F, 400F);
// set annotation flags
annot.IsInvisible = false;
annot.IsPrint = true;
annot.NoRotate = true;
annot.NoZoom = true;
// add annotation
PDFAnnotHandler.AddAnnotation(inputFilePath, 1, annot, outputFilePath);
|