PDF管理控件Aspose.PDF for .Net使用教程(三十九):從標(biāo)記的PDF中提取標(biāo)記的內(nèi)容
Aspose.PDF for .NET是一種高PDF處理和解析API,用于在跨平臺應(yīng)用程序中執(zhí)行文檔管理和操作任務(wù)。API可以輕松用于生成、修改、轉(zhuǎn)換、渲染、保護和打印PDF文檔,而無需使用Adobe Acrobat。此外,API還提供PDF壓縮選項,表格創(chuàng)建和操作,圖形和圖像功能,廣泛的超鏈接功能,印章和水印任務(wù),擴展的安全控制和自定義字體處理。
在接下來的系列教程中,將為開發(fā)者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉(zhuǎn)換,如何標(biāo)記PDF文件,如何使用表單和圖表等等。本文將介紹如何從標(biāo)記的PDF中提取標(biāo)記的內(nèi)容。
>>Aspose.PDF for .NET更新至最新版v20.6,歡迎下載體驗。
獲得標(biāo)簽的PDF內(nèi)容
為了獲取帶有標(biāo)簽文本的PDF文檔的內(nèi)容,Aspose.PDF提供了Document Class的TaggedContent屬性 。以下代碼段顯示了如何使用“標(biāo)記文本”獲取PDF文檔的內(nèi)容:
// 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");
獲取根結(jié)構(gòu)
為了獲得Tagged PDF文檔的根結(jié)構(gòu),Aspose.PDF提供了ITaggedContent 接口的StructTreeRootElement和StructureElement屬性。以下代碼段顯示了如何獲取標(biāo)記PDF文檔的根結(jié)構(gòu):
// 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;
訪問子元素
為了訪問帶有標(biāo)簽的PDF文檔的子元素,Aspose.PDF提供了 ElementList 類。以下代碼段顯示了如何訪問帶標(biāo)簽的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");
標(biāo)記現(xiàn)有PDF中的圖像
為了標(biāo)記現(xiàn)有PDF文檔中的圖像,Aspose.PDF提供了StructureElement Class的FindElements()方法。您可以使用FigureElement類的AlternativeText屬性為圖形添加替代文本 。以下代碼段顯示了如何標(biāo)記現(xiàn)有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));
還想要更多嗎?您可以點擊閱讀【2019 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問或需求,請隨時加入Aspose技術(shù)交流群(642018183),我們很高興為您提供查詢和咨詢。