public static OpenXmlPowerToolsDocument FromFileName(string fileName)
        {
            byte[] bytes = File.ReadAllBytes(fileName);
            Type   type;

            try
            {
                type = GetDocumentType(bytes);
            }
            catch (FileFormatException)
            {
                throw new PowerToolsDocumentException("Not an Open XML document.");
            }
            if (type == typeof(WordprocessingDocument))
            {
                return(new WmlDocument(fileName, bytes));
            }
            if (type == typeof(SpreadsheetDocument))
            {
                return(new SmlDocument(fileName, bytes));
            }
            if (type == typeof(PresentationDocument))
            {
                return(new PmlDocument(fileName, bytes));
            }
            if (type == typeof(Package))
            {
                OpenXmlPowerToolsDocument pkg = new OpenXmlPowerToolsDocument(bytes);
                pkg.FileName = fileName;
                return(pkg);
            }
            throw new PowerToolsDocumentException("Not an Open XML document.");
        }
        /// <summary>
        /// Sets the document theme
        /// </summary>
        /// <param name="theme">Theme package</param>
        public static OpenXmlPowerToolsDocument SetTheme(WmlDocument doc, OpenXmlPowerToolsDocument themeDoc)
        {
            using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc))
            {
                using (WordprocessingDocument document = streamDoc.GetWordprocessingDocument())
                {
                    using (OpenXmlMemoryStreamDocument themeStream = new OpenXmlMemoryStreamDocument(themeDoc))
                        using (Package theme = themeStream.GetPackage())
                        {
                            // Gets the theme manager part
                            PackageRelationship themeManagerRelationship =
                                theme.GetRelationshipsByType(mainDocumentRelationshipType).FirstOrDefault();
                            if (themeManagerRelationship != null)
                            {
                                PackagePart themeManagerPart = theme.GetPart(themeManagerRelationship.TargetUri);

                                // Gets the theme main part
                                PackageRelationship themeRelationship =
                                    themeManagerPart.GetRelationshipsByType(themeRelationshipType).FirstOrDefault();
                                if (themeRelationship != null)
                                {
                                    PackagePart themePart        = theme.GetPart(themeRelationship.TargetUri);
                                    XDocument   newThemeDocument = XDocument.Load(XmlReader.Create(themePart.GetStream(FileMode.Open, FileAccess.Read)));

                                    // Removes existing theme part from document
                                    if (document.MainDocumentPart.ThemePart != null)
                                    {
                                        document.MainDocumentPart.DeletePart(document.MainDocumentPart.ThemePart);
                                    }

                                    // Creates a new theme part
                                    ThemePart documentThemePart = document.MainDocumentPart.AddNewPart <ThemePart>();

                                    var embeddedItems =
                                        newThemeDocument
                                        .Descendants()
                                        .Attributes(relationshipns + "embed");
                                    foreach (PackageRelationship imageRelationship in themePart.GetRelationships())
                                    {
                                        // Adds an image part to the theme part and stores contents inside
                                        PackagePart imagePart    = theme.GetPart(imageRelationship.TargetUri);
                                        ImagePart   newImagePart =
                                            documentThemePart.AddImagePart(GetImagePartType(imagePart.ContentType));
                                        newImagePart.FeedData(imagePart.GetStream(FileMode.Open, FileAccess.Read));

                                        // Updates relationship references into the theme XDocument
                                        XAttribute relationshipAttribute = embeddedItems.FirstOrDefault(e => e.Value == imageRelationship.Id);
                                        if (relationshipAttribute != null)
                                        {
                                            relationshipAttribute.Value = documentThemePart.GetIdOfPart(newImagePart);
                                        }
                                    }
                                    documentThemePart.PutXDocument(newThemeDocument);
                                }
                            }
                        }
                }
                return(streamDoc.GetModifiedDocument());
            }
        }
