XDoc.PDF
Features
Tech Specs
How-to C#
Pricing
C# PDF: How to PDF Create PDF Export File and Page Process PDF Read PDF Write Form Process Document Protect Annotation & Drawing PDF Print Work with Other SDKs Barcode Read Barcode Create OCR Twain

.NET PDF Viewer Component
How to open, display, add, delete PDF file stamp annotation in C#.net


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.










  • 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 sticky note to any position on PDF page.



Since RasterEdge XDoc.PDF SDK is based on .NET framework 2.0, users are enabled to use it in any type of a 32-bit or 64-bit .NET application, including ASP.NET web service and Windows Forms for any .NET Framework version from 2.0 to 4.5.







C#.NET Stamp: Create Template for Stamp Annotation in C#.NET




With XDoc.PDF library, it is necessary to add PDFStampTemplate through PDFStampTemplateMgr before adding Stamp Annotation to pdf document. Every template has a unique template ID. When adding Stamp Annotation, it will create Stamp Annotation object with ID and responding User Info.


To create a stamp template, the following things are necessary:


      Background image, which will be provided as vector image


      Dynamic Fields, which is responding to User Info properties



What’s more, it has two ways to add background image for Stamp Template:


      Add by PDFContext


      Add from existing PDF file



The properties of Dynamic Field are as follows:


      The area of Stamp Template , RectangleF


      Field Type, which is responding to PDFAnnotStampUserInfo property


      Font, Size, Color





C# prepare a stamp source file (PDF file)


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


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