using System.Collections.Generic; using System.Linq; using System.Xml.Linq; using System.IO.Packaging; namespace Novacode { /// /// Represents a Picture in this document, a Picture is a customized view of an Image. /// public class Picture: DocXElement { private const int EmusInPixel = 9525; internal Dictionary picture_rels; internal Image img; private string id; private string name; private string descr; private int cx, cy; //private string fileName; private uint rotation; private bool hFlip, vFlip; private object pictureShape; private XElement xfrm; private XElement prstGeom; /// /// Remove this Picture from this document. /// public void Remove() { Xml.Remove(); } /// /// Wraps an XElement as an Image /// /// The XElement i to wrap internal Picture(DocX document, XElement i, Image img):base(document, i) { picture_rels = new Dictionary(); this.img = img; this.id = ( from e in Xml.Descendants() where e.Name.LocalName.Equals("blip") select e.Attribute(XName.Get("embed", "http://schemas.openxmlformats.org/officeDocument/2006/relationships")).Value ).Single(); this.name = ( from e in Xml.Descendants() let a = e.Attribute(XName.Get("name")) where (a != null) select a.Value ).First(); this.descr = ( from e in Xml.Descendants() let a = e.Attribute(XName.Get("descr")) where (a != null) select a.Value ).FirstOrDefault(); this.cx = ( from e in Xml.Descendants() let a = e.Attribute(XName.Get("cx")) where (a != null) select int.Parse(a.Value) ).First(); this.cy = ( from e in Xml.Descendants() let a = e.Attribute(XName.Get("cy")) where (a != null) select int.Parse(a.Value) ).First(); this.xfrm = ( from d in Xml.Descendants() where d.Name.LocalName.Equals("xfrm") select d ).Single(); this.prstGeom = ( from d in Xml.Descendants() where d.Name.LocalName.Equals("prstGeom") select d ).Single(); this.rotation = xfrm.Attribute(XName.Get("rot")) == null ? 0 : uint.Parse(xfrm.Attribute(XName.Get("rot")).Value); } private void SetPictureShape(object shape) { this.pictureShape = shape; XAttribute prst = prstGeom.Attribute(XName.Get("prst")); if (prst == null) prstGeom.Add(new XAttribute(XName.Get("prst"), "rectangle")); prstGeom.Attribute(XName.Get("prst")).Value = shape.ToString(); } /// /// Set the shape of this Picture to one in the BasicShapes enumeration. /// /// A shape from the BasicShapes enumeration. public void SetPictureShape(BasicShapes shape) { SetPictureShape((object)shape); } /// /// Set the shape of this Picture to one in the RectangleShapes enumeration. /// /// A shape from the RectangleShapes enumeration. public void SetPictureShape(RectangleShapes shape) { SetPictureShape((object)shape); } /// /// Set the shape of this Picture to one in the BlockArrowShapes enumeration. /// /// A shape from the BlockArrowShapes enumeration. public void SetPictureShape(BlockArrowShapes shape) { SetPictureShape((object)shape); } /// /// Set the shape of this Picture to one in the EquationShapes enumeration. /// /// A shape from the EquationShapes enumeration. public void SetPictureShape(EquationShapes shape) { SetPictureShape((object)shape); } /// /// Set the shape of this Picture to one in the FlowchartShapes enumeration. /// /// A shape from the FlowchartShapes enumeration. public void SetPictureShape(FlowchartShapes shape) { SetPictureShape((object)shape); } /// /// Set the shape of this Picture to one in the StarAndBannerShapes enumeration. /// /// A shape from the StarAndBannerShapes enumeration. public void SetPictureShape(StarAndBannerShapes shape) { SetPictureShape((object)shape); } /// /// Set the shape of this Picture to one in the CalloutShapes enumeration. /// /// A shape from the CalloutShapes enumeration. public void SetPictureShape(CalloutShapes shape) { SetPictureShape((object)shape); } /// /// A unique id that identifies an Image embedded in this document. /// public string Id { get { return id; } } /// /// Flip this Picture Horizontally. /// public bool FlipHorizontal { get { return hFlip; } set { hFlip = value; XAttribute flipH = xfrm.Attribute(XName.Get("flipH")); if (flipH == null) xfrm.Add(new XAttribute(XName.Get("flipH"), "0")); xfrm.Attribute(XName.Get("flipH")).Value = hFlip ? "1" : "0"; } } /// /// Flip this Picture Vertically. /// public bool FlipVertical { get { return vFlip; } set { vFlip = value; XAttribute flipV = xfrm.Attribute(XName.Get("flipV")); if (flipV == null) xfrm.Add(new XAttribute(XName.Get("flipV"), "0")); xfrm.Attribute(XName.Get("flipV")).Value = vFlip ? "1" : "0"; } } /// /// The rotation in degrees of this image, actual value = value % 360 /// public uint Rotation { get { return rotation / 60000; } set { rotation = (value % 360) * 60000; XElement xfrm = (from d in Xml.Descendants() where d.Name.LocalName.Equals("xfrm") select d).Single(); XAttribute rot = xfrm.Attribute(XName.Get("rot")); if(rot == null) xfrm.Add(new XAttribute(XName.Get("rot"), 0)); xfrm.Attribute(XName.Get("rot")).Value = rotation.ToString(); } } /// /// Gets or sets the name of this Image. /// public string Name { get { return name; } set { name = value; foreach (XAttribute a in Xml.Descendants().Attributes(XName.Get("name"))) a.Value = name; } } /// /// Gets or sets the description for this Image. /// public string Description { get { return descr; } set { descr = value; foreach (XAttribute a in Xml.Descendants().Attributes(XName.Get("descr"))) a.Value = descr; } } /// /// Returns the name of the image file for the picture. /// public string FileName { get { return img.FileName; } } /// /// Get or sets the Width of this Image. /// public int Width { get { return cx / EmusInPixel; } set { cx = value; foreach (XAttribute a in Xml.Descendants().Attributes(XName.Get("cx"))) a.Value = (cx * EmusInPixel).ToString(); } } /// /// Get or sets the height of this Image. /// public int Height { get { return cy / EmusInPixel; } set { cy = value; foreach (XAttribute a in Xml.Descendants().Attributes(XName.Get("cy"))) a.Value = (cy * EmusInPixel).ToString(); } } //public void Delete() //{ // // Remove xml // i.Remove(); // // Rebuild the image collection for this paragraph // // Requires that every Image have a link to its paragraph //} } }