zoukankan      html  css  js  c++  java
  • 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, 0, (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()); 
    }
  • 相关阅读:
    vue中sessionStorage的使用
    SQLserver将查询的字段中的数据 拼接成字符串用逗号隔开
    vue中引入百度地图
    已拦截跨源请求:同源策略禁止读取位于XXX的远程资源。(原因:CORS 头缺少 'Access-Control-Allow-Origin'
    前台调用微信接口成功还报Network Error
    SpringBoot获得application.properties中数据的几种方式
    设置session销毁时间
    Linux基本命令-ls
    Linux基础命令-cd
    Linux基础命令-mkdir
  • 原文地址:https://www.cnblogs.com/ac1985482/p/3387570.html
Copyright © 2011-2022 走看看