/// <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());
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sets the document theme
        /// </summary>
        /// <param name="theme">Theme package</param>
        public void SetTheme(Package theme)
        {
            // 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 (parentDocument.Document.MainDocumentPart.ThemePart != null)
                    {
                        parentDocument.Document.MainDocumentPart.DeletePart(parentDocument.Document.MainDocumentPart.ThemePart);
                    }

                    // Creates a new theme part
                    ThemePart documentThemePart = parentDocument.Document.MainDocumentPart.AddNewPart <ThemePart>();
                    XDocument themeDocument     = parentDocument.GetXDocument(documentThemePart);
                    themeDocument.Add(newThemeDocument.Root);

                    var embeddedItems =
                        themeDocument
                        .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);
                        }
                    }

                    // Updates the theme part with actual contents of the theme XDocument
                    using (XmlWriter newThemeWriter = XmlWriter.Create(documentThemePart.GetStream(FileMode.Create, FileAccess.Write)))
                        themeDocument.WriteTo(newThemeWriter);
                }
            }
        }