Esempio n. 1
0
        private static void FillInEmptyFootnotesEndnotes(WordprocessingDocument wDoc)
        {
            XElement emptyFootnote = XElement.Parse(
                @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  <w:pPr>
    <w:pStyle w:val='FootnoteText'/>
  </w:pPr>
  <w:r>
    <w:rPr>
      <w:rStyle w:val='FootnoteReference'/>
    </w:rPr>
    <w:footnoteRef/>
  </w:r>
</w:p>");

            XElement emptyEndnote = XElement.Parse(
                @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  <w:pPr>
    <w:pStyle w:val='EndnoteText'/>
  </w:pPr>
  <w:r>
    <w:rPr>
      <w:rStyle w:val='EndnoteReference'/>
    </w:rPr>
    <w:endnoteRef/>
  </w:r>
</w:p>");

            FootnotesPart footnotePart = wDoc.MainDocumentPart.FootnotesPart;

            if (footnotePart != null)
            {
                XElement fnRoot = footnotePart.GetXDocument().Root ?? throw new ArgumentException();
                foreach (XElement fn in fnRoot.Elements(W.footnote))
                {
                    if (!fn.HasElements)
                    {
                        fn.Add(emptyFootnote);
                    }
                }

                footnotePart.PutXDocument();
            }

            EndnotesPart endnotePart = wDoc.MainDocumentPart.EndnotesPart;

            if (endnotePart != null)
            {
                XElement fnRoot = endnotePart.GetXDocument().Root ?? throw new ArgumentException();
                foreach (XElement fn in fnRoot.Elements(W.endnote))
                {
                    if (!fn.HasElements)
                    {
                        fn.Add(emptyEndnote);
                    }
                }

                endnotePart.PutXDocument();
            }
        }
Esempio n. 2
0
        private static void ChangeFootnoteEndnoteReferencesToUniqueRange(
            WordprocessingDocument wDoc,
            int startingIdForFootnotesEndnotes)
        {
            MainDocumentPart mainDocPart   = wDoc.MainDocumentPart;
            FootnotesPart    footnotesPart = wDoc.MainDocumentPart.FootnotesPart;
            EndnotesPart     endnotesPart  = wDoc.MainDocumentPart.EndnotesPart;

            XElement document =
                mainDocPart.GetXDocument().Root ?? throw new OpenXmlPowerToolsException("Invalid document.");

            XElement footnotes = footnotesPart?.GetXDocument().Root;
            XElement endnotes  = endnotesPart?.GetXDocument().Root;

            IEnumerable <XElement> references = document
                                                .Descendants()
                                                .Where(d => d.Name == W.footnoteReference || d.Name == W.endnoteReference);

            foreach (XElement r in references)
            {
                var    oldId = (string)r.Attribute(W.id);
                string newId = startingIdForFootnotesEndnotes.ToString();
                startingIdForFootnotesEndnotes++;
                r.SetAttributeValue(W.id, newId);
                if (r.Name == W.footnoteReference)
                {
                    XElement fn = footnotes?
                                  .Elements()
                                  .FirstOrDefault(e => (string)e.Attribute(W.id) == oldId);

                    if (fn == null)
                    {
                        throw new OpenXmlPowerToolsException("Invalid document");
                    }

                    fn.SetAttributeValue(W.id, newId);
                }
                else
                {
                    XElement en = endnotes?
                                  .Elements()
                                  .FirstOrDefault(e => (string)e.Attribute(W.id) == oldId);

                    if (en == null)
                    {
                        throw new OpenXmlPowerToolsException("Invalid document");
                    }

                    en.SetAttributeValue(W.id, newId);
                }
            }

            mainDocPart.PutXDocument();
            footnotesPart?.PutXDocument();
            endnotesPart?.PutXDocument();
        }
Esempio n. 3
0
        private static void RemoveExistingPowerToolsMarkup(WordprocessingDocument wDoc)
        {
            wDoc.MainDocumentPart
            .GetXDocument()
            .Root?
            .Descendants()
            .Attributes()
            .Where(a => a.Name.Namespace == PtOpenXml.pt)
            .Where(a => a.Name != PtOpenXml.Unid)
            .Remove();

            wDoc.MainDocumentPart.PutXDocument();

            FootnotesPart fnPart = wDoc.MainDocumentPart.FootnotesPart;

            if (fnPart != null)
            {
                XDocument fnXDoc = fnPart.GetXDocument();
                fnXDoc
                .Root?
                .Descendants()
                .Attributes()
                .Where(a => a.Name.Namespace == PtOpenXml.pt)
                .Where(a => a.Name != PtOpenXml.Unid)
                .Remove();

                fnPart.PutXDocument();
            }

            EndnotesPart enPart = wDoc.MainDocumentPart.EndnotesPart;

            if (enPart != null)
            {
                XDocument enXDoc = enPart.GetXDocument();
                enXDoc
                .Root?
                .Descendants()
                .Attributes()
                .Where(a => a.Name.Namespace == PtOpenXml.pt)
                .Where(a => a.Name != PtOpenXml.Unid)
                .Remove();

                enPart.PutXDocument();
            }
        }