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的更多相关文章

  1. 使用docx4j编程式地创建复杂的Word(.docx)文档

    原文链接:Create complex Word (.docx) documents programatically with docx4j 原文作者:jos.dirksen 发表日期:2012年2月 ...

  2. Python-docx 读取word.docx内容

    第一次写博客,也不知道要写点儿什么好,所以就把我在学习Python的过程中遇到的问题记录下来,以便之后查看,本人小白,写的不好,如有错误,还请大家批评指正! 中文编码问题总是让人头疼,想要用Pytho ...

  3. POI读写Word docx文件

    使用POI读写word docx文件 目录 1     读docx文件 1.1     通过XWPFWordExtractor读 1.2     通过XWPFDocument读 2     写docx ...

  4. 使用POI读写word docx文件

    目录 1     读docx文件 1.1     通过XWPFWordExtractor读 1.2     通过XWPFDocument读 2     写docx文件 2.1     直接通过XWPF ...

  5. POI读word docx 07 文件的两种方法

    POI在读写word docx文件时是通过xwpf模块来进行的,其核心是XWPFDocument.一个XWPFDocument代表一个docx文档,其可以用来读docx文档,也可以用来写docx文档. ...

  6. 解决 apache poi 转换 word(docx) 文件到 html 文件表格没边框的问题

    一.起因 这几天在做电子签章问题,要通过替换docx文件中的占位符生成包含业务数据的合同数据,再转换成html文件,转换成pdf文件.遇到的问题是:通过apache poi转换docx到html时,原 ...

  7. 初探JavaScript PDF blob转换为Word docx方法

    PDF转WORD为什么是历史难题 PDF 转Word 是一个非常非常普遍的需求,可谓人人忌危,为什么如此普遍的需求,却如此难行呢,还得看为什么会有这样的一个需求: PDF文档遵循iOS32000的规范 ...

  8. 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 ...

  9. 开源Office Word——DocX

    1.前言 请阅读前请看以下这位大神的文章 http://www.cnblogs.com/asxinyu/archive/2013/02/22/2921861.html 另附两个附件 1.DocX.DL ...

随机推荐

  1. 主调度器schedule

    中断处理完毕后,系统有三种执行流向:                                                                               1)直 ...

  2. python-类的方法与类的成员

    preface include: @classmethod @staticmethod @property 私有属性 类的成员 #!/usr/bin/env python class animal(o ...

  3. 2. 自动化运维系列之Cobbler给Openstack节点安装操作系统。

    preface 我们在一篇博文知道了如何搭建Cobbler,那么下面就通过Cobbler来安抓Openstack所有节点吧. 服务器配置信息如下: 主机名 IP 角色 Cobbler.node.com ...

  4. ios Develop mark

    App Distribution Guidehttps://developer.apple.com/library/ios/documaentation/IDEs/Conceptual/AppDist ...

  5. geoserver PostGIS的安装和使用

    PostGIS是一个非常流行并且开源的具有空间分析能力的关系型数据库,它作为PostgreSQL数据库的一个插件.PostgreSQL是一个功能非常强大并且开源的关系型数据库.目前项目使用的版本为Po ...

  6. application/x-www-form-urlencoded 的contentType,POST数据内容过大,导致tomcat的request取不到参数

    如题, 可通过设置tomcat的connector的参数 server.xml中的connector中加上属性 maxPostSize="20971520" maxPostSize ...

  7. 标准SQL资料整理

    最近感觉SQL忘记的很快,于是把SQL的基础知识整理了下. 使用的工具是MindManager 9. 资料来自W3C School:http://www.w3school.com.cn/sql/ind ...

  8. VMWare------启动虚拟机时出现“start booting fron CD... Error loading image:DFEAULT.EZB”提示

    提示详情: start booting fron CD... Error loading image:DFEAULT.EZB 解决方法: iso镜像文件有问题,需要下载正确的镜像文件 MSDN下载地址 ...

  9. OO模式-Singleton

    讨论一: 既然仅仅有一个类?为什么非要用一个模式来定义?难道就不能用程序猿之间的约定又或者使用伟大的设计模式来完毕? 1)先来说说全局变量的优点,当定义一个全局变量时,不论什么一个函数或者一行代码都能 ...

  10. flexbox常用布局上下固定,中间滚动

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...