Query docx properties : Word « Windows « C# / CSharp Tutorial






using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.DocumentFormat.OpenXml;
using Microsoft.Office.DocumentFormat.OpenXml.Packaging;
using System.Xml.Linq;
using System.IO;

public class MainClass{

   public static void Main(string[] args){   
            
            WordprocessingDocument WD = WordprocessingDocument.Open("a.docx", false);
            OpenXmlPackage CustFileProps = WD.CustomFilePropertiesPart.OpenXmlPackage;
            IEnumerable<IdPartPair> Parts = from ThisPart in CustFileProps.Parts
               where ThisPart.OpenXmlPart.Uri.OriginalString == "/docProps/custom.xml"
               select ThisPart;

            TextReader PropStream = new StreamReader(Parts.ElementAt(0).OpenXmlPart.GetStream());
            XDocument PropDoc = XDocument.Load(PropStream);

            IEnumerable<XElement> Props =
               from ThisProp in PropDoc.Document.Element(
                  XName.Get("Properties", 
                     "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties")).Elements()
               select ThisProp;
               
            foreach (var ThisProp in Props){
               Console.WriteLine(ThisProp.Attribute(XName.Get("name", "")).Value +":\r\n" + ThisProp.Elements().ElementAt(0).Value+ "\r\n");
            }
            PropStream.Close();
            WD.Close();

   }
}








29.16.Word
29.16.1.Word._Application
29.16.2.Modify Document Properties
29.16.3.Query docx properties