Exemple #1
0
        /// <summary>
        /// Inserts Xml markup representing format attributes inside a specific paragraph or paragraph run
        /// </summary>
        /// <param name="document">Document to insert formatting Xml tags</param>
        /// <param name="xpathInsertionPoint">Paragraph or paragraph run to set format</param>
        /// <param name="content">Formatting tags</param>
        public void InsertFormat(PTWordprocessingDocument document, string xpathInsertionPoint, string content)
        {
            XDocument   xDocument       = parentDocument.GetXDocument(document.Document.MainDocumentPart);
            XmlDocument xmlMainDocument = OpenXmlDocument.LoadXmlDocumentFromXDocument(xDocument);

            XmlNamespaceManager namespaceManager = new XmlNamespaceManager(new NameTable());

            namespaceManager.AddNamespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");

            XmlNodeList insertionPoints = xmlMainDocument.SelectNodes(xpathInsertionPoint, namespaceManager);

            if (insertionPoints.Count == 0)
            {
                throw new Exception("The xpath query did not return a valid location.");
            }

            foreach (XmlNode insertionPoint in insertionPoints)
            {
                XmlNode propertiesElement = insertionPoint.SelectSingleNode(@"w:pPr|w:rPr", namespaceManager);
                if (insertionPoint.Name == "w:p")
                {
                    // Checks if the rPr or pPr element exists
                    if (propertiesElement == null)
                    {
                        propertiesElement = xmlMainDocument.CreateElement("w", "pPr", namespaceManager.LookupNamespace("w"));
                        insertionPoint.PrependChild(propertiesElement);
                    }
                }
                else if (insertionPoint.Name == "w:r")
                {
                    // Checks if the rPr or pPr element exists
                    if (propertiesElement == null)
                    {
                        propertiesElement = xmlMainDocument.CreateElement("w", "rPr", namespaceManager.LookupNamespace("w"));
                        insertionPoint.PrependChild(propertiesElement);
                    }
                }

                if (propertiesElement != null)
                {
                    propertiesElement.InnerXml += content;
                }
                else
                {
                    throw new Exception("Specified xpath query result is not a valid location to place a formatting markup");
                }
            }
            OpenXmlDocument.SaveXmlDocumentIntoXDocument(xmlMainDocument, xDocument);
        }
