Пример #1
0
        /// <summary>
        /// Sets the document background color
        /// </summary>
        /// <param name="colorValue">String representation of the hexadecimal RGB color</param>
        public static OpenXmlPowerToolsDocument SetColor(WmlDocument doc, string colorValue)
        {
            using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc))
            {
                using (WordprocessingDocument document = streamDoc.GetWordprocessingDocument())
                {
                    XDocument mainDocument = document.MainDocumentPart.GetXDocument();

                    // If the background element already exists, deletes it
                    XElement backgroundElement = BackgroundElement(document);
                    if (backgroundElement != null)
                    {
                        backgroundElement.Remove();
                    }

                    mainDocument.Root.AddFirst(
                        new XElement(ns + "background",
                                     new XAttribute(ns + "color", colorValue)
                                     )
                        );

                    // Enables background displaying by adding "displayBackgroundShape" tag
                    if (SettingAccessor.DisplayBackgroundShapeElements(document) == null)
                    {
                        SettingAccessor.AddBackgroundShapeElement(document);
                    }

                    document.MainDocumentPart.PutXDocument();
                }
                return(streamDoc.GetModifiedDocument());
            }
        }
Пример #2
0
        /// <summary>
        /// Sets the document background image
        /// </summary>
        /// <param name="imagePath">Path of the background image</param>
        public static OpenXmlPowerToolsDocument SetImage(WmlDocument doc, string imagePath)
        {
            using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc))
            {
                using (WordprocessingDocument document = streamDoc.GetWordprocessingDocument())
                {
                    XDocument mainDocument = document.MainDocumentPart.GetXDocument();

                    // Adds the image to the package
                    ImagePart imagePart   = document.MainDocumentPart.AddImagePart(ImagePartType.Bmp);
                    Stream    imageStream = new StreamReader(imagePath).BaseStream;
                    byte[]    imageBytes  = new byte[imageStream.Length];
                    imageStream.Read(imageBytes, 0, imageBytes.Length);
                    imagePart.GetStream().Write(imageBytes, 0, imageBytes.Length);

                    // Creates a "background" element relating the image and the document

                    // If the background element already exists, deletes it
                    XElement backgroundElement = BackgroundElement(document);
                    if (backgroundElement != null)
                    {
                        backgroundElement.Remove();
                    }

                    // Background element construction
                    mainDocument.Root.Add(
                        new XElement(ns + "background",
                                     new XAttribute(ns + "color", defaultBackgroundColor),
                                     new XElement(vmlns + "background",
                                                  new XAttribute(vmlns + "id", defaultVmlBackgroundImageId),
                                                  new XAttribute(officens + "bwmode", defaultBWMode),
                                                  new XAttribute(officens + "targetscreensize", defaultTargetScreenSize),
                                                  new XElement(vmlns + "fill",
                                                               new XAttribute(relationshipsns + "id", document.MainDocumentPart.GetIdOfPart(imagePart)),
                                                               new XAttribute("recolor", defaultImageRecolor),
                                                               new XAttribute("type", defaultImageType)
                                                               )
                                                  )
                                     )
                        );


                    // Enables background displaying by adding "displayBackgroundShape" tag
                    if (SettingAccessor.DisplayBackgroundShapeElements(document) == null)
                    {
                        SettingAccessor.AddBackgroundShapeElement(document);
                    }

                    document.MainDocumentPart.PutXDocument();
                }
                return(streamDoc.GetModifiedDocument());
            }
        }