【问题标题】:Stream editing OpenXml powerpoint presentation slides流式编辑 OpenXml PowerPoint 演示文稿幻灯片
【发布时间】:2018-04-11 04:25:48
【问题描述】:

我正在尝试使用 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 流,它们已更改。

只是这永远不会保存回处置(最终使用)中的文件,并且不会引发错误异常。

有其他人经历过吗?

【问题讨论】:

    标签: vb.net powerpoint openxml streamreader streamwriter


    【解决方案1】:

    在搞砸了大约一周之后,我找到了答案。它是从集合中引用幻灯片部分,而不是通过关系 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
    

    【讨论】:

      猜你喜欢
      • 2013-12-14
      • 2023-03-29
      • 2014-10-19
      • 1970-01-01
      • 2013-06-07
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      相关资源
      最近更新 更多