Agregar Anotaciones a un archivo PDF existente
Agregar Anotación de Texto Libre en un Archivo PDF Existente
PdfContentEditor te permite agregar anotaciones de diferentes tipos en un archivo PDF existente. Puedes usar el método respectivo para agregar una anotación particular. Por ejemplo, en el siguiente fragmento de código, hemos utilizado el método CreateFreeText para agregar una anotación de tipo FreeText .
Cualquier tipo de anotaciones se puede agregar al archivo PDF de la misma manera. Primero, necesitas crear un objeto de tipo PdfContentEditor y vincular el archivo PDF de entrada usando el método BindPdf . En segundo lugar, debes crear un objeto Rectangle para especificar el área de la anotación.
Después de eso, puedes llamar al método CreateFreeText para agregar la anotación FreeText , y luego usar el método Save para guardar el archivo PDF actualizado.
El siguiente fragmento de código te muestra cómo agregar una anotación de texto libre en un archivo PDF.
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddFreeTextAnnotation ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Open PDF 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 PDF 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
private static void AddFreeTextAnnotation ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Open PDF 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 PDF document
editor . Save ( dataDir + "AddFreeTextAnnotation_out.pdf" );
}
Agregar Anotación de Texto en un Archivo PDF Existente
En este ejemplo también, necesitas crear un objeto de tipo PdfContentEditor y vincular el archivo PDF de entrada usando el método BindPdf . En segundo lugar, debes crear un objeto Rectangle para especificar el área de la anotación. Después de eso, puedes llamar al método CreateFreeText para agregar la anotación de texto libre, crear el título de tus anotaciones y el número de página en la que se encuentra la anotación.
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddTextAnnotation ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Open PDF 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 PDF 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
private static void AddTextAnnotation ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Open PDF 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 PDF document
editor . Save ( dataDir + "AddTextAnnotation_out.pdf" );
}
Agregar Anotación de Línea en un Archivo PDF Existente
También especificamos el Rectangle, las coordenadas del inicio y el final de la línea, el número de página, el grosor, el estilo y el color del marco de la anotación, el tipo de guion de la línea, el tipo de inicio y final de la línea.
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddLineAnnotation ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Open PDF 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 PDF 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
private static void AddLineAnnotation ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Open PDF 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 PDF document
editor . Save ( dataDir + "AddLineAnnotation_out.pdf" );
}