- Code: Select all
Spire.Doc.Document docSpire = new Spire.Doc.Document(@"sprie-shape.docx");
foreach (Spire.Doc.Section sec in docSpire.Sections)
{
Console.WriteLine($"sec:{sec.ChildObjects.Count}");
Console.WriteLine($"sec.Body:{sec.Body.ChildObjects.Count}");
foreach (DocumentObject bodyItem in sec.Body.ChildObjects)
{
if (bodyItem is Paragraph)
{
foreach (DocumentObject docObject in bodyItem.ChildObjects)
{
Console.WriteLine($"bodyItemGetType:{docObject.GetType()}");
Console.WriteLine($"bodyItemDocumentObjectType:{docObject.DocumentObjectType}");
//switch (docObject.DocumentObjectType)
//{
// case DocumentObjectType.Shape:
// ShapeObject shape = docObject as ShapeObject;
// break;
// case DocumentObjectType.Picture:
// DocPicture picture = docObject as DocPicture;
// break;
// default:
// break;
//}
}
}
if (bodyItem is Table)
{
//获取表格
Table table = bodyItem as Table;
for (int i = 0; i < table.Rows.Count; i++)
{
//遍历表格行
TableRow row = table.Rows[i];
for (int j = 0; j < row.Cells.Count; j++)
{
//遍历行中单元格
TableCell cell = row.Cells[j];
foreach (DocumentObject docObject in cell.ChildObjects)
{
//遍历单元格中所有子对象 找到段落
if (docObject is Paragraph)
{
//遍历段落中的子对象 找到图片
foreach (DocumentObject paraItem in docObject.ChildObjects)
{
Console.WriteLine($"paraItemGetType:{paraItem.GetType()}");
Console.WriteLine($"paraItemDocumentObjectType:{paraItem.DocumentObjectType}");
//if (paraItem is DocPicture)
//{
// //打印目标信息
// DocPicture picture = paraItem as DocPicture;
// Console.WriteLine(picture.AlternativeText);
//}
//if (paraItem is ShapeObject)
//{
// //打印目标信息
// DocPicture picture = paraItem as DocPicture;
// Console.WriteLine(picture.AlternativeText);
//}
}
}
}
}
}
}
}
}
但是读取出来的结果 如下。 没有找到 shape。 是有 textbox
- Code: Select all
sec:7
sec.Body:4
bodyItemGetType:Spire.Doc.Fields.TextBox
bodyItemDocumentObjectType:TextBox
paraItemGetType:Spire.Doc.BookmarkStart
paraItemDocumentObjectType:BookmarkStart
paraItemGetType:Spire.Doc.Fields.TextBox
paraItemDocumentObjectType:TextBox
paraItemGetType:Spire.Doc.BookmarkEnd
paraItemDocumentObjectType:BookmarkEnd
请问我如何才能遍历获取到呢?