Menambahkan Anotasi ke File PDF yang Ada
Tambahkan Anotasi Teks Bebas di File PDF yang Ada (facades)
PdfContentEditor memungkinkan Anda untuk menambahkan anotasi dari berbagai jenis di file PDF yang ada. Anda dapat menggunakan metode yang sesuai untuk menambahkan anotasi tertentu. Misalnya, dalam cuplikan kode berikut, kami telah menggunakan metode CreateFreeText untuk menambahkan anotasi tipe FreeText .
Jenis anotasi apa pun dapat ditambahkan ke file PDF dengan cara yang sama. Pertama-tama, Anda perlu membuat objek tipe PdfContentEditor dan mengikat file PDF input menggunakan metode BindPdf . Kedua, Anda harus membuat objek Rectangle untuk menentukan area anotasi.
Setelah itu, Anda dapat memanggil metode CreateFreeText untuk menambahkan anotasi FreeText , dan kemudian menggunakan metode Save untuk menyimpan file PDF yang diperbarui.
Cuplikan kode berikut menunjukkan kepada Anda cara menambahkan anotasi teks bebas di file 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" );
}
Tambahkan Anotasi Teks di File PDF yang Ada (facades)
Dalam contoh ini juga, Anda perlu membuat objek tipe PdfContentEditor dan mengikat file PDF input menggunakan metode BindPdf . Kedua, Anda harus membuat objek Rectangle untuk menentukan area anotasi. Setelah itu, Anda dapat memanggil metode CreateFreeText untuk menambahkan anotasi FreeText, membuat judul anotasi Anda, dan nomor halaman di mana anotasi tersebut berada.
.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" );
}
Tambahkan Anotasi Garis di File PDF yang Ada (facades)
Kami juga menentukan Rectangle, koordinat awal dan akhir garis, nomor halaman, ketebalan, gaya dan warna bingkai anotasi, jenis garis putus-putus, jenis awal dan akhir garis.
.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" );
}