Aspose.PDF for .NET是一种高PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务。API可以轻松用于生成、修改、转换、渲染、保护和打印PDF文档,而无需使用AdobeAcrobat。此外,API还提供PDF压缩选项,表格创建和操作,图形和图像功能,广泛的超链接功能,印章和水印任务,扩展的安全控制和自定义字体处理。
>>Aspose.PDF for .NET更新至最新版v20.3,欢迎下载体验。
在PDF文件中添加超链接
可以将超链接添加到PDF文件,以允许读者导航到PDF的另一部分或外部内容。为了向PDF文档添加Web超链接,需要以下步骤:
- 创建一个Document对象。
- 获取您要添加链接的页面类。
- LinkAnnotation使用Page和Rectangle对象创建一个对象。矩形对象用于指定页面上应添加链接的位置。
- 将Action属性设置为GoToURIAction指定远程URI位置的对象。
- 显示超链接文本,请在类似于LinkAnnotation放置对象的位置上添加文本字符串。
-
要添加自由文本:
- 实例化一个FreeTextAnnotation对象。它还接受Page和Rectangle对象作为参数,因此可以提供与针对LinkAnnotation构造函数指定的值相同的值。
- 使用FreeTextAnnotation对象的Contents属性,指定应在输出PDF中显示的字符串。
- 将LinkAnnotation和FreeTextAnnotation对象的边框宽度都设置为0,这样它们就不会出现在PDF文档中。
- 一旦LinkAnnotation和FreeTextAnnotation对象已经确定,添加这些链接的Page对象的Annotations集合。
- 使用Document对象的Save方法保存更新的PDF 。
以下代码段显示了如何向PDF文件添加超链接。
// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions();// Open documentDocument document = new Document(dataDir + "AddHyperlink.pdf");// Create linkPage page = document.Pages[1];// Create Link annotation objectLinkAnnotation link = new LinkAnnotation(page, new Aspose.Pdf.Rectangle(100, 100, 300, 300));// Create border object for LinkAnnotationBorder border = new Border(link);// Set the border width value as 0border.Width = 0;// Set the border for LinkAnnotationlink.Border = border;// Specify the link type as remote URIlink.Action = new GoToURIAction("www.aspose.com");// Add link annotation to annotations collection of first page of PDF filepage.Annotations.Add(link);// Create Free Text annotationFreeTextAnnotation 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 texttextAnnotation.Contents = "Link to Aspose website";// Set the border for Free Text AnnotationtextAnnotation.Border = border;// Add FreeText annotation to annotations collection of first page of Documentdocument.Pages[1].Annotations.Add(textAnnotation);dataDir = dataDir + "AddHyperlink_out.pdf";// Save updated documentdocument.Save(dataDir);
创建指向同一PDF页面的超链接
用于.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 instanceDocument doc = new Document();// Add page to pages collection of PDF filePage page = doc.Pages.Add();// Create Text Fragment instanceAspose.Pdf.Text.TextFragment text = new Aspose.Pdf.Text.TextFragment("link page number test to page 7");// Create local hyperlink instanceAspose.Pdf.LocalHyperlink link = new Aspose.Pdf.LocalHyperlink();// Set target page for link instancelink.TargetPageNumber = 7;// Set TextFragment hyperlinktext.Hyperlink = link;// Add text to paragraphs collection of Pagepage.Paragraphs.Add(text);// Create new TextFragment instancetext = new TextFragment("link page number test to page 1");// TextFragment should be added over new pagetext.IsInNewPage = true;// Create another local hyperlink instancelink = new LocalHyperlink();// Set Target page for second hyperlinklink.TargetPageNumber = 1;// Set link for second TextFragmenttext.Hyperlink = link;// Add text to paragraphs collection of page objectpage.Paragraphs.Add(text); dataDir = dataDir + "CreateLocalHyperlink_out.pdf";// Save updated documentdoc.Save(dataDir);
获取PDF超链接目标(URL)
链接在PDF文件中表示为注释,可以添加,更新或删除它们。用于.NET的Aspose.PDF还支持获取PDF文件中超链接的目标(URL)。获取链接的URL,需要以下步骤:
- 创建一个Document对象。
- 获取您要添加链接的页面类。
- 使用AnnotationSelector该类LinkAnnotation从指定页面提取所有对象。
- 将AnnotationSelector对象传递给Page对象的Accept()方法。
- IList使用AnnotationSelector对象的Selected属性将所有选定的链接注释获取到对象中。
- 提取LinkAnnotation Actionas GoToURIAction。
以下代码段显示了如何从PDF文件获取超链接目标(URL)。
// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions();// Load the PDF fileDocument document = new Document(dataDir + "input.pdf");// Traverse through all the page of PDFforeach (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 fileDocument document = new Document(dataDir + "input.pdf");// Iterate through each page of PDFforeach (Page page in document.Pages){ // Show link annotation ShowLinkAnnotations(page);}
还想要更多吗可以点击阅读【2019 · Aspose最新资源整合】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!