Exemple #2
0
        /// <summary>
        /// Insert a style into a given xmlpath inside the document part
        /// </summary>
        /// <param name="xpathInsertionPoint">place where we are going to put the style</param>
        /// <param name="styleValue">name of the style</param>
        /// <param name="stylesSource">XDocument containing styles</param>
        public void InsertStyle(string xpathInsertionPoint, string styleValue, XDocument stylesSource)
        {
            XDocument   xDocument       = parentDocument.GetXDocument(parentDocument.Document.MainDocumentPart);
            XmlDocument xmlMainDocument = OpenXmlDocument.LoadXmlDocumentFromXDocument(xDocument);

            //  create the style element to add in the document, based upon the style name.
            //  this is an example of an style element

            //  <w:pStyle w:val="Heading1" />

            //  so, in order to construct this, we have to know already if the style will be placed inside
            //  a run or inside a paragraph. to know this we have to verify against the xpath, and know if
            //  the query want to access a 'run' or a paragraph

            XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlMainDocument.NameTable);

            namespaceManager.AddNamespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");

            XmlNodeList insertionPoints = xmlMainDocument.SelectNodes(xpathInsertionPoint, namespaceManager);

            if (insertionPoints.Count == 0)
            {
                throw new Exception("The xpath query did not return a valid location.");
            }

            foreach (XmlNode insertionPoint in insertionPoints)
            {
                XmlElement xmlStyle = null;

                if (insertionPoint.LocalName == "r" || insertionPoint.LocalName == "p")
                {
                    XmlNode propertiesElement = insertionPoint.SelectSingleNode(@"w:pPr|w:rPr", namespaceManager);

                    //if (propertiesElement != null)
                    //{
                    if (insertionPoint.Name == "w:p")
                    {
                        xmlStyle = xmlMainDocument.CreateElement("w", "pStyle", namespaceManager.LookupNamespace("w"));

                        //  retrieve the suffix from the styleAccesor class
                        xmlStyle.SetAttribute("val", namespaceManager.LookupNamespace("w"), styleValue + newStyleNameSuffix);

                        //  check if the rPr or pPr element exist, if so, then add the style xml element
                        //  inside, if not, then add a new rPr or pPr element
                        if (propertiesElement != null)
                        {
                            //  check if there is already a style node and remove it
                            XmlNodeList xmlStyleList = propertiesElement.SelectNodes("w:pStyle", namespaceManager);
                            for (int i = 0; i < xmlStyleList.Count; i++)
                            {
                                propertiesElement.RemoveChild(xmlStyleList[i]);
                            }
                            propertiesElement.PrependChild(xmlStyle);
                        }
                        else
                        {
                            propertiesElement = xmlMainDocument.CreateElement("w", "pPr", namespaceManager.LookupNamespace("w"));
                            propertiesElement.PrependChild(xmlStyle);
                            insertionPoint.PrependChild(propertiesElement);
                        }
                    }

                    if (insertionPoint.Name == "w:r")
                    {
                        xmlStyle = xmlMainDocument.CreateElement("w", "rStyle", namespaceManager.LookupNamespace("w"));
                        xmlStyle.SetAttribute("val", namespaceManager.LookupNamespace("w"), styleValue + newStyleNameSuffix);
                        if (propertiesElement != null)
                        {
                            // check if there is already a style node and remove it
                            XmlNodeList xmlStyleList = propertiesElement.SelectNodes("w:rStyle", namespaceManager);
                            for (int i = 0; i < xmlStyleList.Count; i++)
                            {
                                propertiesElement.RemoveChild(xmlStyleList[i]);
                            }
                            propertiesElement.PrependChild(xmlStyle);
                        }
                        else
                        {
                            propertiesElement = xmlMainDocument.CreateElement("w", "rPr", namespaceManager.LookupNamespace("w"));
                            propertiesElement.PrependChild(xmlStyle);
                            insertionPoint.PrependChild(propertiesElement);
                        }
                    }
                    //}
                }
                else
                {
                    throw new Exception("The xpath query did not return a valid location.");
                }
            }

            OpenXmlDocument.SaveXmlDocumentIntoXDocument(xmlMainDocument, xDocument);

            //  the style has been added in the main document part, but now we have to add the
            //  style definition in the styles definitions part. the style definition need to be
            //  extracted from the given inputStyle file.

            //  Because a style can be linked with other styles and
            //  can also be based on other styles,  all the complete hierarchy of styles has
            //  to be added
            Collection <XElement> styleHierarchy = parentDocument.Style.GetStyleHierarchy(styleValue, stylesSource, newStyleNameSuffix);

            //  open the styles file in the document
            XDocument xmlStylesDefinitionDocument = parentDocument.Style.GetStylesDocument();

            XDocument xElem = new XDocument();

            xElem.Add(xmlStylesDefinitionDocument.Root);
            //insert the new style
            foreach (XElement xmlStyleDefinition in styleHierarchy)
            {
                xElem.Root.Add(xmlStyleDefinition);
            }
            xmlStylesDefinitionDocument.Root.ReplaceWith(xElem.Root);
        }
        /// <summary>
        /// Sets the style to a given location inside the document part
        /// </summary>
        /// <param name="xpathInsertionPoint">Document fragment to set style</param>
        /// <param name="styleValue">Name of style</param>
        /// <param name="stylesSourceFilePath">Styles library</param>
        public void InsertStyle(string xpathInsertionPoint, string styleValue, string stylesSourceFilePath)
        {
            XDocument   xDocument       = parentDocument.GetXDocument(parentDocument.Document.MainDocumentPart);
            XmlDocument xmlMainDocument = OpenXmlDocument.LoadXmlDocumentFromXDocument(xDocument);

            XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlMainDocument.NameTable);

            namespaceManager.AddNamespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");

            XmlNodeList insertionPoints = xmlMainDocument.SelectNodes(xpathInsertionPoint, namespaceManager);


            foreach (XmlNode insertionPoint in insertionPoints)
            {
                XmlElement xmlStyle = null;

                XmlNode propertiesElement = insertionPoint.SelectSingleNode(@"w:pPr|w:rPr", namespaceManager);
                if (insertionPoint.Name == "w:p")
                {
                    xmlStyle = xmlMainDocument.CreateElement("w", "pStyle", namespaceManager.LookupNamespace("w"));
                    xmlStyle.SetAttribute("val", namespaceManager.LookupNamespace("w"), styleValue + newStyleNameSuffix);

                    //  check if the rPr or pPr element exist, if so, then add the style xml element
                    //  inside, if not, then add a new rPr or pPr element
                    if (propertiesElement != null)
                    {
                        //  check if there is already a style node and remove it
                        XmlNodeList xmlStyleList = propertiesElement.SelectNodes("w:pStyle", namespaceManager);
                        for (int i = 0; i < xmlStyleList.Count; i++)
                        {
                            propertiesElement.RemoveChild(xmlStyleList[i]);
                        }
                        propertiesElement.PrependChild(xmlStyle);
                    }
                    else
                    {
                        propertiesElement = xmlMainDocument.CreateElement("w", "pPr", namespaceManager.LookupNamespace("w"));
                        propertiesElement.PrependChild(xmlStyle);
                        insertionPoint.PrependChild(propertiesElement);
                    }
                }

                if (insertionPoint.Name == "w:r")
                {
                    xmlStyle = xmlMainDocument.CreateElement("w", "rStyle", namespaceManager.LookupNamespace("w"));
                    xmlStyle.SetAttribute("val", namespaceManager.LookupNamespace("w"), styleValue + newStyleNameSuffix);
                    if (propertiesElement != null)
                    {
                        // check if there is already a style node and remove it
                        XmlNodeList xmlStyleList = propertiesElement.SelectNodes("w:rStyle", namespaceManager);
                        for (int i = 0; i < xmlStyleList.Count; i++)
                        {
                            propertiesElement.RemoveChild(xmlStyleList[i]);
                        }
                        propertiesElement.PrependChild(xmlStyle);
                    }
                    else
                    {
                        propertiesElement = xmlMainDocument.CreateElement("w", "rPr", namespaceManager.LookupNamespace("w"));
                        propertiesElement.PrependChild(xmlStyle);
                        insertionPoint.PrependChild(propertiesElement);
                    }
                }
            }

            OpenXmlDocument.SaveXmlDocumentIntoXDocument(xmlMainDocument, xDocument);

            //  Adds the style definition in the styles definitions part. The style definition need to be
            //  extracted from the given styles library file.
            Collection <XElement> styleHierarchy = GetStyleHierarchy(styleValue, stylesSourceFilePath);

            //  Opens the styles file in the document
            XDocument xmlStylesDefinitionDocument = parentDocument.GetXDocument(parentDocument.Document.MainDocumentPart.StyleDefinitionsPart);
            XDocument xElem = new XDocument();

            xElem.Add(xmlStylesDefinitionDocument.Root);

            //Inserts the new style
            foreach (XElement xmlStyleDefinition in styleHierarchy)
            {
                xElem.Root.Add(xmlStyleDefinition);
            }
            xmlStylesDefinitionDocument.Root.ReplaceWith(xElem.Root);
        }