翻譯|使用教程|編輯:李顯亮|2020-03-17 10:24:53.307|閱讀 387 次
概述:在本系列教程中,將為開發(fā)者帶來(lái)Aspose.PDF for .NET的一系列使用教程,例如進(jìn)行文檔間的轉(zhuǎn)換,如何標(biāo)記PDF文件,如何使用表單和圖表等等。本文將介紹創(chuàng)建,更新和提取鏈接。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Aspose.PDF for .NET是一種高PDF處理和解析API,用于在跨平臺(tái)應(yīng)用程序中執(zhí)行文檔管理和操作任務(wù)。API可以輕松用于生成、修改、轉(zhuǎn)換、渲染、保護(hù)和打印PDF文檔,而無(wú)需使用Adobe Acrobat。此外,API還提供PDF壓縮選項(xiàng),表格創(chuàng)建和操作,圖形和圖像功能,廣泛的超鏈接功能,印章和水印任務(wù),擴(kuò)展的安全控制和自定義字體處理。
在接下來(lái)的系列教程中,將為開發(fā)者帶來(lái)Aspose.PDF for .NET的一系列使用教程,例如進(jìn)行文檔間的轉(zhuǎn)換,如何標(biāo)記PDF文件,如何使用表單和圖表等等。本文將介紹創(chuàng)建,更新和提取鏈接。
>>Aspose.PDF for .NET更新至最新版v20.3,歡迎下載體驗(yàn)。
通過(guò)將到應(yīng)用程序的鏈接添加到文檔中,可以從文檔鏈接到應(yīng)用程序。例如,當(dāng)希望讀者在教程中的特定位置采取特定措施或創(chuàng)建功能豐富的文檔時(shí),此功能很有用。要?jiǎng)?chuàng)建一個(gè)應(yīng)用程序鏈接:
以下代碼段顯示了如何在PDF文件中創(chuàng)建到應(yīng)用程序的鏈接。
// 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文件的鏈接,以便可以將多個(gè)文檔鏈接在一起。要?jiǎng)?chuàng)建PDF文檔鏈接:
以下代碼段顯示了如何在PDF文件中創(chuàng)建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文件中添加鏈接。還有一個(gè)類似的類,用于從PDF文件內(nèi)部獲取現(xiàn)有鏈接。如果需要更新現(xiàn)有鏈接,請(qǐng)使用此選項(xiàng)。要更新現(xiàn)有鏈接:
將鏈接目標(biāo)設(shè)置為同一文檔中的頁(yè)面
以下代碼段顯示了如何更新PDF文件中的鏈接并將其目標(biāo)設(shè)置為文檔的第二頁(yè)。
// 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);
將鏈接目標(biāo)設(shè)置為網(wǎng)址
要更新超鏈接使其指向網(wǎng)址,請(qǐng)實(shí)例化該GoToURIAction對(duì)象并將其傳遞給LinkAnnotation的Action屬性。以下代碼段顯示了如何更新PDF文件中的鏈接并將其目標(biāo)設(shè)置為網(wǎng)址。
// 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);
將鏈接目標(biāo)設(shè)置為另一個(gè)PDF文件
以下代碼段顯示了如何更新PDF文件中的鏈接并將其目標(biāo)設(shè)置為另一個(gè)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文本顏色
鏈接注釋不包含文本。而是將文本放置在頁(yè)面內(nèi)容的注釋下。因此,要更改文本的顏色,請(qǐng)?zhí)鎿Q頁(yè)面文本的顏色,而不要嘗試更改批注的顏色。以下代碼段顯示了如何更新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文件中表示為注釋,因此要提取鏈接,請(qǐng)?zhí)崛∷蠰inkAnnotation對(duì)象。
以下代碼段顯示了如何從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);還想要更多嗎?您可以點(diǎn)擊閱讀【2019 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問(wèn)或需求,請(qǐng)隨時(shí)加入Aspose技術(shù)交流群(642018183),我們很高興為您提供查詢和咨詢。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn