Esempio n. 1
0
        /// <summary>
        /// Gets the document background image
        /// </summary>
        /// <returns>string path of the background image</returns>
        public string GetImage()
        {
            string      imageName;
            string      imageRelationshipId;
            OpenXmlPart imagePart;
            XElement    fillElement = BackgroundFillElement();

            if (fillElement != null)
            {
                imageRelationshipId = fillElement.Attribute(relationshipsns + "id").Value;
                imagePart           = parentDocument.Document.MainDocumentPart.GetPartById(imageRelationshipId);

                // Gets the image name (path stripped)
                string imagePath = imagePart.Uri.OriginalString;
                imageName = imagePath.Substring(imagePath.LastIndexOf('/') + 1);

                // Writes the image outside the package
                OpenXmlDocument.SavePartAs(imagePart, imageName);

                // Returns the path of the place where we wrote the image.
                return(Path.GetFullPath(imageName));
            }
            else
            {
                return(string.Empty);
            }
        }
Esempio n. 2
0
        public void ShouldGetTagNamesForCorrespondingToMarkupFromStream(string documentPath, int tagsNumber)
        {
            // Arrange
            var escapedBeginMarkup = @"\|\{";
            var escapedEndMarkup   = @"\}\|";
            var expectedTags       = new List <string>()
            {
                "Title",
                "Subtitle"
            };
            IEnumerable <string> actualTagsToReplace;

            // Act
            using (var stream = File.Open(documentPath, FileMode.Open))
            {
                using (var document = new OpenXmlDocument(stream))
                {
                    actualTagsToReplace = document.GetTagNames(escapedBeginMarkup, escapedEndMarkup);
                }
            }

            // Assert
            Assert.NotEmpty(actualTagsToReplace);
            Assert.Equal(tagsNumber, actualTagsToReplace.Count());
            Assert.All(expectedTags, expectedTag => Assert.Contains(expectedTag, actualTagsToReplace));
        }
Esempio n. 3
0
 public void Dispose()
 {
     OpenXmlDocument.Dispose();
     if (_streamOwner)
     {
         _stream.Dispose();
     }
 }
Esempio n. 4
0
        public void ShouldGetContentFromFilePath(string documentPath)
        {
            // Assign
            string actualDocumentContent;

            // Act
            using (var document = new OpenXmlDocument(documentPath))
            {
                actualDocumentContent = document.GetContent();
            }

            // Assert
            Assert.NotEmpty(actualDocumentContent);
        }
Esempio n. 5
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);
        }
Esempio n. 6
0
        public void ShouldGetAliasNamesFromFilePath(string documentPath, int aliasNumber)
        {
            // Arrange
            IEnumerable <string> actualAliasNames;

            // Act
            using (var document = new OpenXmlDocument(documentPath))
            {
                actualAliasNames = document.GetAliasNames();
            }

            // Assert
            Assert.NotNull(actualAliasNames);
            Assert.Equal(aliasNumber, actualAliasNames.Count());
        }
Esempio n. 7
0
        public void ShouldRemoveAllAliases(string documentPath)
        {
            // Arrange
            IEnumerable <string> actualAliasNames;

            // Act
            using (var document = new OpenXmlDocument(documentPath))
            {
                actualAliasNames = document.GetAliasNames();
                document.SearchAndRemoveAliases(actualAliasNames);
                actualAliasNames = document.GetAliasNames();
            }

            // Assert
            Assert.Empty(actualAliasNames);
        }
Esempio n. 8
0
        public void ShouldGetContentFromStream(string documentPath)
        {
            // Assign
            string actualDocumentContent;

            // Act
            using (var stream = File.Open(documentPath, FileMode.Open))
            {
                using (var document = new OpenXmlDocument(stream))
                {
                    actualDocumentContent = document.GetContent();
                }
            }

            // Assert
            Assert.NotEmpty(actualDocumentContent);
        }
Esempio n. 9
0
        public void ShouldSearchAndReplaceTagNames()
        {
            // Arrange
            var tags = new Dictionary <string, string>()
            {
            };

            var(documentPath, _, _) = GetDocumentsCharacteristic().First(d => d.Item2 == 0);
            string actualDocumentContent;

            // Act
            using (var document = new OpenXmlDocument(documentPath))
            {
                document.SearchAndReplaceTags(tags);
                actualDocumentContent = document.GetContent();
            }

            // Assert
            Assert.Contains("Lorem ipsum dolor sit amet", actualDocumentContent);
            Assert.Contains("Proin blandit porta mauris nec convallis", actualDocumentContent);
        }
Esempio n. 10
0
        public void ShouldSearchAndReplaceTagNamesFromFilePath(string documentPath)
        {
            // Arrange
            var tags = new Dictionary <string, string>()
            {
                { "Title", "Lorem ipsum dolor sit amet" },
                { "Subtitle", "Proin blandit porta mauris nec convallis" }
            };
            string actualDocumentContent;

            // Act
            using (var document = new OpenXmlDocument(documentPath))
            {
                document.SearchAndReplaceTags(tags);
                actualDocumentContent = document.GetContent();
            }

            // Assert
            Assert.Contains("Lorem ipsum dolor sit amet", actualDocumentContent);
            Assert.Contains("Proin blandit porta mauris nec convallis", actualDocumentContent);
        }
Esempio n. 11
0
        public void ShouldGetTagNamesFromFilePath(string documentPath, int tagsNumber)
        {
            // Arrange
            var expectedTags = new List <string>()
            {
                "Title",
                "Subtitle"
            };
            IEnumerable <string> actualTagsToReplace;

            // Act
            using (var document = new OpenXmlDocument(documentPath))
            {
                actualTagsToReplace = document.GetTagNames();
            }

            // Assert
            Assert.NotEmpty(actualTagsToReplace);
            Assert.Equal(tagsNumber, actualTagsToReplace.Count());
            Assert.All(expectedTags, expectedTag => Assert.Contains(expectedTag, actualTagsToReplace));
        }
Esempio n. 12
0
 public DocumentViewer(OpenXmlDocument document)
 {
     InitializeComponent();
     docArea.Document = document;
 }
Esempio n. 13
0
 public void Save()
 {
     OpenXmlDocument.Save();
 }
Esempio n. 14
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);
        }
Esempio n. 15
0
        public static void ShowDocument(OpenXmlDocument doc)
        {
            var window = new DocumentWindow(doc);

            window.Show();
        }
Esempio n. 16
0
        public static void ShowDocument(OpenXmlDocument document)
        {
            var docWindow = new DocumentViewer(document);

            docWindow.Show();
        }
Esempio n. 17
0
 public DocumentWindow(OpenXmlDocument doc)
 {
     InitializeComponent();
     docArea.Document = doc;
 }
Esempio n. 18
0
        private void btnVentes_Click(object sender, EventArgs e)
        {
            OpenXmlDocument xml = new OpenXmlDocument();

            xml.CreerFacture();
        }
Esempio n. 19
0
        /// <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);
        }