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 ...
随机推荐
- Android Material Design控件学习(二)——NavigationView的学习和使用
前言 上次我们学习了TabLayout的用法,今天我们继续学习MaterialDesign(简称MD)控件--NavigationView. 正如其名,NavigationView,导航View.一般 ...
- Android ScrollView 和ListView 一起使用的问题汇总
1.ScrollView 嵌套 ListView ,touch事件的截获问题. 参考 http://www.cnblogs.com/lqminn/archive/2013/03/02/2940194 ...
- ASCII码与16进制的互相转换(表)
所谓的ASCII和16进制都只是概念上的东西,在计算机中通通是二进制 转换应该是输出的转换,同样是一个数,在计算机内存中表示是一样的,只是输出不一样ASCII是针对字符的编码,几乎是键盘上的字符的编码 ...
- 06-Linux RPM 命令参数使用详解
rpm 执行安装包二进制包(Binary)以及源代码包(Source)两种.二进制包可以直接安装在计算机中,而源代码包将会由 RPM自动编译.安装.源代码包经常以src.rpm作为后缀名. 常用命令组 ...
- 测试amqplib实例,报错 Error: connect ECONNREFUSED 127.0.0.1:5672
研究消息队列处理库amqplib(https://www.npmjs.com/package/amqplib)时,把实例直接拿过来运行的时候报错如下 不知道为什么会出这样的问题,后来跑到github上 ...
- DDMS调试信息
1.可以使用System.out.println() 2.android.util包下的Log类 该类可以将信息以日志的形式输出到LogCat中: import android.util.Log; S ...
- AddComponentRecursively
class AddComponentRecursively extends ScriptableWizard { var componentName : String = ""; ...
- [Python] Unofficial Windows Binaries for Python Extension Packages
1. Unofficial Windows Binaries for Python Extension Packages 非官方的Python第三方库,提供基于Windows的二进制扩展包,由加州大学 ...
- Elastic Search 5.4.3 java api 入门
首先介绍一点,es的版本从之前的2.x跳跃到5.x,很多插件要保持一致,不然会产生很多版本不兼容的问题. 首先看一个demo先熟悉一下, 具体代码在git服务器上: https://github.co ...
- 从经典问题来看 Copy 方法
经典面试题:为什么 NSString 类型成员变量的修饰属性用 copy 而不是 strong (或 retain ) ? 在初学 iOS 的时候,可能会被灌输这么一个常识,切记 NSString 的 ...