Esempio n. 3
0
 public static IEnumerable <ValidationErrorInfo> ValidateXml(OpenXmlPowerToolsDocument document)
 {
     if (document is WmlDocument)
     {
         using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(document))
             using (WordprocessingDocument doc = streamDoc.GetWordprocessingDocument())
             {
                 OpenXmlValidator validator = new OpenXmlValidator();
                 return(validator.Validate(doc));
             }
     }
     else if (document is SmlDocument)
     {
         using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(document))
             using (SpreadsheetDocument doc = streamDoc.GetSpreadsheetDocument())
             {
                 OpenXmlValidator validator = new OpenXmlValidator();
                 return(validator.Validate(doc));
             }
     }
     else if (document is PmlDocument)
     {
         using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(document))
             using (PresentationDocument doc = streamDoc.GetPresentationDocument())
             {
                 OpenXmlValidator validator = new OpenXmlValidator();
                 return(validator.Validate(doc));
             }
     }
     throw new PowerToolsDocumentException("Not an Open XML document.");
 }
 /// <summary>
 ///  Tests a Digital Signature from a package
 /// </summary>
 /// <returns>Digital signatures list</returns>
 public static Collection<string> GetList(OpenXmlPowerToolsDocument doc)
 {
     using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc))
     {
         // Creates the PackageDigitalSignatureManager
         PackageDigitalSignatureManager digitalSignatureManager = new PackageDigitalSignatureManager(streamDoc.GetPackage());
         // Verifies the collection of certificates in the package
         Collection<string> digitalSignatureDescriptions = new Collection<string>();
         ReadOnlyCollection<PackageDigitalSignature> digitalSignatures = digitalSignatureManager.Signatures;
         if (digitalSignatures.Count > 0)
         {
             foreach (PackageDigitalSignature signature in digitalSignatures)
             {
                 if (PackageDigitalSignatureManager.VerifyCertificate(signature.Signer) != X509ChainStatusFlags.NoError)
                 {
                     digitalSignatureDescriptions.Add(string.Format(System.Globalization.CultureInfo.InvariantCulture, "Signature: {0} ({1})", signature.Signer.Subject, PackageDigitalSignatureManager.VerifyCertificate(signature.Signer)));
                 }
                 else
                     digitalSignatureDescriptions.Add("Signature: " + signature.Signer.Subject);
             }
         }
         else
         {
             digitalSignatureDescriptions.Add("No digital signatures found");
         }
         return digitalSignatureDescriptions;
     }
 }
 public static OpenXmlPowerToolsDocument Insert(OpenXmlPowerToolsDocument doc, IEnumerable<string> certificateList)
 {
     using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc))
     {
         using (Package package = streamDoc.GetPackage())
         {
             foreach (string digitalCertificate in certificateList)
             {
                 X509Certificate x509Certificate = X509Certificate2.CreateFromCertFile(digitalCertificate);
                 PackageDigitalSignatureManager digitalSigntaureManager = new PackageDigitalSignatureManager(package);
                 digitalSigntaureManager.CertificateOption = CertificateEmbeddingOption.InSignaturePart;
                 System.Collections.Generic.List<Uri> partsToSign = new System.Collections.Generic.List<Uri>();
                 //Adds each part to the list, except relationships parts.
                 foreach (PackagePart openPackagePart in package.GetParts())
                 {
                     if (!PackUriHelper.IsRelationshipPartUri(openPackagePart.Uri))
                         partsToSign.Add(openPackagePart.Uri);
                 }
                 List<PackageRelationshipSelector> relationshipSelectors = new List<PackageRelationshipSelector>();
                 //Creates one selector for each package-level relationship, based on id
                 foreach (PackageRelationship relationship in package.GetRelationships())
                 {
                     PackageRelationshipSelector relationshipSelector =
                         new PackageRelationshipSelector(relationship.SourceUri, PackageRelationshipSelectorType.Id, relationship.Id);
                     relationshipSelectors.Add(relationshipSelector);
                 }
                 digitalSigntaureManager.Sign(partsToSign, x509Certificate, relationshipSelectors);
             }
         }
         return streamDoc.GetModifiedDocument();
     }
 }
