没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
翻译|使用教程|编辑:李显亮|2020-03-25 10:33:35.720|阅读 313 次
概述:在本系列教程中,将为开发者带来Aspose.PDF for .NET的一系列使用教程,例如进行文档间的转换,如何标记PDF文件,如何使用表单和图表等等。本文将介绍添加和获取超链接。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
相关链接:
Aspose.PDF for .NET是一种高PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务。API可以轻松用于生成、修改、转换、渲染、保护和打印PDF文档,而无需使用Adobe Acrobat。此外,API还提供PDF压缩选项,表格创建和操作,图形和图像功能,广泛的超链接功能,印章和水印任务,扩展的安全控制和自定义字体处理。
在接下来的系列教程中,将为开发者带来Aspose.PDF for .NET的一系列使用教程,例如进行文档间的转换,如何标记PDF文件,如何使用表单和图表等等。本文将介绍如何添加和获取超链接。
>>Aspose.PDF for .NET更新至最新版v20.3,欢迎下载体验。
可以将超链接添加到PDF文件,以允许读者导航到PDF的另一部分或外部内容。为了向PDF文档添加Web超链接,需要以下步骤:
以下代码段显示了如何向PDF文件添加超链接。
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions();
// Open document
Document document = new Document(dataDir + "AddHyperlink.pdf");
// Create link
Page page = document.Pages[1];
// Create Link annotation object
LinkAnnotation link = new LinkAnnotation(page, new Aspose.Pdf.Rectangle(100, 100, 300, 300));
// Create border object for LinkAnnotation
Border border = new Border(link);
// Set the border width value as 0
border.Width = 0;
// Set the border for LinkAnnotation
link.Border = border;
// Specify the link type as remote URI
link.Action = new GoToURIAction("www.aspose.com");
// Add link annotation to annotations collection of first page of PDF file
page.Annotations.Add(link);
// Create Free Text annotation
FreeTextAnnotation textAnnotation = new FreeTextAnnotation(document.Pages[1], new Aspose.Pdf.Rectangle(100, 100, 300, 300), new DefaultAppearance(Aspose.Pdf.Text.FontRepository.FindFont("TimesNewRoman"), 10, System.Drawing.Color.Blue));
// String to be added as Free text
textAnnotation.Contents = "Link to Aspose website";
// Set the border for Free Text Annotation
textAnnotation.Border = border;
// Add FreeText annotation to annotations collection of first page of Document
document.Pages[1].Annotations.Add(textAnnotation);
dataDir = dataDir + "AddHyperlink_out.pdf";
// Save updated document
document.Save(dataDir);
用于.NET的Aspose.PDF为PDF创建及其操纵提供了强大的功能。它还提供了向PDF页面添加链接的功能,并且链接可以直接指向另一个PDF文件中的页面,Web URL,启动应用程序的链接,甚至可以链接到同一PDF文件中的页面。为了添加本地超链接(链接到同一PDF文件中的页面),将名为LocalHyperlink的类添加到Aspose.PDF命名空间,并且该类具有名为TargetPageNumber的属性,该属性用于指定超链接的目标/目标页面。
为了添加本地超链接,我们需要创建一个TextFragment,以便可以将链接与TextFragment关联。该TextFragment类有一个名为属性超链接是用来关联LocalHyperlink实例。以下代码片段显示了实现此要求的步骤。
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions();
// Create Document instance
Document doc = new Document();
// Add page to pages collection of PDF file
Page page = doc.Pages.Add();
// Create Text Fragment instance
Aspose.Pdf.Text.TextFragment text = new Aspose.Pdf.Text.TextFragment("link page number test to page 7");
// Create local hyperlink instance
Aspose.Pdf.LocalHyperlink link = new Aspose.Pdf.LocalHyperlink();
// Set target page for link instance
link.TargetPageNumber = 7;
// Set TextFragment hyperlink
text.Hyperlink = link;
// Add text to paragraphs collection of Page
page.Paragraphs.Add(text);
// Create new TextFragment instance
text = new TextFragment("link page number test to page 1");
// TextFragment should be added over new page
text.IsInNewPage = true;
// Create another local hyperlink instance
link = new LocalHyperlink();
// Set Target page for second hyperlink
link.TargetPageNumber = 1;
// Set link for second TextFragment
text.Hyperlink = link;
// Add text to paragraphs collection of page object
page.Paragraphs.Add(text);
dataDir = dataDir + "CreateLocalHyperlink_out.pdf";
// Save updated document
doc.Save(dataDir);
链接在PDF文件中表示为注释,可以添加,更新或删除它们。用于.NET的Aspose.PDF还支持获取PDF文件中超链接的目标(URL)。获取链接的URL,需要以下步骤:
以下代码段显示了如何从PDF文件获取超链接目标(URL)。
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions();
// Load the PDF file
Document document = new Document(dataDir + "input.pdf");
// Traverse through all the page of PDF
foreach (Aspose.Pdf.Page page in document.Pages)
{
// Get the link annotations from particular page
AnnotationSelector selector = new AnnotationSelector(new Aspose.Pdf.Annotations.LinkAnnotation(page, Aspose.Pdf.Rectangle.Trivial));
page.Accept(selector);
// Create list holding all the links
IListlist = selector.Selected;
// Iterate through invidiaul item inside list
foreach (LinkAnnotation a in list)
{
// Print the destination URL
Console.WriteLine("\nDestination: " + (a.Action as Aspose.Pdf.Annotations.GoToURIAction).URI + "\n");
}
}
超链接分为两部分:文档中显示的文本和目标URL。在某些情况下,它是文本,而不是我们需要的URL。PDF文件中的文本和注释/操作由不同的实体表示。页面上的文本只是一组单词和字符,而注释带来了一些交互性,例如超链接中固有的交互性。
要查找URL内容,您需要同时使用注释和文本。该Annotation对象不具有本身具有的文本,但页面上的文本下坐。因此,要获取文本,将Annotation给出URL的范围,而Text对象将给出URL的内容。请参见以下代码段。
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions();
// Load the PDF file
Document document = new Document(dataDir + "input.pdf");
// Iterate through each page of PDF
foreach (Page page in document.Pages)
{
// Show link annotation
ShowLinkAnnotations(page);
}
还想要更多吗?您可以点击阅读【2019 · Aspose最新资源整合】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(642018183),我们很高兴为您提供查询和咨询。 本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn




大型SaaS系统的自动化测试常常受制于界面变化快、结构复杂、加载机制多变等因素。从元素识别到脚本管理,SmartBear TestComplete帮助Salesforce建了可靠的自动化测试体系。
BarTender 标签管理系统,正是帮助企业轻松实现 GS1 标准化标签设计、编码生成与信息联动的强大工具。
Parasoft C/C++test 是一款功能强大的 C/C++ 软件测试工具,集成了静态代码分析、单元测试、集成测试和覆盖率分析等功能,单元测试作为其关键功能之一,为了适配多样化的目标部署环境,C/C++test 设计了灵活的测试结果收集机制。通过Socket通讯方式来收集单元测试结果,从而扩展其测试覆盖范围与应用场景。
Parasoft C/C++test作为一款功能全面的自动化代码质量保障工具,为C/C++开发者提供了静态分析、单元测试和运行时错误检测等核心功能。通过将其与轻量级且广受欢迎的VScode编辑器集成,开发团队可以在熟悉的编码环境中实时获得代码质量反馈,有效提升开发效率与代码可靠性。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@ke049m.cn
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
半岛外围网上直营