Beispiel #1
0
        #pragma warning restore CS1998

        public static FieldExtractResult ExtractFields(string templateFileName,
                                                       bool removeCustomProperties = true, IEnumerable <string> keepPropertyNames = null)
        {
            string      newTemplateFileName  = templateFileName + "obj.docx";
            string      outputFile           = templateFileName + "obj.json";
            WmlDocument templateDoc          = new WmlDocument(templateFileName); // just reads the template's bytes into memory (that's all), read-only
            WmlDocument preprocessedTemplate = null;

            byte[] byteArray        = templateDoc.DocumentByteArray;
            var    fieldAccumulator = new FieldAccumulator();

            using (MemoryStream mem = new MemoryStream())
            {
                mem.Write(byteArray, 0, byteArray.Length);                                      // copy template file (binary) into memory -- I guess so the template/file handle isn't held/locked?
                using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(mem, true)) // read & parse that byte array into OXML document (also in memory)
                {
                    // first, remove all the task panes / web extension parts from the template (if there are any)
                    wordDoc.DeleteParts <WebExTaskpanesPart>(wordDoc.GetPartsOfType <WebExTaskpanesPart>());
                    // next, extract all fields (and thus logic) from the template's content parts
                    ExtractAllTemplateFields(wordDoc, fieldAccumulator, removeCustomProperties, keepPropertyNames);
                }
                preprocessedTemplate = new WmlDocument(newTemplateFileName, mem.ToArray());
            }
            // save the output (even in the case of error, since error messages are in the file)
            preprocessedTemplate.Save();

            using (StreamWriter sw = File.CreateText(outputFile))
            {
                fieldAccumulator.JsonSerialize(sw);
                sw.Close();
            }

            return(new FieldExtractResult(newTemplateFileName, outputFile));
        }
 /// <summary>
 /// Creates a bindable version of the <see cref="WordprocessingDocument" />.
 /// </summary>
 /// <param name="document">The document.</param>
 /// <returns>The <see cref="WordprocessingDocument" />.</returns>
 /// <seealso cref="http://blogs.msdn.com/ericwhite/archive/2008/10/19/creating-data-bound-content-controls-using-the-open-xml-sdk-and-linq-to-xml.aspx"/>
 public static WordprocessingDocument ToBindable(this WordprocessingDocument document)
 {
     foreach (MainDocumentPart mainPart in document.GetPartsOfType <MainDocumentPart>())
     {
         mainPart.RemoveCustomXmlParts();
         Guid      id                  = Guid.NewGuid();
         XDocument customXml           = mainPart.CreateCustomXml();
         XDocument customXmlProperties = CreateCustomXmlProperties(id);
         mainPart.AddCustomXmlPart(customXml, customXmlProperties);
         XDocument partXDoc = mainPart.GetXDocument();
         AddDataBinding(partXDoc, id);
         mainPart.PutXDocument(partXDoc);
     }
     return(document);
 }