翻譯|使用教程|編輯:李顯亮|2020-06-24 10:06:54.347|閱讀 127 次
概述:在本系列教程中,將為開發者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉換,如何標記PDF文件,如何使用表單和圖表等等。本文將介紹如何從標記的PDF中提取標記的內容。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.PDF for .NET是一種高PDF處理和解析API,用于在跨平臺應用程序中執行文檔管理和操作任務。API可以輕松用于生成、修改、轉換、渲染、保護和打印PDF文檔,而無需使用Adobe Acrobat。此外,API還提供PDF壓縮選項,表格創建和操作,圖形和圖像功能,廣泛的超鏈接功能,印章和水印任務,擴展的安全控制和自定義字體處理。
在接下來的系列教程中,將為開發者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉換,如何標記PDF文件,如何使用表單和圖表等等。本文將介紹如何從標記的PDF中提取標記的內容。
>>Aspose.PDF for .NET更新至最新版v20.6,歡迎下載體驗。
獲得標簽的PDF內容
為了獲取帶有標簽文本的PDF文檔的內容,Aspose.PDF提供了Document Class的TaggedContent屬性 。以下代碼段顯示了如何使用“標記文本”獲取PDF文檔的內容:
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments(); // Create Pdf Document Document document = new Document(); // Get Content for work with TaggedPdf ITaggedContent taggedContent = document.TaggedContent; // // Work with Tagged Pdf content // // Set Title and Language for Documnet taggedContent.SetTitle("Simple Tagged Pdf Document"); taggedContent.SetLanguage("en-US"); // Save Tagged Pdf Document document.Save(dataDir + "TaggedPDFContent.pdf");
獲取根結構
為了獲得Tagged PDF文檔的根結構,Aspose.PDF提供了ITaggedContent 接口的StructTreeRootElement和StructureElement屬性。以下代碼段顯示了如何獲取標記PDF文檔的根結構:
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments(); // Create Pdf Document Document document = new Document(); // Get Content for work with TaggedPdf ITaggedContent taggedContent = document.TaggedContent; // Set Title and Language for Documnet taggedContent.SetTitle("Tagged Pdf Document"); taggedContent.SetLanguage("en-US"); // Properties StructTreeRootElement and RootElement are used for access to // StructTreeRoot object of pdf document and to root structure element (Document structure element). StructTreeRootElement structTreeRootElement = taggedContent.StructTreeRootElement; StructureElement rootElement = taggedContent.RootElement;
訪問子元素
為了訪問帶有標簽的PDF文檔的子元素,Aspose.PDF提供了 ElementList 類。以下代碼段顯示了如何訪問帶標簽的PDF文檔的子元素:
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments(); // Open Pdf Document Document document = new Document(dataDir + "StructureElementsTree.pdf"); // Get Content for work with TaggedPdf ITaggedContent taggedContent = document.TaggedContent; // Access to root element(s) ElementList elementList = taggedContent.StructTreeRootElement.ChildElements; foreach (Element element in elementList) { if (element is StructureElement) { StructureElement structureElement = element as StructureElement; // Get properties string title = structureElement.Title; string language = structureElement.Language; string actualText = structureElement.ActualText; string expansionText = structureElement.ExpansionText; string alternativeText = structureElement.AlternativeText; } } // Access to children elements of first element in root element elementList = taggedContent.RootElement.ChildElements[1].ChildElements; foreach (Element element in elementList) { if (element is StructureElement) { StructureElement structureElement = element as StructureElement; // Set properties structureElement.Title = "title"; structureElement.Language = "fr-FR"; structureElement.ActualText = "actual text"; structureElement.ExpansionText = "exp"; structureElement.AlternativeText = "alt"; } } // Save Tagged Pdf Document document.Save(dataDir + "AccessChildrenElements.pdf");
標記現有PDF中的圖像
為了標記現有PDF文檔中的圖像,Aspose.PDF提供了StructureElement Class的FindElements()方法。您可以使用FigureElement類的AlternativeText屬性為圖形添加替代文本 。以下代碼段顯示了如何標記現有PDF文檔中的圖像:
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments(); string inFile = dataDir + "TH.pdf"; string outFile = dataDir + "TH_out.pdf"; string logFile = dataDir + "TH_out.xml"; // Open document Document document = new Document(inFile); // Gets tagged content and root structure element ITaggedContent taggedContent = document.TaggedContent; StructureElement rootElement = taggedContent.RootElement; // Set title for tagged pdf document taggedContent.SetTitle("Document with images"); foreach (FigureElement figureElement in rootElement.FindElements(true)) { // Set Alternative Text for Figure figureElement.AlternativeText = "Figure alternative text (technique 2)"; // Create and Set BBox Attribute StructureAttribute bboxAttribute = new StructureAttribute(AttributeKey.BBox); bboxAttribute.SetRectangleValue(new Rectangle(0.0, 0.0, 100.0, 100.0)); StructureAttributes figureLayoutAttributes = figureElement.Attributes.GetAttributes(AttributeOwnerStandard.Layout); figureLayoutAttributes.SetAttribute(bboxAttribute); } // Move Span Element into Paragraph (find wrong span and paragraph in first TD) TableElement tableElement = rootElement.FindElements(true)[0]; SpanElement spanElement = tableElement.FindElements(true)[0]; TableTDElement firstTdElement = tableElement.FindElements(true)[0]; ParagraphElement paragraph = firstTdElement.FindElements(true)[0]; // Move Span Element into Paragraph spanElement.ChangeParentElement(paragraph); // Save document document.Save(outFile); // Checking PDF/UA Compliance for out document document = new Document(outFile); bool isPdfUaCompliance = document.Validate(logFile, PdfFormat.PDF_UA_1); Console.WriteLine(String.Format("PDF/UA compliance: {0}", isPdfUaCompliance));
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn