PDF管理控件Aspose.PDF for .Net使用教程(三十):创建,更新和提取链接

在本系列教程中,将为开发者带来Aspose.PDF for .NET的一系列使用教程,例如进行文档间的转换,如何标记PDF文件,如何使用表单和图表等等。本文将介绍创建,更新和提取链接。

Aspose.PDF for .NET是一种高PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务。API可以轻松用于生成、修改、转换、渲染、保护和打印PDF文档,而无需使用Adobe Acrobat。此外,API还提供PDF压缩选项,表格创建和操作,图形和图像功能,广泛的超链接功能,印章和水印任务,扩展的安全控制和自定义字体处理。

在接下来的系列教程中,将为开发者带来Aspose.PDF for .NET的一系列使用教程,例如进行文档间的转换,如何标记PDF文件,如何使用表单和图表等等。本文将介绍创建,更新和提取链接。

>>Aspose.PDF for .NET更新至最新版v20.3,欢迎下载体验。


在PDF文件中创建应用程序链接

通过将到应用程序的链接添加到文档中,可以从文档链接到应用程序。例如,当希望读者在教程中的特定位置采取特定措施或创建功能丰富的文档时,此功能很有用。要创建一个应用程序链接:

  1. 创建一个Document对象。
  2. 获取Page您要添加的链接。
  3. LinkAnnotation使用Page和Rectangle对象创建一个对象。
  4. 使用LinkAnnotation对象设置链接属性。
  5. 将设置为LaunchAction对象的Action属性。
  6. 创建LaunchAction对象时,请指定要启动的应用程序。
  7. 将链接添加到Page对象的Annotations属性。
  8. 使用Document对象的Save方法保存更新的PDF 。

以下代码段显示了如何在PDF文件中创建到应用程序的链接。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions();// Open documentDocument document = new Document( dataDir + "CreateApplicationLink.pdf");// Create linkPage page = document.Pages[1];LinkAnnotation link = new LinkAnnotation(page, new Aspose.Pdf.Rectangle(100, 100, 300, 300));link.Color = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Green);link.Action = new LaunchAction(document, dataDir + "CreateApplicationLink.pdf");page.Annotations.Add(link);dataDir = dataDir + "CreateApplicationLink_out.pdf";// Save updated documentdocument.Save(dataDir);

在PDF文件中创建PDF文档链接

.NET的Aspose.PDF允许您添加到外部PDF文件的链接,以便可以将多个文档链接在一起。要创建PDF文档链接:

  1. 创建一个Document对象。
  2. 获取Page要添加链接的特定内容。
  3. LinkAnnotation使用Page和Rectangle对象创建一个对象。
  4. 使用LinkAnnotation对象设置链接属性。
  5. 将Action属性设置为GoToRemoteAction对象。
  6. 创建GoToRemoteAction对象时,指定应启动的PDF文件以及应打开的页码。
  7. 将链接添加到Page对象的Annotations集合。
  8. 使用Document对象的Save方法保存更新的PDF 。

以下代码段显示了如何在PDF文件中创建PDF文档链接。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions();// Open documentDocument document = new Document(dataDir+ "CreateDocumentLink.pdf");// Create linkPage page = document.Pages[1];LinkAnnotation link = new LinkAnnotation(page, new Aspose.Pdf.Rectangle(100, 100, 300, 300));link.Color = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Green);link.Action = new GoToRemoteAction(dataDir + "RemoveOpenAction.pdf", 1);page.Annotations.Add(link);dataDir = dataDir + "CreateDocumentLink_out.pdf";// Save updated documentdocument.Save(dataDir);

更新PDF文件中的链接

如在PDF文件中添加超链接中所讨论的,LinkAnnotation该类使得可以在PDF文件中添加链接。还有一个类似的类,用于从PDF文件内部获取现有链接。如果需要更新现有链接,请使用此选项。要更新现有链接:

  1. 加载PDF文件。
  2. 转到PDF文件中的特定页面。
  3. 使用GoToAction对象的Destination属性指定链接目标。
  4. 目标页面是使用 XYZExplicitDestination 构造函数。

将链接目标设置为同一文档中的页面

