翻譯|使用教程|編輯:李顯亮|2020-03-17 10:24:53.307|閱讀 387 次
概述:在本系列教程中,將為開發者帶來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文件中創建到應用程序的鏈接。
// 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);
.NET的Aspose.PDF允許您添加到外部PDF文件的鏈接,以便可以將多個文檔鏈接在一起。要創建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文件中添加超鏈接中所討論的,LinkAnnotation該類使得可以在PDF文件中添加鏈接。還有一個類似的類,用于從PDF文件內部獲取現有鏈接。如果需要更新現有鏈接,請使用此選項。要更新現有鏈接:
將鏈接目標設置為同一文檔中的頁面
以下代碼段顯示了如何更新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文件中表示為注釋,因此要提取鏈接,請提取所有LinkAnnotation對象。
以下代碼段顯示了如何從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),我們很高興為您提供查詢和咨詢。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn