I am trying to count the number of paragraphs or runs in a Word document using OpenXML. For a simple document this can be accomplished this way:
using (WordprocessingDocument document = WordprocessingDocument.Open(file, false))
{
var paragraphs = document.MainDocumentPart.Document.Body.
Descendants<DocumentFormat.OpenXml.Wordprocessing.Paragraph>();
int numberOfParagraphs = paragraphs.ToArray().Length;
var runs = document.MainDocumentPart.Document.Body.
Descendants<DocumentFormat.OpenXml.Wordprocessing.Run>();
int numberOfRuns = runs.ToArray().Length;
}
But I run into trouble with documents that have been created from merging small documents using AltChunks. The values returned are wrong and way too low. So I presume that I am missing all or some of the paragraphs or runs in the chunks.
Any ideas for how to count the paragraph or run elements in an AltChunk? I tried looking at descendants in each AltChunk, but I get 0's.
I have an idea that I'd be able to count if I convert each AltChunk to a document, but the WordprocessingDocument.Open method doesn't work when I pass it an AltChunk.
Any ideas would be appreciated.