为有中文需求的客户提供多渠道中文技术支持.

Fri Apr 15, 2022 1:53 am

请查收附件内容。 我使用如下代码


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



请问我如何才能遍历获取到呢?

xjdataspire
 
Posts: 24
Joined: Mon Mar 28, 2022 2:06 am

Fri Apr 15, 2022 7:11 am

您好,

感谢您的咨询。
请注意你提供的文件中的shape的类型是TextBox,所以需要获取的是TextBox中的文本。请参考下面的代码来实现您的需求。
Code: Select all
 Spire.Doc.Document docSpire = new Spire.Doc.Document(@"sprie-shape.docx");
 foreach (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)
                 {
                     //类型为TextBox
                     case DocumentObjectType.TextBox:
                         TextBox textBox = (TextBox)docObject;
                         foreach (Paragraph paragraph in textBox.Body.Paragraphs)
                         {
                             string text = paragraph.Text;
                             Console.WriteLine(text);
                         }
                         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)
                                 {
                                     ShapeObject shape = (ShapeObject)paraItem;
                                     if (shape.ShapeType is ShapeType.TextBox)
                                     {
                                         TextBox textBox = (TextBox)shape;
                                         foreach (Paragraph paragraph in textBox.Body.Paragraphs)
                                         {
                                             string text = paragraph.Text;
                                             Console.WriteLine(text);
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Fri Apr 15, 2022 7:16 am

谢谢,帅哥。

xjdataspire
 
Posts: 24
Joined: Mon Mar 28, 2022 2:06 am

Fri Apr 15, 2022 7:30 am

您好,

不客气!
以后您在使用我们产品时,如果遇到任何其他问题,请随时联系我们。

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Return to 中文技术支持