Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
以下代码片段也适用于 Aspose.PDF.Drawing 库。
一个 链接注释 代表一个超链接、一个其他地方的目标和一个文档。根据PDF标准,链接注释可以在三种情况下使用:打开页面视图、打开文件和打开网页。
创建注释时执行了几个额外步骤。我们使用了2个TextFragmentAbsorbers来查找片段进行演示。第一个用于链接注释文本,第二个指示文档中的某些位置。
// 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");
}
}
要创建注释,我们遵循了以下步骤:
LinkAnnotation
并传递页面对象和与注释对应的文本片段的矩形。Action
设置为 GoToAction
并传递 XYZExplicitDestination
作为所需目标。我们根据页面、左侧和顶部坐标以及缩放创建了 XYZExplicitDestination
。在处理各种文档时,会出现一种情况,即您在输入时不知道注释将指向何处。 在这种情况下,您可以使用特殊的(命名的)目标,代码将如下所示:
var destinationName = "Link2";
var linkAnnotation2 = new LinkAnnotation(page, linkFragments[2].Rectangle)
{
Action = new GoToAction(document, destinationName)
};
在另一个地方,您可以创建一个命名目标。
document.NamedDestinations.Add(destinationName,
new XYZExplicitDestination(
sampleTextFragments[2].Page,
sampleTextFragments[2].Rectangle.LLX,
sampleTextFragments[2].Rectangle.URX, 1));
创建打开文件的注释时使用相同的方法,如上面的示例所示。
var linkAnnotation3 = new LinkAnnotation(page, linkFragments[3].Rectangle)
{
Action = new GoToRemoteAction("another.pdf", 2)
};
不同之处在于我们将使用 GoToRemoteAction
而不是 GoToAction
。GoToRemoteAction的构造函数接受两个参数:文件名和页码。
您还可以使用另一种形式,传递文件名和某个目标。显然,您需要在使用之前创建这样的目标。
要打开网页,只需将 Action
设置为 GoToURIAction
对象。
您可以将超链接作为构造函数参数传递,或任何其他类型的URI。例如,您可以使用 callto
来实现拨打电话号码的操作。
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
};
您可以使用边框自定义链接注释。在下面的示例中,我们将创建一个宽度为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
};
另一个示例展示了如何模拟浏览器样式并为链接使用下划线。
var linkAnnotation5 = new LinkAnnotation(page, linkFragments[5].Rectangle)
{
Color = Color.Blue
};
linkAnnotation5.Border = new Border(linkAnnotation5)
{
Style = BorderStyle.Underline,
Width = 3
};
请尝试使用以下代码片段从PDF文档中获取LinkAnnotation。
// 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);
}
}
}
以下代码片段展示了如何从PDF文件中删除链接注释。为此,我们需要查找并删除第一页上的所有链接注释。之后,我们将保存删除了注释的文档。
// 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.