翻譯|使用教程|編輯:李顯亮|2020-03-25 10:33:35.720|閱讀 305 次
概述:在本系列教程中,將為開發(fā)者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉換,如何標記PDF文件,如何使用表單和圖表等等。本文將介紹添加和獲取超鏈接。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.PDF for .NET是一種高PDF處理和解析API,用于在跨平臺應用程序中執(zhí)行文檔管理和操作任務。API可以輕松用于生成、修改、轉換、渲染、保護和打印PDF文檔,而無需使用Adobe Acrobat。此外,API還提供PDF壓縮選項,表格創(chuàng)建和操作,圖形和圖像功能,廣泛的超鏈接功能,印章和水印任務,擴展的安全控制和自定義字體處理。
在接下來的系列教程中,將為開發(fā)者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉換,如何標記PDF文件,如何使用表單和圖表等等。本文將介紹如何添加和獲取超鏈接。
>>Aspose.PDF for .NET更新至最新版v20.3,歡迎下載體驗。
可以將超鏈接添加到PDF文件,以允許讀者導航到PDF的另一部分或外部內容。為了向PDF文檔添加Web超鏈接,需要以下步驟:
以下代碼段顯示了如何向PDF文件添加超鏈接。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions(); // Open document Document document = new Document(dataDir + "AddHyperlink.pdf"); // Create link Page page = document.Pages[1]; // Create Link annotation object LinkAnnotation link = new LinkAnnotation(page, new Aspose.Pdf.Rectangle(100, 100, 300, 300)); // Create border object for LinkAnnotation Border border = new Border(link); // Set the border width value as 0 border.Width = 0; // Set the border for LinkAnnotation link.Border = border; // Specify the link type as remote URI link.Action = new GoToURIAction("www.aspose.com"); // Add link annotation to annotations collection of first page of PDF file page.Annotations.Add(link); // Create Free Text annotation 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)); // String to be added as Free text textAnnotation.Contents = "Link to Aspose website"; // Set the border for Free Text Annotation textAnnotation.Border = border; // Add FreeText annotation to annotations collection of first page of Document document.Pages[1].Annotations.Add(textAnnotation); dataDir = dataDir + "AddHyperlink_out.pdf"; // Save updated document document.Save(dataDir);
用于.NET的Aspose.PDF為PDF創(chuàng)建及其操縱提供了強大的功能。它還提供了向PDF頁面添加鏈接的功能,并且鏈接可以直接指向另一個PDF文件中的頁面,Web URL,啟動應用程序的鏈接,甚至可以鏈接到同一PDF文件中的頁面。為了添加本地超鏈接(鏈接到同一PDF文件中的頁面),將名為LocalHyperlink的類添加到Aspose.PDF命名空間,并且該類具有名為TargetPageNumber的屬性,該屬性用于指定超鏈接的目標/目標頁面。
為了添加本地超鏈接,我們需要創(chuàng)建一個TextFragment,以便可以將鏈接與TextFragment關聯(lián)。該TextFragment類有一個名為屬性超鏈接是用來關聯(lián)LocalHyperlink實例。以下代碼片段顯示了實現(xiàn)此要求的步驟。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions(); // Create Document instance Document doc = new Document(); // Add page to pages collection of PDF file Page page = doc.Pages.Add(); // Create Text Fragment instance Aspose.Pdf.Text.TextFragment text = new Aspose.Pdf.Text.TextFragment("link page number test to page 7"); // Create local hyperlink instance Aspose.Pdf.LocalHyperlink link = new Aspose.Pdf.LocalHyperlink(); // Set target page for link instance link.TargetPageNumber = 7; // Set TextFragment hyperlink text.Hyperlink = link; // Add text to paragraphs collection of Page page.Paragraphs.Add(text); // Create new TextFragment instance text = new TextFragment("link page number test to page 1"); // TextFragment should be added over new page text.IsInNewPage = true; // Create another local hyperlink instance link = new LocalHyperlink(); // Set Target page for second hyperlink link.TargetPageNumber = 1; // Set link for second TextFragment text.Hyperlink = link; // Add text to paragraphs collection of page object page.Paragraphs.Add(text); dataDir = dataDir + "CreateLocalHyperlink_out.pdf"; // Save updated document doc.Save(dataDir);
鏈接在PDF文件中表示為注釋,可以添加,更新或刪除它們。用于.NET的Aspose.PDF還支持獲取PDF文件中超鏈接的目標(URL)。獲取鏈接的URL,需要以下步驟:
以下代碼段顯示了如何從PDF文件獲取超鏈接目標(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的內容。請參見以下代碼段。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions(); // Load the PDF file Document document = new Document(dataDir + "input.pdf"); // Iterate through each page of PDF foreach (Page page in document.Pages) { // Show link annotation ShowLinkAnnotations(page); }還想要更多嗎?您可以點擊閱讀【2019 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問或需求,請隨時加入Aspose技術交流群(642018183),我們很高興為您提供查詢和咨詢。
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn