Open XML C# and Word docx documents
openxml sdk2.5 : http://download.microsoft.com/download/5/5/3/553C731E-9333-40FB-ADE3-E02DC9643B31/OpenXMLSDKV25.msi
openxml Tool v2.5: http://download.microsoft.com/download/5/5/3/553C731E-9333-40FB-ADE3-E02DC9643B31/OpenXMLSDKToolV25.msi
dome: RemoveComments
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Packaging; namespace AcceptRevisions
{
public static class LocalExtensions
{
public static XDocument GetXDocument(this OpenXmlPart part)
{
XDocument xdoc = part.Annotation<XDocument>();
if (xdoc != null)
return xdoc;
using (StreamReader sr = new StreamReader(part.GetStream()))
using (XmlReader xr = XmlReader.Create(sr))
xdoc = XDocument.Load(xr);
part.AddAnnotation(xdoc);
return xdoc;
}
} class Program
{
public static void RemoveComments(WordprocessingDocument document)
{
// remove w:commentRangeStart, w:commentRangeEnd, w:commentReference
XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
XDocument mainDocumentXDoc = document.MainDocumentPart.GetXDocument(); // pre-atomize the XName objects so that they are not atomized for every item in the collection
XName commentRangeStart = w + "commentRangeStart";
XName commentRangeEnd = w + "commentRangeEnd";
XName commentReference = w + "commentReference";
mainDocumentXDoc.Descendants()
.Where(x => x.Name == commentRangeStart || x.Name == commentRangeEnd || x.Name == commentReference)
.Remove(); // remove the comment part
document.MainDocumentPart.DeletePart(document.MainDocumentPart.WordprocessingCommentsPart); using (XmlWriter xw =
XmlWriter.Create(document.MainDocumentPart.GetStream(FileMode.Create, FileAccess.Write)))
mainDocumentXDoc.Save(xw);
} public static bool HasComments(WordprocessingDocument document)
{
XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
XDocument mainDocumentXDoc = document.MainDocumentPart.GetXDocument();
return mainDocumentXDoc.Descendants(w + "commentReference").Any();
} static void Main(string[] args)
{
using (WordprocessingDocument doc = WordprocessingDocument.Open("Test.docx", true))
{
Console.WriteLine(HasComments(doc));
RemoveComments(doc);
Console.WriteLine(HasComments(doc));
}
}
}
}
save Document:
byte[] byteArray = File.ReadAllBytes("c:\\data\\hello.docx");
using (MemoryStream stream = new MemoryStream())
{
stream.Write(byteArray, , (int)byteArray.Length);
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(stream, true))
{
// Do work here
}
// Save the file with the new name
File.WriteAllBytes("C:\\data\\newFileName.docx", stream.ToArray());
}
Open XML C# and Word docx documents的更多相关文章
- 使用docx4j编程式地创建复杂的Word(.docx)文档
原文链接:Create complex Word (.docx) documents programatically with docx4j 原文作者:jos.dirksen 发表日期:2012年2月 ...
- Python-docx 读取word.docx内容
第一次写博客,也不知道要写点儿什么好,所以就把我在学习Python的过程中遇到的问题记录下来,以便之后查看,本人小白,写的不好,如有错误,还请大家批评指正! 中文编码问题总是让人头疼,想要用Pytho ...
- POI读写Word docx文件
使用POI读写word docx文件 目录 1 读docx文件 1.1 通过XWPFWordExtractor读 1.2 通过XWPFDocument读 2 写docx ...
- 使用POI读写word docx文件
目录 1 读docx文件 1.1 通过XWPFWordExtractor读 1.2 通过XWPFDocument读 2 写docx文件 2.1 直接通过XWPF ...
- POI读word docx 07 文件的两种方法
POI在读写word docx文件时是通过xwpf模块来进行的,其核心是XWPFDocument.一个XWPFDocument代表一个docx文档,其可以用来读docx文档,也可以用来写docx文档. ...
- 解决 apache poi 转换 word(docx) 文件到 html 文件表格没边框的问题
一.起因 这几天在做电子签章问题,要通过替换docx文件中的占位符生成包含业务数据的合同数据,再转换成html文件,转换成pdf文件.遇到的问题是:通过apache poi转换docx到html时,原 ...
- 初探JavaScript PDF blob转换为Word docx方法
PDF转WORD为什么是历史难题 PDF 转Word 是一个非常非常普遍的需求,可谓人人忌危,为什么如此普遍的需求,却如此难行呢,还得看为什么会有这样的一个需求: PDF文档遵循iOS32000的规范 ...
- java解析excel2003和excel2007:The supplied data appears to be in the office 2007+XML Polonly supports OLE2 office documents
上传excel解析存到数据库时报: org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears ...
- 开源Office Word——DocX
1.前言 请阅读前请看以下这位大神的文章 http://www.cnblogs.com/asxinyu/archive/2013/02/22/2921861.html 另附两个附件 1.DocX.DL ...
随机推荐
- [译]Angular-ui 之 Url Routing
◄ 前一篇 (Multiple Named Views) 下一篇 (The Components) ► 在你的应用中多数的状态都是基于特定的url地址的.Url Routing机制绝不是在状态 ...
- python 在windows 中文显示
今天看到mechanize,在网上找例子实验,发现只要代码里出现中文,就会报错 SyntaxError: Non-ASCII character , but no encoding declared; ...
- mysql中使用show variables同时查询多个参数值?show variables的使用?
需求描述: 今天在查mysq关于连接数的问题,想要通过一个show variables命令同时查出来多个值.在此记录下. 操作过程: 1.通过show variables语句的like可以匹配多个值或 ...
- Tomcat------如何更改被IIS占用的80端口
1.打开cmd,运行'netstat -ano'发现80端口被pid=4的进程占用 2.打开任务管理器,发现pid=4的进程,其实是system进程,其对应的进程描述是NT kernel & ...
- SpringMVC由浅入深day01_10@RequestMapping_11controller方法的返回值
10 @RequestMapping 10.1 Url路径映射 @RequestMapping(value="/item")或@RequestMapping("/item ...
- tomcat 的 server.xml配置文件
tomcat的配置文件在其安装后生成的conf目录下,其中主配置文件便是conf下的server.xml文件. server.xml文件由server->service->engine-& ...
- Javascript生成GUID算法
var GUID = { date: new Date(), /* 生成GUID码 */ newGUID: function () { this.date = new Date(); var guid ...
- 【权限维持】window服务端常见后门技术
0x00 前言 未知攻焉知防,攻击者在获取服务器权限后,通常会用一些后门技术来维持服务器权限,服务器一旦被植入后门,攻击者如入无人之境.这里整理一些window服务端常见的后门技术,了解攻击者的常见后 ...
- Django 定义数据模型
如何定义数据模型: (1) 在 MVC 设计模式中,M 表示数据模型 ( Model ),负责业务对象与数据库的映射,我们可以通过应用的 models.py 来定义数据模型(2) Model 采用了 ...
- NSIS安装vcredist_64.exe
; ExecWait ‘vcredist_x86.exe’ # 一般的安装ExecWait ‘”vcredist_x86.exe” /q’ # silent install 静默安装; ExecWai ...