Esempio n. 1
0
            public override void OnPackageLoaded()
            {
                foreach (var part in _package.GetAllParts())
                {
                    if (_errors.TryGetValue(part.Uri.OriginalString, out var ids))
                    {
                        foreach (var id in ids)
                        {
                            part.DeleteReferenceRelationship(id);

                            if (part is WorksheetPart && part.RootElement is Worksheet ws)
                            {
                                foreach (var h in ws.Descendants <Hyperlink>())
                                {
                                    var parent = h.Parent;

                                    if (h.Id == id)
                                    {
                                        h.Remove();
                                    }

                                    if (!parent.HasChildren)
                                    {
                                        parent.Remove();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        private static void RemovePowerToolsAnnotations(this OpenXmlPackage package)
        {
            if (package == null)
            {
                throw new ArgumentNullException("package");
            }

            foreach (OpenXmlPart part in package.GetAllParts())
            {
                part.RemoveAnnotations <XDocument>();
                part.RemoveAnnotations <XmlNamespaceManager>();
            }
        }
        /// <summary>
        /// Ends a PowerTools Block by reloading the root elements of all package parts
        /// that were changed by the PowerTools. A part is deemed changed by the PowerTools
        /// if it has an annotation of type <see cref="XDocument" />.
        /// </summary>
        /// <param name="package">
        /// A <see cref="WordprocessingDocument" />, <see cref="SpreadsheetDocument" />,
        /// or <see cref="PresentationDocument" />.
        /// </param>
        public static void EndPowerToolsBlock(this OpenXmlPackage package)
        {
            if (package == null)
            {
                throw new ArgumentNullException("package");
            }

            foreach (OpenXmlPart part in package.GetAllParts())
            {
                if (part.Annotations <XDocument>().Any() && part.RootElement != null)
                {
                    part.RootElement.Reload();
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Asserts that two OpenXmlPackage instances have the same content.
        /// </summary>
        /// <param name="first">The first OpenXmlPackage</param>
        /// <param name="second">The second OpenXmlPackage</param>
        public static void Equal(OpenXmlPackage first, OpenXmlPackage second)
        {
            bool IsXml(OpenXmlPart part) => part.ContentType.EndsWith("xml", StringComparison.OrdinalIgnoreCase);

            byte[] GetBytes(Stream stream)
            {
                using (var ms = new MemoryStream())
                {
                    stream.CopyTo(ms);

                    return(ms.ToArray());
                }
            }

            Assert.Equal(first.GetAllParts().Count(), second.GetAllParts().Count());

            var combined = first.GetAllParts().Zip(second.GetAllParts(), (f, s) => (f, s));

            foreach (var item in combined)
            {
                Assert.Equal(item.f.GetType(), item.s.GetType());

                if (IsXml(item.f))
                {
                    Assert.Equal(item.f.GetXmlString(), item.s.GetXmlString());
                }
                else
                {
                    using (var firstStream = item.f.GetStream())
                        using (var secondStream = item.s.GetStream())
                        {
                            Assert.Equal(GetBytes(firstStream), GetBytes(secondStream));
                        }
                }
            }
        }
        /// <summary>
        /// Adds disposable feature.
        /// </summary>
        /// <param name="package">Package to add disposable feature to.</param>
        public static void AddDisposableFeature(this OpenXmlPackage package)
        {
            if (package.Features.Get <IDisposableFeature>() is not null)
            {
                return;
            }

            package.AddPartEventsFeature();
            package.AddPackageEventsFeature();
            package.Features.AddDisposableFeature();

            // Add feature to all parts already created
            foreach (var part in package.GetAllParts())
            {
                part.Features.AddDisposableFeature();
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Asserts that two OpenXmlPackage instances have the same content.
        /// </summary>
        /// <param name="first">The first OpenXmlPackage</param>
        /// <param name="second">The second OpenXmlPackage</param>
        protected static void AssertThatPackagesAreEqual(OpenXmlPackage first, OpenXmlPackage second)
        {
            var firstParts  = first.GetAllParts().ToList();
            var secondParts = second.GetAllParts().ToList();

            Assert.Equal(firstParts.Select(p => p.GetType()), secondParts.Select(p => p.GetType()));

            // Assert that the parts' root elements are equal.
            for (var i = 0; i < firstParts.Count(); i++)
            {
                if (firstParts[i].ContentType.EndsWith("xml"))
                {
                    var firstString  = GetXmlString(firstParts[i]);  // firstParts[i].GetRootElement().ToString();
                    var secondString = GetXmlString(secondParts[i]); // secondParts[i].GetRootElement().ToString();
                    Assert.Equal(firstString, secondString);
                }
                else
                {
                    byte[] firstByteArray;
                    using (var stream = firstParts[i].GetStream())
                        using (var binaryReader = new BinaryReader(stream))
                        {
                            var len = (int)binaryReader.BaseStream.Length;
                            firstByteArray = binaryReader.ReadBytes(len);
                        }

                    byte[] secondByteArray;
                    using (var stream = secondParts[i].GetStream())
                        using (var binaryReader = new BinaryReader(stream))
                        {
                            var len = (int)binaryReader.BaseStream.Length;
                            secondByteArray = binaryReader.ReadBytes(len);
                        }

                    Assert.Equal(firstByteArray, secondByteArray);
                }
            }
        }