Esempio n. 6
0
 /// <summary>
 ///  Tests a Digital Signature from a package
 /// </summary>
 /// <returns>Digital signatures list</returns>
 public static Collection <string> GetList(OpenXmlPowerToolsDocument doc)
 {
     using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc))
     {
         // Creates the PackageDigitalSignatureManager
         PackageDigitalSignatureManager digitalSignatureManager = new PackageDigitalSignatureManager(streamDoc.GetPackage());
         // Verifies the collection of certificates in the package
         Collection <string> digitalSignatureDescriptions = new Collection <string>();
         ReadOnlyCollection <PackageDigitalSignature> digitalSignatures = digitalSignatureManager.Signatures;
         if (digitalSignatures.Count > 0)
         {
             foreach (PackageDigitalSignature signature in digitalSignatures)
             {
                 if (PackageDigitalSignatureManager.VerifyCertificate(signature.Signer) != X509ChainStatusFlags.NoError)
                 {
                     digitalSignatureDescriptions.Add(string.Format(System.Globalization.CultureInfo.InvariantCulture, "Signature: {0} ({1})", signature.Signer.Subject, PackageDigitalSignatureManager.VerifyCertificate(signature.Signer)));
                 }
                 else
                 {
                     digitalSignatureDescriptions.Add("Signature: " + signature.Signer.Subject);
                 }
             }
         }
         else
         {
             digitalSignatureDescriptions.Add("No digital signatures found");
         }
         return(digitalSignatureDescriptions);
     }
 }
Esempio n. 7
0
 public static OpenXmlPowerToolsDocument Insert(OpenXmlPowerToolsDocument doc, IEnumerable <string> certificateList)
 {
     using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc))
     {
         using (Package package = streamDoc.GetPackage())
         {
             foreach (string digitalCertificate in certificateList)
             {
                 X509Certificate x509Certificate = X509Certificate2.CreateFromCertFile(digitalCertificate);
                 PackageDigitalSignatureManager digitalSigntaureManager = new PackageDigitalSignatureManager(package);
                 digitalSigntaureManager.CertificateOption = CertificateEmbeddingOption.InSignaturePart;
                 System.Collections.Generic.List <Uri> partsToSign = new System.Collections.Generic.List <Uri>();
                 //Adds each part to the list, except relationships parts.
                 foreach (PackagePart openPackagePart in package.GetParts())
                 {
                     if (!PackUriHelper.IsRelationshipPartUri(openPackagePart.Uri))
                     {
                         partsToSign.Add(openPackagePart.Uri);
                     }
                 }
                 List <PackageRelationshipSelector> relationshipSelectors = new List <PackageRelationshipSelector>();
                 //Creates one selector for each package-level relationship, based on id
                 foreach (PackageRelationship relationship in package.GetRelationships())
                 {
                     PackageRelationshipSelector relationshipSelector =
                         new PackageRelationshipSelector(relationship.SourceUri, PackageRelationshipSelectorType.Id, relationship.Id);
                     relationshipSelectors.Add(relationshipSelector);
                 }
                 digitalSigntaureManager.Sign(partsToSign, x509Certificate, relationshipSelectors);
             }
         }
         return(streamDoc.GetModifiedDocument());
     }
 }
 public WmlDocument(OpenXmlPowerToolsDocument original, bool convertToTransitional)
     : base(original, convertToTransitional)
 {
     if (GetDocumentType() != typeof(WordprocessingDocument))
     {
         throw new PowerToolsDocumentException("Not a Wordprocessing document.");
     }
 }
 public SmlDocument(OpenXmlPowerToolsDocument original, bool convertToTransitional)
     : base(original, convertToTransitional)
 {
     if (GetDocumentType() != typeof(SpreadsheetDocument))
     {
         throw new PowerToolsDocumentException("Not a Spreadsheet document.");
     }
 }
 public PmlDocument(OpenXmlPowerToolsDocument original, bool convertToTransitional)
     : base(original, convertToTransitional)
 {
     if (GetDocumentType() != typeof(PresentationDocument))
     {
         throw new PowerToolsDocumentException("Not a Presentation document.");
     }
 }
 public OpenXmlPowerToolsDocument(OpenXmlPowerToolsDocument original, bool convertToTransitional)
 {
     if (convertToTransitional)
     {
         ConvertToTransitional(original.FileName, original.DocumentByteArray);
     }
     else
     {
         DocumentByteArray = new byte[original.DocumentByteArray.Length];
         Array.Copy(original.DocumentByteArray, DocumentByteArray, original.DocumentByteArray.Length);
         FileName = original.FileName;
     }
 }
