-
Notifications
You must be signed in to change notification settings - Fork 0
/
docx2txt.cs
42 lines (40 loc) · 1.28 KB
/
docx2txt.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.IO;
using System.IO.Compression;
using System.Diagnostics;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Text;
using System.Collections.Generic;
namespace Docx2txt
{
class Program
{
static void PrintText(ZipArchive DocZip, string entry_name){
XmlNamespaceManager WpNs = new XmlNamespaceManager(new NameTable());
WpNs.AddNamespace("w",
"http://schemas.openxmlformats.org/wordprocessingml/2006/main");
ZipArchiveEntry wpxml_part = DocZip.GetEntry(entry_name);
if(wpxml_part==null)return;
Stream xml_stream = wpxml_part.Open();
IEnumerable<XElement> ParagraphElems = XDocument.Load(xml_stream).XPathSelectElements("//w:p", WpNs);
foreach(XElement pnode in ParagraphElems){
Console.WriteLine(pnode.Value);
}
xml_stream.Dispose();
}
static void Main(string[] args)
{
FileStream DocFile = File.Open(args[0],FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
ZipArchive DocZip = new ZipArchive(DocFile, ZipArchiveMode.Read);
PrintText(DocZip, "word/comments.xml");
PrintText(DocZip, "word/document.xml");
PrintText(DocZip, "word/footnotes.xml");
PrintText(DocZip, "word/endnotes.xml");
DocZip.Dispose();
DocFile.Dispose();
}
}
}