Я хочу прочитать pdf с тегами, пройти по дереву структуры и извлечь текст для каждого элемента, окончательный результат будет примерно таким:
- document
- div
- H1
- "The title of the document"
- P
- "The contents of the paragraph"
Я могу пройти по дереву, используя этот код: 1004 *
if (doc.IsTagged())
{
var root = doc.GetStructTreeRoot();
var stack = new Stack<iText.Kernel.Pdf.Tagging.IStructureNode>();
var stack2 = new Stack<iText.Kernel.Pdf.Tagging.IStructureNode>();
stack.Push(root);
while (stack.Count > 0)
{
var currentNode = stack.Pop();
stack2.Push(currentNode);
var kids = currentNode.GetKids();
if (kids != null)
{
foreach (var kid in kids)
{
stack.Push(kid);
}
}
}
while (stack2.Count > 0)
{
var currentNode = stack2.Pop();
var role = currentNode.GetRole()?.ToString();
if (currentNode is iText.Kernel.Pdf.Tagging.PdfMcrDictionary mcr) {
// this is where I want to extract the text from the structured node
}
}
}
Я не уверен, как получить фактический текст, который будет go внутри узла структуры, например, содержимое H1, P и других тегов.