Esempio n. 12
0
 /// <summary>
 /// RemoveAll
 /// </summary>
 public static OpenXmlPowerToolsDocument RemoveAll(OpenXmlPowerToolsDocument doc)
 {
     using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc))
     {
         using (Package package = streamDoc.GetPackage())
         {
             // Creates the PackageDigitalSignatureManager
             PackageDigitalSignatureManager digitalSignatureManager = new PackageDigitalSignatureManager(package);
             digitalSignatureManager.RemoveAllSignatures();
         }
         return(streamDoc.GetModifiedDocument());
     }
 }
Esempio n. 13
0
 public OpenXmlMemoryStreamDocument(OpenXmlPowerToolsDocument doc)
 {
     Document        = doc;
     DocMemoryStream = new MemoryStream();
     doc.WriteByteArray(DocMemoryStream);
     try
     {
         DocPackage = Package.Open(DocMemoryStream, FileMode.Open);
     }
     catch (Exception)
     {
         throw new PowerToolsDocumentException("Not an Open XML document.");
     }
 }
 public OpenXmlMemoryStreamDocument(OpenXmlPowerToolsDocument doc)
 {
     Document        = doc;
     DocMemoryStream = new MemoryStream();
     DocMemoryStream.Write(doc.DocumentByteArray, 0, doc.DocumentByteArray.Length);
     try
     {
         DocPackage = Package.Open(DocMemoryStream, FileMode.Open);
     }
     catch (Exception e)
     {
         throw new PowerToolsDocumentException(e.Message);
     }
 }
 public OpenXmlMemoryStreamDocument(OpenXmlPowerToolsDocument doc)
 {
     Document = doc;
     DocMemoryStream = new MemoryStream();
     doc.WriteByteArray(DocMemoryStream);
     try
     {
         DocPackage = Package.Open(DocMemoryStream, FileMode.Open);
     }
     catch (Exception)
     {
         throw new PowerToolsDocumentException("Not an Open XML document.");
     }
 }
        public static OpenXmlPowerToolsDocument FromDocument(OpenXmlPowerToolsDocument doc)
        {
            Type type = doc.GetDocumentType();

            if (type == typeof(WordprocessingDocument))
            {
                return(new WmlDocument(doc));
            }
            if (type == typeof(SpreadsheetDocument))
            {
                return(new SmlDocument(doc));
            }
            if (type == typeof(PresentationDocument))
            {
                return(new PmlDocument(doc));
            }
            return(null);    // This should not be possible from a valid OpenXmlPowerToolsDocument object
        }
