Adding Annotations to existing PDF file
Add Free Text Annotation in an Existing PDF File (facades)
PdfContentEditor allows you to add annotations of different types in an existing PDF file. You can use the respective method to add a particular annotation. For example, in the following code snippet, we have used CreateFreeText method add FreeText type annotation.
Any type of annotations can be added to the PDF file in the same way. First of all, you need to create an object of type PdfContentEditor and bind input PDF file using BindPdf method. Secondly, you have to create a Rectangle object to specify the area of the annotatation.
After that, you can call CreateFreeText method to add FreeText annotation, and then use Save method to save the updated PDF file.
The following code snippet shows you how to add free text annotation in a PDF file.
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
public static void AddFreeTextAnnotation ()
{
// The path to the documents directory
string dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Open document
using ( var document = new Aspose . Pdf . Document ( dataDir + "input.pdf" ))
{
// Instantiate PdfContentEditor object
var editor = new Aspose . Pdf . Facades . PdfContentEditor ( document );
var tfa = new Aspose . Pdf . Text . TextFragmentAbsorber ( "PDF" );
tfa . Visit ( document . Pages [ 1 ]);
var rect = new System . Drawing . Rectangle
{
X = ( int ) tfa . TextFragments [ 1 ]. Rectangle . LLX ,
Y = ( int ) tfa . TextFragments [ 1 ]. Rectangle . URY + 5 ,
Height = 18 ,
Width = 100
};
// Add annotation
editor . CreateFreeText ( rect , "Free Text Demo" , 1 ); // last param is a page number
// Save document
editor . Save ( dataDir + "AddFreeTextAnnotation_out.pdf" );
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
public static void AddFreeTextAnnotation ()
{
// The path to the documents directory
string dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Open document
using var document = new Aspose . Pdf . Document ( dataDir + "input.pdf" );
// Instantiate PdfContentEditor object
var editor = new Aspose . Pdf . Facades . PdfContentEditor ( document );
var tfa = new Aspose . Pdf . Text . TextFragmentAbsorber ( "PDF" );
tfa . Visit ( document . Pages [ 1 ]);
var rect = new System . Drawing . Rectangle
{
X = ( int ) tfa . TextFragments [ 1 ]. Rectangle . LLX ,
Y = ( int ) tfa . TextFragments [ 1 ]. Rectangle . URY + 5 ,
Height = 18 ,
Width = 100
};
// Add annotation
editor . CreateFreeText ( rect , "Free Text Demo" , 1 ); // last param is a page number
// Save document
editor . Save ( dataDir + "AddFreeTextAnnotation_out.pdf" );
}
Add Text Annotation in an Existing PDF File (facades)
In this example also, you need to create an object of type PdfContentEditor and bind input PDF file using BindPdf method. Secondly, you have to create a Rectangle object to specify the area of the annotation. After that, you can call CreateFreeText method to add FreeText annotation, create title of your annotations, and the page number on which the annotation is located.
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
public static void AddTextAnnotation ()
{
// The path to the documents directory
string dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Open document
using ( var document = new Aspose . Pdf . Document ( dataDir + "input.pdf" ))
{
// Instantiate PdfContentEditor object
var editor = new Aspose . Pdf . Facades . PdfContentEditor ( document );
var tfa = new Aspose . Pdf . Text . TextFragmentAbsorber ( "PDF" );
tfa . Visit ( document . Pages [ 1 ]);
var rect = new System . Drawing . Rectangle
{
X = ( int ) tfa . TextFragments [ 1 ]. Rectangle . LLX ,
Y = ( int ) tfa . TextFragments [ 1 ]. Rectangle . URY + 5 ,
Height = 18 ,
Width = 100
};
// Add annotation
editor . CreateText ( rect , "Aspose User" , "PDF is a better format for modern documents" , false , "Key" , 1 );
// Save document
editor . Save ( dataDir + "AddTextAnnotation_out.pdf" );
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
public static void AddTextAnnotation ()
{
// The path to the documents directory
string dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Open document
using var document = new Aspose . Pdf . Document ( dataDir + "input.pdf" );
// Instantiate PdfContentEditor object
var editor = new Aspose . Pdf . Facades . PdfContentEditor ( document );
var tfa = new Aspose . Pdf . Text . TextFragmentAbsorber ( "PDF" );
tfa . Visit ( document . Pages [ 1 ]);
var rect = new System . Drawing . Rectangle
{
X = ( int ) tfa . TextFragments [ 1 ]. Rectangle . LLX ,
Y = ( int ) tfa . TextFragments [ 1 ]. Rectangle . URY + 5 ,
Height = 18 ,
Width = 100
};
// Add annotation
editor . CreateText ( rect , "Aspose User" , "PDF is a better format for modern documents" , false , "Key" , 1 );
// Save document
editor . Save ( dataDir + "AddTextAnnotation_out.pdf" );
}
Add Line Annotation in an Existing PDF File (facades)
We also specify the Rectangle, coordinates of the beginning and end of the line, page number, thickness, style and color of the annotation frame, type of line dash, type of start and end of the line.
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
public static void AddLineAnnotation ()
{
// The path to the documents directory
string dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Open document
using ( var document = new Aspose . Pdf . Document ( dataDir + "input.pdf" ))
{
// Instantiate PdfContentEditor object
var editor = new Aspose . Pdf . Facades . PdfContentEditor ( document );
// Create Line Annotation
editor . CreateLine (
new System . Drawing . Rectangle ( 550 , 93 , 562 , 439 ),
"Test" ,
556 , 99 , 556 , 443 , 1 , 2 ,
System . Drawing . Color . Red ,
"dash" ,
new int [] { 1 , 0 , 3 },
new [] { "Open" , "Open" });
// Save document
editor . Save ( dataDir + "AddLineAnnotation_out.pdf" );
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
public static void AddLineAnnotation ()
{
// The path to the documents directory
string dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Open document
using var document = new Aspose . Pdf . Document ( dataDir + "input.pdf" );
// Instantiate PdfContentEditor object
var editor = new Aspose . Pdf . Facades . PdfContentEditor ( document );
// Create Line Annotation
editor . CreateLine (
new System . Drawing . Rectangle ( 550 , 93 , 562 , 439 ),
"Test" ,
556 , 99 , 556 , 443 , 1 , 2 ,
System . Drawing . Color . Red ,
"dash" ,
new int [] { 1 , 0 , 3 },
new [] { "Open" , "Open" });
// Save document
editor . Save ( dataDir + "AddLineAnnotation_out.pdf" );
}