Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Potongan kode berikut juga bekerja dengan pustaka Aspose.PDF.Drawing.
Anotasi Tautan mewakili hyperlink, tujuan di tempat lain, dan dokumen. Menurut Standar PDF, anotasi tautan dapat digunakan dalam tiga kasus: membuka tampilan halaman, membuka file, dan membuka halaman web.
Beberapa langkah tambahan dilakukan untuk membuat anotasi. Kami menggunakan 2 TextFragmentAbsorbers untuk menemukan fragmen untuk demo. Yang pertama adalah untuk teks anotasi tautan, dan yang kedua menunjukkan beberapa tempat di dokumen.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddLinkAnnotation()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "Link Annotation Demo.pdf"))
{
// Get first page
var page = document.Pages[1];
// Define regular expressions for text fragments
var regEx1 = new Regex(@"Link Annotation Demo \d");
var regEx2 = new Regex(@"Sample text \d");
// Create TextFragmentAbsorber for the first regular expression
var textFragmentAbsorber1 = new Aspose.Pdf.Text.TextFragmentAbsorber(regEx1);
textFragmentAbsorber1.Visit(document);
// Create TextFragmentAbsorber for the second regular expression
var textFragmentAbsorber2 = new Aspose.Pdf.Text.TextFragmentAbsorber(regEx2);
textFragmentAbsorber2.Visit(document);
// Get the text fragments for both absorbers
var linkFragments = textFragmentAbsorber1.TextFragments;
var sampleTextFragments = textFragmentAbsorber2.TextFragments;
// Create a LinkAnnotation
var linkAnnotation1 = new Aspose.Pdf.Annotations.LinkAnnotation(page, linkFragments[1].Rectangle)
{
Action = new Aspose.Pdf.Annotations.GoToAction(
new Aspose.Pdf.Annotations.XYZExplicitDestination(
sampleTextFragments[1].Page,
sampleTextFragments[1].Rectangle.LLX,
sampleTextFragments[1].Rectangle.URX, 1.5))
};
// Add the link annotation to the page
page.Annotations.Add(linkAnnotation1);
// Save PDF document
document.Save(dataDir + "AddLinkAnnotation_out.pdf");
}
}
Untuk membuat anotasi kami telah mengikuti langkah-langkah tertentu:
LinkAnnotation
dan lewati objek halaman serta persegi panjang dari fragmen teks yang sesuai dengan anotasi.Action
sebagai GoToAction
dan lewati XYZExplicitDestination
sebagai tujuan yang diinginkan. Kami membuat XYZExplicitDestination
berdasarkan halaman, koordinat kiri dan atas, serta zoom.Saat memproses berbagai dokumen, muncul situasi ketika Anda mengetik dan tidak tahu ke mana anotasi akan menunjuk. Dalam hal ini, Anda dapat menggunakan tujuan khusus (bernama) dan kode akan terlihat seperti di sini:
var destinationName = "Link2";
var linkAnnotation2 = new LinkAnnotation(page, linkFragments[2].Rectangle)
{
Action = new GoToAction(document, destinationName)
};
Di tempat lain Anda dapat membuat Tujuan Bernama.
document.NamedDestinations.Add(destinationName,
new XYZExplicitDestination(
sampleTextFragments[2].Page,
sampleTextFragments[2].Rectangle.LLX,
sampleTextFragments[2].Rectangle.URX, 1));
Pendekatan yang sama digunakan saat membuat anotasi untuk membuka file, seperti dalam contoh di atas.
var linkAnnotation3 = new LinkAnnotation(page, linkFragments[3].Rectangle)
{
Action = new GoToRemoteAction("another.pdf", 2)
};
Perbedaannya adalah bahwa kami akan menggunakan GoToRemoteAction
alih-alih GoToAction
. Konstruktor GoToRemoteAction mendapatkan dua parameter: nama file dan nomor halaman. Anda juga dapat menggunakan bentuk lain dan melewatkan nama file serta beberapa tujuan. Jelas, Anda perlu membuat tujuan tersebut sebelum menggunakannya.
Untuk membuka halaman web, cukup atur Action
dengan objek GoToURIAction
. Anda dapat melewatkan hyperlink sebagai parameter konstruktor atau jenis URI lainnya. Misalnya, Anda dapat menggunakan callto
untuk menerapkan tindakan dengan nomor telepon.
var linkAnnotation4 = new LinkAnnotation(page, linkFragments[4].Rectangle)
{
Action = new GoToURIAction("https://products.aspose.com/pdf/net"),
// Create Link Annotation and set the action to call a phone number
//Action = new GoToURIAction("callto:678-555-0103")
Color = Color.Blue
};
Anda dapat menyesuaikan Anotasi Tautan menggunakan batas. Dalam contoh di bawah ini, kami akan membuat batas putus-putus biru dengan lebar 3pt.
var linkAnnotation4 = new LinkAnnotation(page, linkFragments[4].Rectangle)
{
Action = new GoToURIAction("https://products.aspose.com/pdf/net"),
Color = Color.Blue
};
linkAnnotation4.Border = new Border(linkAnnotation4)
{
Style = BorderStyle.Dashed,
Dash = new Dash(5, 5),
Width = 3
};
Contoh lain menunjukkan bagaimana mensimulasikan gaya browser dan menggunakan Garis Bawah untuk tautan.
var linkAnnotation5 = new LinkAnnotation(page, linkFragments[5].Rectangle)
{
Color = Color.Blue
};
linkAnnotation5.Border = new Border(linkAnnotation5)
{
Style = BorderStyle.Underline,
Width = 3
};
Silakan coba menggunakan potongan kode berikut untuk Mengambil LinkAnnotation dari dokumen PDF.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetLinkAnnotations()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "SimpleResume_mod.pdf"))
{
// Get all Link annotations from the first page
var linkAnnotations = document.Pages[1].Annotations.Where(a => a.AnnotationType == Aspose.Pdf.Annotations.AnnotationType.Link);
// Iterate through each Link annotation
foreach (Aspose.Pdf.Annotations.Annotation annot in linkAnnotations)
{
// Print the URL of each Link Annotation
Console.WriteLine("URI: " + ((annot as Aspose.Pdf.Annotations.LinkAnnotation).Action as Aspose.Pdf.Annotations.GoToURIAction).URI);
// Create a TextAbsorber to extract text within the annotation's rectangle
var absorber = new Aspose.Pdf.Text.TextAbsorber();
absorber.TextSearchOptions.LimitToPageBounds = true;
absorber.TextSearchOptions.Rectangle = annot.Rect;
// Accept the absorber for the first page
document.Pages[1].Accept(absorber);
// Extract and print the text associated with the hyperlink
string extractedText = absorber.Text;
Console.WriteLine(extractedText);
}
}
}
Potongan kode berikut menunjukkan cara Menghapus Anotasi Tautan dari file PDF. Untuk ini, kami perlu menemukan dan menghapus semua anotasi tautan di halaman 1. Setelah ini, kami akan menyimpan dokumen dengan anotasi yang dihapus.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void DeleteLinkAnnotations()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "SimpleResume_mod.pdf"))
{
// Find and delete all link annotations on the 1st page
var linkAnnotations = document.Pages[1].Annotations.Where(a => a.AnnotationType == Aspose.Pdf.Annotations.AnnotationType.Link);
foreach (var la in linkAnnotations)
{
document.Pages[1].Annotations.Delete(la);
}
// Save PDF document
document.Save(dataDir + "DeleteLinkAnnotations_out.pdf");
}
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.