Esempio n. 17
0
        public static void SaveImageToFile(WmlDocument doc, string fileName)
        {
            using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc))
                using (WordprocessingDocument document = streamDoc.GetWordprocessingDocument())
                {
                    XDocument mainDocument = document.MainDocumentPart.GetXDocument();
                    XElement  fillElement  = mainDocument.Descendants(W.background).Descendants(VML.fill).FirstOrDefault();
                    if (fillElement != null)
                    {
                        string      imageRelationshipId = fillElement.Attribute(R.id).Value;
                        OpenXmlPart imagePart           = document.MainDocumentPart.GetPartById(imageRelationshipId);

                        // Gets the image name (path stripped)
                        string imagePath = imagePart.Uri.OriginalString;

                        // Writes the image outside the package
                        OpenXmlPowerToolsDocument.SavePartAs(imagePart, fileName);
                    }
                }
        }
 public OpenXmlPowerToolsDocument(OpenXmlPowerToolsDocument original, bool convertToTransitional)
 {
     if (convertToTransitional)
     {
         ConvertToTransitional(original.FileName, original.DocumentByteArray);
     }
     else
     {
         DocumentByteArray = new byte[original.DocumentByteArray.Length];
         Array.Copy(original.DocumentByteArray, DocumentByteArray, original.DocumentByteArray.Length);
         FileName = original.FileName;
     }
 }
 /// <summary>
 /// RemoveAll
 /// </summary>
 public static OpenXmlPowerToolsDocument RemoveAll(OpenXmlPowerToolsDocument doc)
 {
     using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc))
     {
         using (Package package = streamDoc.GetPackage())
         {
             // Creates the PackageDigitalSignatureManager
             PackageDigitalSignatureManager digitalSignatureManager = new PackageDigitalSignatureManager(package);
             digitalSignatureManager.RemoveAllSignatures();
         }
         return streamDoc.GetModifiedDocument();
     }
 }
 public OpenXmlPowerToolsDocument(OpenXmlPowerToolsDocument original)
 {
     DocumentByteArray = new byte[original.DocumentByteArray.Length];
     Array.Copy(original.DocumentByteArray, DocumentByteArray, original.DocumentByteArray.Length);
     FileName = original.FileName;
 }
 public static OpenXmlPowerToolsDocument FromDocument(OpenXmlPowerToolsDocument doc)
 {
     Type type = doc.GetDocumentType();
     if (type == typeof(WordprocessingDocument))
         return new WmlDocument(doc);
     if (type == typeof(SpreadsheetDocument))
         return new SmlDocument(doc);
     if (type == typeof(PresentationDocument))
         return new PmlDocument(doc);
     return null;    // This should not be possible from a valid OpenXmlPowerToolsDocument object
 }
 public OpenXmlPowerToolsDocument(OpenXmlPowerToolsDocument original)
 {
     DocumentByteArray = new byte[original.DocumentByteArray.Length];
     Array.Copy(original.DocumentByteArray, DocumentByteArray, original.DocumentByteArray.Length);
     FileName = original.FileName;
 }
        /// <summary>
        /// Sets the document theme
        /// </summary>
        /// <param name="theme">Theme package</param>
        public static OpenXmlPowerToolsDocument SetTheme(WmlDocument doc, OpenXmlPowerToolsDocument themeDoc)
        {
            using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc))
            {
                using (WordprocessingDocument document = streamDoc.GetWordprocessingDocument())
                {
                    using (OpenXmlMemoryStreamDocument themeStream = new OpenXmlMemoryStreamDocument(themeDoc))
                    using (Package theme = themeStream.GetPackage())
                    {
                        // Gets the theme manager part
                        PackageRelationship themeManagerRelationship =
                            theme.GetRelationshipsByType(mainDocumentRelationshipType).FirstOrDefault();
                        if (themeManagerRelationship != null)
                        {
                            PackagePart themeManagerPart = theme.GetPart(themeManagerRelationship.TargetUri);

                            // Gets the theme main part
                            PackageRelationship themeRelationship =
                                themeManagerPart.GetRelationshipsByType(themeRelationshipType).FirstOrDefault();
                            if (themeRelationship != null)
                            {
                                PackagePart themePart = theme.GetPart(themeRelationship.TargetUri);
                                XDocument newThemeDocument = XDocument.Load(XmlReader.Create(themePart.GetStream(FileMode.Open, FileAccess.Read)));

                                // Removes existing theme part from document
                                if (document.MainDocumentPart.ThemePart != null)
                                    document.MainDocumentPart.DeletePart(document.MainDocumentPart.ThemePart);

                                // Creates a new theme part
                                ThemePart documentThemePart = document.MainDocumentPart.AddNewPart<ThemePart>();

                                var embeddedItems =
                                    newThemeDocument
                                    .Descendants()
                                    .Attributes(relationshipns + "embed");
                                foreach (PackageRelationship imageRelationship in themePart.GetRelationships())
                                {
                                    // Adds an image part to the theme part and stores contents inside
                                    PackagePart imagePart = theme.GetPart(imageRelationship.TargetUri);
                                    ImagePart newImagePart =
                                        documentThemePart.AddImagePart(GetImagePartType(imagePart.ContentType));
                                    newImagePart.FeedData(imagePart.GetStream(FileMode.Open, FileAccess.Read));

                                    // Updates relationship references into the theme XDocument
                                    XAttribute relationshipAttribute = embeddedItems.FirstOrDefault(e => e.Value == imageRelationship.Id);
                                    if (relationshipAttribute != null)
                                        relationshipAttribute.Value = documentThemePart.GetIdOfPart(newImagePart);
                                }
                                documentThemePart.PutXDocument(newThemeDocument);
                            }
                        }
                    }
                }
                return streamDoc.GetModifiedDocument();
            }
        }
 public static OpenXmlPowerToolsDocument FromFileName(string fileName)
 {
     byte[] bytes = File.ReadAllBytes(fileName);
     Type type;
     try
     {
         type = GetDocumentType(bytes);
     }
     catch (FileFormatException)
     {
         throw new PowerToolsDocumentException("Not an Open XML document.");
     }
     if (type == typeof(WordprocessingDocument))
         return new WmlDocument(fileName, bytes);
     if (type == typeof(SpreadsheetDocument))
         return new SmlDocument(fileName, bytes);
     if (type == typeof(PresentationDocument))
         return new PmlDocument(fileName, bytes);
     if (type == typeof(Package))
     {
         OpenXmlPowerToolsDocument pkg = new OpenXmlPowerToolsDocument(bytes);
         pkg.FileName = fileName;
         return pkg;
     }
     throw new PowerToolsDocumentException("Not an Open XML document.");
 }
