Извлеките текст из формы группы в pptx, используя Spire.Presentation для. NET - PullRequest
0 голосов
/ 23 апреля 2020

Я пытаюсь получить текст из презентации Power Point с помощью плагина Spire.Presentation. Я могу извлечь текст из простых текстовых полей с помощью следующего кода успешно. Но как только текстовые поля сгруппированы вместе, этот код возвращает пустые строки. Пожалуйста, помогите получить текст, когда фигуры сгруппированы. Не смог найти нигде решения.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim presentation As New Presentation("Drawing.pptx", FileFormat.Pptx2010)
    Dim sb As New StringBuilder()
    For Each slide As ISlide In presentation.Slides
        For Each shape As IShape In slide.Shapes
            If TypeOf shape Is IAutoShape Then
                For Each tp As TextParagraph In TryCast(shape, IAutoShape).TextFrame.Paragraphs
                    sb.Append(tp.Text + Environment.NewLine)
                Next
            End If

        Next
    Next
    File.WriteAllText("target.txt", sb.ToString())
    Process.Start("target.txt")

End Sub

1 Ответ

0 голосов
/ 30 апреля 2020

Вы можете использовать следующий код для извлечения текста из фигуры и формы группы. Код в c#, но я думаю, что вы можете перенести его на VB. net.

Presentation ppt = new Presentation(@"..\..\test document\3SlidesNoTransitions8762.pptx", FileFormat.Pptx2010);
        StringBuilder sb = new StringBuilder();
        foreach (ISlide slide in ppt.Slides)
        {
            foreach (IShape shape in slide.Shapes)
            {
                sb.AppendLine(getShapeText(shape));
            }
            File.WriteAllText("8672.txt", sb.ToString());
        }
    }
    public static string getShapeText(IShape shape)
    {
        StringBuilder sb = new StringBuilder();
        if (shape is IAutoShape)
        {
            IAutoShape ashape = shape as IAutoShape;
            if (ashape.TextFrame != null)
            {
                foreach (TextParagraph pg in ashape.TextFrame.Paragraphs)
                {
                    sb.AppendLine(pg.Text);
                }
            }
        }
        else if (shape is GroupShape)
        {
            GroupShape gs = shape as GroupShape;
            foreach (IShape s in gs.Shapes)
            {
                sb.AppendLine(getShapeText(s));
            }
        }
        return sb.ToString();
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...