Добавление аннотаций в существующий PDF файл
Добавление аннотации свободного текста в существующий PDF файл
PdfContentEditor позволяет добавлять аннотации различных типов в существующий PDF файл. Вы можете использовать соответствующий метод для добавления конкретной аннотации. Например, в следующем фрагменте кода мы использовали метод CreateFreeText для добавления аннотации типа FreeText .
Любой тип аннотаций можно добавить в PDF файл тем же способом. Прежде всего, вам нужно создать объект типа PdfContentEditor и связать входной PDF файл с помощью метода BindPdf . Во-вторых, вам нужно создать объект Rectangle, чтобы указать область аннотации.
После этого вы можете вызвать метод CreateFreeText для добавления аннотации FreeText , а затем использовать метод Save для сохранения обновленного PDF файла.
Следующий фрагмент кода показывает, как добавить аннотацию свободного текста в 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" );
}
Добавление текстовой аннотации в существующий PDF файл
В этом примере также необходимо создать объект типа PdfContentEditor и связать входной PDF файл с помощью метода BindPdf . Во-вторых, вам нужно создать объект Rectangle, чтобы указать область аннотации. После этого вы можете вызвать метод CreateFreeText для добавления аннотации FreeText, создать заголовок ваших аннотаций и номер страницы, на которой находится аннотация.
.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" );
}
Добавление аннотации линии в существующий PDF файл
Мы также указываем Rectangle, координаты начала и конца линии, номер страницы, толщину, стиль и цвет рамки аннотации, тип штриха линии, тип начала и конца линии.
.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" );
}