Esempio n. 25
0
        public static OpenXmlPowerToolsDocument InsertXml(OpenXmlPowerToolsDocument doc, string[] partPaths, string insertionXpath, string content)
        {
            using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc))
            {
                using (Package package = streamDoc.GetPackage())
                {
                    foreach (string partPath in partPaths)
                    {
                        Uri         xmlPartUri;
                        XmlDocument xmlDocument;
                        PackagePart xmlPart = null;

                        // Searches for the given part
                        xmlPartUri = new Uri(partPath, UriKind.Relative);
                        //if (!document.Package.PartExists(xmlPartUri))
                        //throw new ArgumentException("Specified part does not exist.");

                        xmlPart = package.GetPart(xmlPartUri);
                        using (XmlReader xmlReader = XmlReader.Create(xmlPart.GetStream(FileMode.Open, FileAccess.Read)))
                        {
                            try
                            {
                                xmlDocument = new XmlDocument();
                                xmlDocument.Load(xmlReader);
                            }
                            catch (XmlException)
                            {
                                xmlDocument = new XmlDocument();
                            }
                        }

                        // Looks into the XmlDocument for nodes at the specified path
                        XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);
                        namespaceManager.AddNamespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
                        XmlNode insertionPoint =
                            xmlDocument.SelectSingleNode(insertionXpath, namespaceManager);
                        if (insertionPoint == null)
                        {
                            throw new ArgumentException("Insertion point does not exist.");
                        }

                        StringReader r = new StringReader("<w:node xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>" + content + "</w:node>");

                        XmlNode nodoid = xmlDocument.ReadNode(XmlReader.Create(r));
                        //doc.LoadXml("<w:node xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>" + xmlContent + "</w:node>");


                        //// Inserts new contents into the given part
                        XmlNode xmlNodeToInsert =
                            nodoid;// doc.FirstChild;
                        //    xmlDocument.CreateElement("w","node", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");


                        //xmlNodeToInsert. .InnerXml = xmlContent;
                        XmlNodeList nodes = xmlNodeToInsert.ChildNodes;
                        if (nodes.Count > 0)
                        {
                            for (int i = nodes.Count - 1; i >= 0; i--)
                            {
                                XmlNode node = nodes[i];
                                insertionPoint.ParentNode.InsertAfter(node, insertionPoint);
                            }
                        }

                        // Writes the XmlDocument back to the part
                        using (XmlWriter writer = XmlWriter.Create(xmlPart.GetStream(FileMode.Create, FileAccess.Write)))
                        {
                            xmlDocument.WriteTo(writer);
                        }
                    }
                }
                return(streamDoc.GetModifiedDocument());
            }
        }
 public WmlDocument(OpenXmlPowerToolsDocument original)
     : base(original)
 {
     if (GetDocumentType() != typeof(WordprocessingDocument))
         throw new PowerToolsDocumentException("Not a Wordprocessing document.");
 }
 public SmlDocument(OpenXmlPowerToolsDocument original)
     : base(original)
 {
     if (GetDocumentType() != typeof(SpreadsheetDocument))
         throw new PowerToolsDocumentException("Not a Spreadsheet document.");
 }
 public PmlDocument(OpenXmlPowerToolsDocument original)
     : base(original)
 {
     if (GetDocumentType() != typeof(PresentationDocument))
         throw new PowerToolsDocumentException("Not a Presentation document.");
 }
 public OpenXmlMemoryStreamDocument(OpenXmlPowerToolsDocument doc)
 {
     Document = doc;
     DocMemoryStream = new MemoryStream();
     DocMemoryStream.Write(doc.DocumentByteArray, 0, doc.DocumentByteArray.Length);
     try
     {
         DocPackage = Package.Open(DocMemoryStream, FileMode.Open);
     }
     catch (Exception e)
     {
         throw new PowerToolsDocumentException(e.Message);
     }
 }