彩票走势图

logo Aspose.PDF for .NET开发者使用教程
文档彩票走势图>>Aspose.PDF for .NET开发者使用教程>>PDF管理控件Aspose.PDF for .Net使用教程(三十九):从标记的PDF中提取标记的内容

PDF管理控件Aspose.PDF for .Net使用教程(三十九):从标记的PDF中提取标记的内容


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));

还想要更多吗?您可以点击阅读
【2019 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(642018183),我们很高兴为您提供查询和咨询
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP