C# – OpenXml Sdk – Copy Sections of docx into another docx

cdocxopenxmlopenxml-sdk

I am trying the following code. It takes a fileName (docx file with many sections) and I try to iterate through each section getting the section name. The problem is that I end up with unreadable docx files. It does not error, but I think I am doing something wrong with getting the elements in the section.

public void Split(string fileName) {
            using (WordprocessingDocument myDoc =
                WordprocessingDocument.Open(fileName, true)) {
                string curCliCode = "";
                MainDocumentPart mdp = myDoc.MainDocumentPart;

                foreach (var element in mdp.Document.Body.ChildElements) {
                    if (element.Descendants().OfType<SectionProperties>().Count() == 1) {
                        //get the name of the section from the footer
                        var footer = (FooterPart) mdp.GetPartById(
                                                      element.Descendants().OfType<SectionProperties>().First().OfType
                                                          <FooterReference>().First().
                                                          Id.Value);
                        foreach (Paragraph p in footer.Footer.ChildElements.OfType<Paragraph>()) {
                            if (p.InnerText != "") {
                                curCliCode = p.InnerText;
                            }
                        }
                        if (curCliCode != "") {
                            var forFile = new List<OpenXmlElement>();
                            var els = element.ElementsBefore();
                            if (els != null) {
                                foreach (var e in els) {
                                    if (e != null) {
                                        forFile.Add(e);
                                    }
                                }
                                for (int i = 0; i < els.Count(); i++) {
                                    els.ElementAt(i).Remove();
                                }
                            }
                            Create(curCliCode, forFile);
                        }
                    }
                }

            }
        }
        private void Create(string cliCode,IEnumerable<OpenXmlElement> docParts) {
            var parts = from e in docParts select e.Clone();
            const string template = @"\Test\toSplit\blank.docx";
            string destination = string.Format(@"\Test\{0}.docx", cliCode);
            File.Copy(template, destination,true);
            /* Create the package and main document part */
            using (WordprocessingDocument myDoc =
                WordprocessingDocument.Open(destination, true)) {
                MainDocumentPart mainPart = myDoc.MainDocumentPart;
                /* Create the contents */
                foreach(var part in parts) {
                    mainPart.Document.Body.Append((OpenXmlElement)part);
                }

                /* Save the results and close */
                mainPart.Document.Save();
                myDoc.Close();
            }
        }

Does anyone know what the problem could be (or how to properly copy a section from one document to another)?

Best Answer

I've done some work in this area, and what I have found invaluable is diffing a known good file with a prospective file; the error is usually fairly obvious.

What I would do is take a file that you know works, and copy all of the sections into the template. Theoretically, the two files should be identical. Run a diff on them the document.xml inside the docx file, and you'll see the difference.

BTW, I'm assuming that you know that the docx is actually a zip; change the extension to "zip", and you'll be able to get at the actual xml files which compose the format.

As far as diff tools, I use Beyond Compare from Scooter Software.

Related Topic