流式编辑 OpenXml powerpoint 演示文稿幻灯片

Stream editing OpenXml powerpoint presentation slides

我正在尝试使用 OpenXml 和 Streamreader/Streamwriter 编辑 Powerpoint 幻灯片的 XML 流。

word文档,很简单:

Imports System.IO
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Presentation
Imports DocumentFormat.OpenXml.Wordprocessing

'  
'
'
' Open a word document
CurrentOpenDocument = WordprocessingDocument.Open(TheWordFileName, True)

' for a word document, this works

Using (CurrentOpenDocument)
    ' read the xml stream
    Dim sr As StreamReader = New StreamReader(CurrentOpenDocument.MainDocumentPart.GetStream)
    docText = sr.ReadToEnd
    ' do the substitutions here
    docText = DoSubstitutions(docText)

    ' write the modified xml stream
    Dim sw As StreamWriter = New StreamWriter(CurrentOpenDocument.MainDocumentPart.GetStream(FileMode.Create))
    Using (sw)
        sw.Write(docText)
    End Using

End Using

但是对于 Powerpoint(演示文稿),我发现插入的修改后的 XML 幻灯片流没有被保存:

Imports System.IO
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Presentation
Imports DocumentFormat.OpenXml.Wordprocessing

'                    
'
' Open a powerpoint presentation 
CurrentOpenPresentation = PresentationDocument.Open(ThePowerpointFileName, True)

' for a powerpoint presentation, this doesn't work
Using (CurrentOpenPresentation)

    ' Get the presentation part of the presentation document.
    Dim pPart As PresentationPart = CurrentOpenPresentation.PresentationPart

    ' Verify that the presentation part and presentation exist.
    If pPart IsNot Nothing AndAlso pPart.Presentation IsNot Nothing Then
        ' Get the Presentation object from the presentation part.
        Dim pres As Presentation = pPart.Presentation

        ' Verify that the slide ID list exists.
        If pres.SlideIdList IsNot Nothing Then
            ' Get the collection of slide IDs from the slide ID list.
            Dim slideIds = pres.SlideIdList.ChildElements

            ' loop through each slide
            For Each sID In slideIds
                Dim slidePartRelationshipId As String = (TryCast(sID, SlideId)).RelationshipId
                Dim TheslidePart As SlidePart = CType(pPart.GetPartById(slidePartRelationshipId), SlidePart)
                ' If the slide exists...
                If TheslidePart.Slide IsNot Nothing Then

                    Dim sr As StreamReader = New StreamReader(TheslidePart.GetStream)
                    Using (sr)
                        docText = sr.ReadToEnd
                    End Using
                    docText = DoSubstitutions(docText)

                    Dim sw As StreamWriter = New StreamWriter(TheslidePart.GetStream(FileMode.Create))
                    Using (sw)
                        sw.Write(docText)
                    End Using

                End If
            Next
    End If
End Using

我还尝试遍历内存中的幻灯片来检查 XML 流,并且它们已更改。

只是这永远不会保存回处理(结束使用)中的文件,也不会引发错误异常。

有没有其他人遇到过这种情况?

经过大约一个星期的折腾,我找到了答案。它是从集合中引用 slideparts 而不是通过关系 Id 引用,虽然我不知道为什么这有效并且最初的方法没有:

' This DOES work
Using (CurrentOpenPresentation)
    ' Get the presentation part of the presentation document.
    Dim pPart As PresentationPart = CurrentOpenPresentation.PresentationPart

    ' Verify that the presentation part and presentation exist.
    If pPart IsNot Nothing AndAlso pPart.Presentation IsNot Nothing Then

        ' reference each slide in turn and do the Substitution
        For Each s In pPart.SlideParts
            Dim sr As StreamReader = New StreamReader(s.GetStream)
            Using (sr)
                docText = sr.ReadToEnd
            End Using
            docText = DoSubstitutions(docText)

            Dim sw As StreamWriter = New StreamWriter(s.GetStream(FileMode.Create))
            Using (sw)
                sw.Write(docText)
            End Using
        Next
    End If
End Using