【发布时间】:2013-10-15 20:29:03
【问题描述】:
我有一个 10 页的文档。每个页面都有页眉和页脚。标题可能有两行或一行文本,每一行都有不同的样式。想要做的是循环浏览文档。阅读每页的页眉和页脚,将其放入DataTable,以便我稍后构建目录。任何想法,我都试过了,但它不能正常工作,它没有读取每个页面页脚并跳过页面(这似乎是乱序的,我想按页面顺序获取值,因为它似乎跳过了第一页直到最后一页迭代)。
我们将不胜感激。 JT
Using wordDoc As WordprocessingDocument = WordprocessingDocument.Open(combineDocName, True)
For Each Head As HeaderPart In wordDoc.MainDocumentPart.HeaderParts
For Each currentParagraph As DocumentFormat.OpenXml.Wordprocessing.Paragraph In Head.RootElement.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Paragraph)()
Dim p As ParagraphProperties = currentParagraph.Elements(Of ParagraphProperties)().First()
If p.Count > 0 Then
If (p.ParagraphStyleId IsNot Nothing) Then
If p.ParagraphStyleId.Val.ToString() = "HeaderBar" Then
For Each currentText As Text In Head.RootElement.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Text)()
If (String.IsNullOrEmpty(keepHeaderM)) Then
HeaderBarTxt = currentText.Text.Trim()
ElseIf keepHeaderM <> currentText.Text.Trim() Then
HeaderBarTxt = currentText.Text.Trim()
End If
Next
ElseIf p.ParagraphStyleId.Val.ToString() = "NavigationBar" Then
For Each currentText As Text In Head.RootElement.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Text)()
iCount = currentText.Text.Split(":").Length - 1
If (String.IsNullOrEmpty(keepHeaderM)) Then
HeaderTxt = currentText.Text.Trim()
ElseIf keepHeaderM <> currentText.Text.Trim() Then
HeaderTxt = currentText.Text.Trim()
End If
Next
End If
End If
End If
Next
Next
For Each foot As FooterPart In wordDoc.MainDocumentPart.FooterParts
For Each currentParagraph2 As DocumentFormat.OpenXml.Wordprocessing.Paragraph In foot.RootElement.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Paragraph)()
If currentParagraph2.Count > 0 Then
For Each currentText2 As Text In foot.RootElement.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Text)()
Dim strTemp As String = currentText2.Text
If strTemp.IndexOf("-") <> -1 Then
FooterTxt = currentText2.Text.Trim()
End If
Next
End If
Next
Next
end using
【问题讨论】:
-
究竟是什么不起作用?文件的格式是什么?
-
我发现当你循环时它并没有按顺序执行我需要的是如何按顺序循环浏览页面......我尝试了以下但我仍然首先得到第 3 页由于某种原因,第一页最后一页。我如何按顺序循环播放?