Adicionando Anotações a um arquivo PDF existente
Adicionar Anotação de Texto Livre em um Arquivo PDF Existente
PdfContentEditor permite que você adicione anotações de diferentes tipos em um arquivo PDF existente. Você pode usar o método respectivo para adicionar uma anotação específica. Por exemplo, no seguinte trecho de código, usamos o método CreateFreeText para adicionar uma anotação do tipo FreeText .
Qualquer tipo de anotações pode ser adicionado ao arquivo PDF da mesma forma. Primeiro de tudo, você precisa criar um objeto do tipo PdfContentEditor e vincular o arquivo PDF de entrada usando o método BindPdf . Em segundo lugar, você deve criar um objeto Rectangle para especificar a área da anotação.
Depois disso, você pode chamar o método CreateFreeText para adicionar a anotação FreeText e, em seguida, usar o método Save para salvar o arquivo PDF atualizado.
O seguinte trecho de código mostra como adicionar uma anotação de texto livre em um arquivo 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" );
}
Adicionar Anotação de Texto em um Arquivo PDF Existente
Neste exemplo também, você precisa criar um objeto do tipo PdfContentEditor e vincular o arquivo PDF de entrada usando o método BindPdf . Em segundo lugar, você deve criar um objeto Rectangle para especificar a área da anotação. Depois disso, você pode chamar o método CreateFreeText para adicionar a anotação FreeText, criar o título de suas anotações e o número da página em que a anotação está localizada.
.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" );
}
Adicionar Anotação de Linha em um Arquivo PDF Existente
Nós também especificamos o Rectangle, as coordenadas do início e do fim da linha, o número da página, a espessura, o estilo e a cor da moldura da anotação, o tipo de traço da linha, o tipo de início e fim da linha.
.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" );
}