以下代码段显示了如何更新PDF文件中的链接并将其目标设置为文档的第二页。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions();// Load the PDF fileDocument doc = new Document(dataDir + "UpdateLinks.pdf");// Get the first link annotation from first page of documentLinkAnnotation linkAnnot = (LinkAnnotation)doc.Pages[1].Annotations[1];// Modification link: change link destinationGoToAction goToAction = (GoToAction)linkAnnot.Action;// Specify the destination for link object// The first parameter is document object, second is destination page number.// The 5ht argument is zoom factor when displaying the respective page. When using 2, the page will be displayed in 200% zoomgoToAction.Destination = new Aspose.Pdf.Annotations.XYZExplicitDestination(1, 1, 2, 2);dataDir = dataDir + "PDFLINK_Modified_UpdateLinks_out.pdf";// Save the document with updated linkdoc.Save(dataDir);

将链接目标设置为网址

要更新超链接使其指向网址,请实例化该GoToURIAction对象并将其传递给LinkAnnotation的Action属性。以下代码段显示了如何更新PDF文件中的链接并将其目标设置为网址。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions();// Load the PDF fileDocument doc = new Document(dataDir + "UpdateLinks.pdf");       // Get the first link annotation from first page of documentLinkAnnotation linkAnnot = (LinkAnnotation)doc.Pages[1].Annotations[1];// Modification link: change link action and set target as web addresslinkAnnot.Action = new GoToURIAction("www.aspose.com");           dataDir = dataDir + "SetDestinationLink_out.pdf";// Save the document with updated linkdoc.Save(dataDir);

将链接目标设置为另一个PDF文件

以下代码段显示了如何更新PDF文件中的链接并将其目标设置为另一个PDF文件。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions();// Load the PDF fileDocument document = new Document(dataDir + "UpdateLinks.pdf");LinkAnnotation linkAnnot = (LinkAnnotation)document.Pages[1].Annotations[1];GoToRemoteAction goToR = (GoToRemoteAction)linkAnnot.Action;// Next line update destination, do not update filegoToR.Destination = new XYZExplicitDestination(2, 0, 0, 1.5);// Next line update filegoToR.File = new FileSpecification(dataDir +  "input.pdf");dataDir = dataDir + "SetTargetLink_out.pdf";// Save the document with updated linkdocument.Save(dataDir);

更新LinkAnnotation文本颜色

链接注释不包含文本。而是将文本放置在页面内容的注释下。因此,要更改文本的颜色,请替换页面文本的颜色,而不要尝试更改批注的颜色。以下代码段显示了如何更新PDF文件中链接注释的颜色。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions();// Load the PDF fileDocument doc = new Document(dataDir + "UpdateLinks.pdf");foreach (Annotation annotation in doc.Pages[1].Annotations){    if (annotation is LinkAnnotation)    {        // Search the text under the annotation        TextFragmentAbsorber ta = new TextFragmentAbsorber();        Rectangle rect = annotation.Rect;        rect.LLX -= 10;        rect.LLY -= 10;        rect.URX += 10;        rect.URY += 10;        ta.TextSearchOptions = new TextSearchOptions(rect);        ta.Visit(doc.Pages[1]);        // Change color of the text.        foreach (TextFragment tf in ta.TextFragments)        {            tf.TextState.ForegroundColor = Color.Red;        }    }}dataDir = dataDir + "UpdateLinkTextColor_out.pdf";// Save the document with updated linkdoc.Save(dataDir);

从PDF文件中提取链接

链接在PDF文件中表示为注释,因此要提取链接,请提取所有LinkAnnotation对象。

  1. 创建一个Document对象。
  2. 获取Page您要从中提取链接的链接。
  3. 使用AnnotationSelector该类LinkAnnotation从指定页面提取所有对象。
  4. 将AnnotationSelector对象传递给Page对象的Accept方法。
  5. IList使用AnnotationSelector对象的Selected属性将所有选定的链接注释获取到对象中。

以下代码段显示了如何从PDF文件提取链接。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions();// Open documentDocument document = new Document(dataDir+ "ExtractLinks.pdf");// Extract actionsPage page = document.Pages[1];AnnotationSelector selector = new AnnotationSelector(new LinkAnnotation(page, Aspose.Pdf.Rectangle.Trivial));page.Accept(selector);IListlist = selector.Selected;Annotation annotation = (Annotation)list[0];dataDir = dataDir + "ExtractLinks_out.pdf";// Save updated documentdocument.Save(dataDir);

还想要更多吗可以点击阅读【2019 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询 
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年1月13日
下一篇 2020年1月13日

相关推荐

发表回复

登录后才能评论