原創|使用教程|編輯:李顯亮|2019-09-04 13:48:49.340|閱讀 633 次
概述:在接下來的系列教程中,將為開發者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉換,如何標記PDF文件,如何使用表單和圖表等等。本文介紹如何添加和獲取超鏈接,希望對您有所幫助。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.PDF for .NET是一種高PDF處理和解析API,用于在跨平臺應用程序中執行文檔管理和操作任務。API可以輕松用于生成、修改、轉換、渲染、保護和打印PDF文檔,而無需使用Adobe Acrobat。此外,API還提供PDF壓縮選項,表格創建和操作,圖形和圖像功能,廣泛的超鏈接功能,印章和水印任務,擴展的安全控制和自定義字體處理。
在接下來的系列教程中,將為開發者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉換,如何標記PDF文件,如何使用表單和圖表等等。
可以添加到PDF文件的超鏈接,以允許讀者導航到PDF的另一部分或外部內容。為了向PDF文檔添加Web超鏈接:
以下代碼段顯示如何向PDF文件添加超鏈接:
// 文檔目錄的路徑。 string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions(); //打開文檔 Document document = new Document(dataDir + "AddHyperlink.pdf"); //創建鏈接 Page page = document.Pages[1]; //創建鏈接注釋對象 LinkAnnotation link = new LinkAnnotation(page, new Aspose.Pdf.Rectangle(100, 100, 300, 300)); //為LinkAnnotation創建邊框對象 Border border = new Border(link); // 將邊框寬度值設置為0 border.Width = 0; //設置LinkAnnotation的邊框 link.Border = border; //將鏈接類型指定為遠程URI link.Action = new GoToURIAction("www.aspose.com"); //將鏈接注釋添加到PDF文件第一頁的注釋集合中 page.Annotations.Add(link); //創建自由文本注釋 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)); //要添加為自由文本的字符串 textAnnotation.Contents = "Link to Aspose website"; //設置自由文本注釋的邊框 textAnnotation.Border = border; //將FreeText注釋添加到Document第一頁的注釋集合中 document.Pages[1].Annotations.Add(textAnnotation); dataDir = dataDir + "AddHyperlink_out.pdf"; // 保存更新的文檔 document.Save(dataDir);
Aspose.PDF for .NET為PDF創建及其操作提供了很好的功能。它還提供了添加PDF頁面鏈接的功能,鏈接可以指向另一個PDF文件中的頁面,Web URL,啟動應用程序的鏈接,甚至鏈接到同一PDF文件中的頁面。為了添加本地超鏈接(指向同一PDF文件中的頁面的鏈接),將一個名為LocalHyperlink的類添加到Aspose.PDF命名空間,并且此類具有名為TargetPageNumber的屬性,該屬性用于指定超鏈接的目標/目標頁面。
為了添加本地超鏈接,需要創建一個TextFragment,以便鏈接可以與TextFragment相關聯。該TextFragment類有一個屬性命名超鏈接,用來LocalHyperlink實例相關聯。以下代碼段顯示了完成此要求的步驟。
//文檔目錄的路徑。 string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions(); //創建文檔實例 Document doc = new Document(); //將頁面添加到PDF文件的頁面集合中 Page page = doc.Pages.Add(); //創建文本片段實例 Aspose.Pdf.Text.TextFragment text = new Aspose.Pdf.Text.TextFragment("link page number test to page 7"); //創建本地超鏈接實例 Aspose.Pdf.LocalHyperlink link = new Aspose.Pdf.LocalHyperlink(); //設置鏈接實例的目標頁面 link.TargetPageNumber = 7; //設置TextFragment超鏈接 text.Hyperlink = link; //將文本添加到Page的集合集合中 page.Paragraphs.Add(text); // 創建新的TextFragment實例 text = new TextFragment("link page number test to page 1"); // 應在新頁面上添加TextFragment text.IsInNewPage = true; // 創建另一個本地超鏈接實例 link = new LocalHyperlink(); //為第二個超鏈接設置目標頁面 link.TargetPageNumber = 1; // 設置第二個TextFragment的鏈接 text.Hyperlink = link; //將文本添加到頁面對象的段落集合中 page.Paragraphs.Add(text); dataDir = dataDir + "CreateLocalHyperlink_out.pdf"; //保存更新的文檔 doc.Save(dataDir);
鏈接在PDF文件中表示為注釋,可以添加,更新或刪除它們。Aspose.PDF for .NET還支持在PDF文件中獲取超鏈接的目標(URL)。要獲取鏈接的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內容。請參閱以下代碼段:
//文檔目錄的路徑。 string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions(); //加載PDF文件 Document document = new Document(dataDir + "input.pdf"); // 遍歷PDF的每一頁 foreach (Page page in document.Pages) { //顯示鏈接注釋 ShowLinkAnnotations(page); }
在PDF查看器(如Adobe Reader)中查看PDF文件時,文件通常會在第一頁上打開。但是,可以將文件設置為在其他頁面上打開。
XYZExplicitDestination類允許你在要打開一個PDF文件中指定的頁面。將GoToAction對象值傳遞給Document類OpenAction屬性時,文檔將在針對該XYZExplicitDestination對象指定的頁面處打開。以下代碼段顯示如何將頁面指定為文檔打開操作。
//文檔目錄的路徑 string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions(); //加載PDF文件 Document doc = new Document(dataDir + "SpecifyPageWhenViewing.pdf"); //獲取文檔第二頁的實例 Page page2 = doc.Pages[2]; // 創建變量以設置目標頁面的縮放系數 double zoom = 1; //創建GoToAction實例 GoToAction action = new GoToAction(doc.Pages[2]); //轉到2頁 action.Destination = new XYZExplicitDestination(page2, 0, page2.Rect.Height, zoom); //設置文檔打開操作 doc.OpenAction = action; //保存更新的文檔 doc.Save(dataDir + "goto2page_out.pdf");
*想要購買Aspose.PDF for .NET正版授權的朋友可以了解詳情哦~
歡迎加入ASPOSE技術交流QQ群,各類資源及時分享,技術問題交流討論!(掃描下方二維碼加入群聊)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn