PDF管理控件Aspose.PDF for .Net使用教程(三十):創建,更新和提取鏈接
Aspose.PDF for .NET是一種高PDF處理和解析API,用于在跨平臺應用程序中執行文檔管理和操作任務。API可以輕松用于生成、修改、轉換、渲染、保護和打印PDF文檔,而無需使用Adobe Acrobat。此外,API還提供PDF壓縮選項,表格創建和操作,圖形和圖像功能,廣泛的超鏈接功能,印章和水印任務,擴展的安全控制和自定義字體處理。
在接下來的系列教程中,將為開發者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉換,如何標記PDF文件,如何使用表單和圖表等等。本文將介紹創建,更新和提取鏈接。
>>Aspose.PDF for .NET更新至最新版v20.3,歡迎下載體驗。
在PDF文件中創建應用程序鏈接
通過將到應用程序的鏈接添加到文檔中,可以從文檔鏈接到應用程序。例如,當希望讀者在教程中的特定位置采取特定措施或創建功能豐富的文檔時,此功能很有用。要創建一個應用程序鏈接:
- 創建一個Document對象。
- 獲取Page您要添加的鏈接。
- LinkAnnotation使用Page和Rectangle對象創建一個對象。
- 使用LinkAnnotation對象設置鏈接屬性。
- 將設置為LaunchAction對象的Action屬性。
- 創建LaunchAction對象時,請指定要啟動的應用程序。
- 將鏈接添加到Page對象的Annotations屬性。
- 使用Document對象的Save方法保存更新的PDF 。
以下代碼段顯示了如何在PDF文件中創建到應用程序的鏈接。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions(); // Open document Document document = new Document( dataDir + "CreateApplicationLink.pdf"); // Create link Page 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 document document.Save(dataDir);
在PDF文件中創建PDF文檔鏈接
.NET的Aspose.PDF允許您添加到外部PDF文件的鏈接,以便可以將多個文檔鏈接在一起。要創建PDF文檔鏈接:
- 創建一個Document對象。
- 獲取Page要添加鏈接的特定內容。
- LinkAnnotation使用Page和Rectangle對象創建一個對象。
- 使用LinkAnnotation對象設置鏈接屬性。
- 將Action屬性設置為GoToRemoteAction對象。
- 創建GoToRemoteAction對象時,指定應啟動的PDF文件以及應打開的頁碼。
- 將鏈接添加到Page對象的Annotations集合。
- 使用Document對象的Save方法保存更新的PDF 。
以下代碼段顯示了如何在PDF文件中創建PDF文檔鏈接。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions(); // Open document Document document = new Document(dataDir+ "CreateDocumentLink.pdf"); // Create link Page 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 document document.Save(dataDir);
更新PDF文件中的鏈接
如在PDF文件中添加超鏈接中所討論的,LinkAnnotation該類使得可以在PDF文件中添加鏈接。還有一個類似的類,用于從PDF文件內部獲取現有鏈接。如果需要更新現有鏈接,請使用此選項。要更新現有鏈接:
- 加載PDF文件。
- 轉到PDF文件中的特定頁面。
- 使用GoToAction對象的Destination屬性指定鏈接目標。
- 目標頁面是使用 XYZExplicitDestination 構造函數。
將鏈接目標設置為同一文檔中的頁面
以下代碼段顯示了如何更新PDF文件中的鏈接并將其目標設置為文檔的第二頁。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions(); // Load the PDF file Document doc = new Document(dataDir + "UpdateLinks.pdf"); // Get the first link annotation from first page of document LinkAnnotation linkAnnot = (LinkAnnotation)doc.Pages[1].Annotations[1]; // Modification link: change link destination GoToAction 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% zoom goToAction.Destination = new Aspose.Pdf.Annotations.XYZExplicitDestination(1, 1, 2, 2); dataDir = dataDir + "PDFLINK_Modified_UpdateLinks_out.pdf"; // Save the document with updated link doc.Save(dataDir);
將鏈接目標設置為網址
要更新超鏈接使其指向網址,請實例化該GoToURIAction對象并將其傳遞給LinkAnnotation的Action屬性。以下代碼段顯示了如何更新PDF文件中的鏈接并將其目標設置為網址。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions(); // Load the PDF file Document doc = new Document(dataDir + "UpdateLinks.pdf"); // Get the first link annotation from first page of document LinkAnnotation linkAnnot = (LinkAnnotation)doc.Pages[1].Annotations[1]; // Modification link: change link action and set target as web address linkAnnot.Action = new GoToURIAction("www.aspose.com"); dataDir = dataDir + "SetDestinationLink_out.pdf"; // Save the document with updated link doc.Save(dataDir);
將鏈接目標設置為另一個PDF文件
以下代碼段顯示了如何更新PDF文件中的鏈接并將其目標設置為另一個PDF文件。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions(); // Load the PDF file Document 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 file goToR.Destination = new XYZExplicitDestination(2, 0, 0, 1.5); // Next line update file goToR.File = new FileSpecification(dataDir + "input.pdf"); dataDir = dataDir + "SetTargetLink_out.pdf"; // Save the document with updated link document.Save(dataDir);
更新LinkAnnotation文本顏色
鏈接注釋不包含文本。而是將文本放置在頁面內容的注釋下。因此,要更改文本的顏色,請替換頁面文本的顏色,而不要嘗試更改批注的顏色。以下代碼段顯示了如何更新PDF文件中鏈接注釋的顏色。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions(); // Load the PDF file Document 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 link doc.Save(dataDir);
從PDF文件中提取鏈接
鏈接在PDF文件中表示為注釋,因此要提取鏈接,請提取所有LinkAnnotation對象。
- 創建一個Document對象。
- 獲取Page您要從中提取鏈接的鏈接。
- 使用AnnotationSelector該類LinkAnnotation從指定頁面提取所有對象。
- 將AnnotationSelector對象傳遞給Page對象的Accept方法。
- IList使用AnnotationSelector對象的Selected屬性將所有選定的鏈接注釋獲取到對象中。
以下代碼段顯示了如何從PDF文件提取鏈接。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions(); // Open document Document document = new Document(dataDir+ "ExtractLinks.pdf"); // Extract actions Page 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 document document.Save(dataDir);還想要更多嗎?您可以點擊閱讀【2019 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問或需求,請隨時加入Aspose技術交流群(642018183),我們很高興為您提供查詢和咨詢。