| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504 |
- /*************************************************************************************
-
- DocX – DocX is the community edition of Xceed Words for .NET
-
- Copyright (C) 2009-2016 Xceed Software Inc.
-
- This program is provided to you under the terms of the Microsoft Public
- License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
-
- For more features and fast professional support,
- pick up Xceed Words for .NET at https://xceed.com/xceed-words-for-net/
-
- ***********************************************************************************/
-
- using System.Collections.Generic;
- using System.Linq;
- using System.Xml.Linq;
- using System.IO.Packaging;
-
- namespace Xceed.Words.NET
- {
- /// <summary>
- /// Represents a Picture in this document, a Picture is a customized view of an Image.
- /// </summary>
- public class Picture : DocXElement
- {
- #region Private Members
-
- private string _id;
- private string _name;
- private string _descr;
- private int _cx, _cy;
- private uint _rotation;
- private bool _hFlip, _vFlip;
- private object _pictureShape;
- private XElement _xfrm;
- private XElement _prstGeom;
-
- // Calculating Height & Width in Inches
- // https://startbigthinksmall.wordpress.com/2010/01/04/points-inches-and-emus-measuring-units-in-office-open-xml/
- // http://lcorneliussen.de/raw/dashboards/ooxml/
- private const int InchToEmuFactor = 914400;
- private const double EmuToInchFactor = 1d / InchToEmuFactor;
-
- #endregion
-
- #region Internal Members
-
- internal const int EmusInPixel = 9525; // Result of : 914400 EMUs per inch / 96 pixel per inch.
-
- internal Dictionary<PackagePart, PackageRelationship> _picture_rels;
-
- internal Image _img;
-
- #endregion
-
- #region Public Properties
-
- /// <summary>
- /// A unique id that identifies an Image embedded in this document.
- /// </summary>
- public string Id
- {
- get
- {
- return _id;
- }
- }
-
- /// <summary>
- /// Flip this Picture Horizontally.
- /// </summary>
- public bool FlipHorizontal
- {
- get
- {
- return _hFlip;
- }
-
- set
- {
- _hFlip = value;
-
- var 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";
- }
- }
-
- /// <summary>
- /// Flip this Picture Vertically.
- /// </summary>
- public bool FlipVertical
- {
- get
- {
- return _vFlip;
- }
-
- set
- {
- _vFlip = value;
-
- var 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";
- }
- }
-
- /// <summary>
- /// The rotation in degrees of this image, actual value = value % 360
- /// </summary>
- public uint Rotation
- {
- get
- {
- return _rotation / 60000;
- }
-
- set
- {
- _rotation = ( value % 360 ) * 60000;
- var xfrm =
- ( from d in Xml.Descendants()
- where d.Name.LocalName.Equals( "xfrm" )
- select d ).Single();
-
- var 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();
- }
- }
-
- /// <summary>
- /// Gets or sets the name of this Image.
- /// </summary>
- public string Name
- {
- get
- {
- return _name;
- }
-
- set
- {
- _name = value;
-
- foreach( XAttribute a in Xml.Descendants().Attributes( XName.Get( "name" ) ) )
- {
- a.Value = _name;
- }
- }
- }
-
- /// <summary>
- /// Gets or sets the description for this Image.
- /// </summary>
- public string Description
- {
- get
- {
- return _descr;
- }
-
- set
- {
- _descr = value;
-
- foreach( XAttribute a in Xml.Descendants().Attributes( XName.Get( "descr" ) ) )
- {
- a.Value = _descr;
- }
- }
- }
-
- ///<summary>
- /// Returns the name of the image file for the picture.
- ///</summary>
- public string FileName
- {
- get
- {
- return _img.FileName;
- }
- }
-
- /// <summary>
- /// Gets or sets the Width of this Image.
- /// </summary>
- public int Width
- {
- get
- {
- return _cx / EmusInPixel;
- }
-
- set
- {
- _cx = value * EmusInPixel;
-
- foreach( XAttribute a in Xml.Descendants().Attributes( XName.Get( "cx" ) ) )
- a.Value = _cx.ToString();
- }
- }
-
- /// <summary>
- /// Gets or sets the Width of this Image (in Inches)
- /// </summary>
- public double WidthInches
- {
- get
- {
- return Width * EmusInPixel * EmuToInchFactor;
- }
-
- set
- {
- Width = ( int )( value * InchToEmuFactor / EmusInPixel );
- }
- }
-
- /// <summary>
- /// Gets or sets the height of this Image.
- /// </summary>
- public int Height
- {
- get
- {
- return _cy / EmusInPixel;
- }
-
- set
- {
- _cy = value * EmusInPixel;
-
- foreach( XAttribute a in Xml.Descendants().Attributes( XName.Get( "cy" ) ) )
- a.Value = _cy.ToString();
- }
- }
-
- /// <summary>
- /// Gets or sets the Height of this Image (in Inches)
- /// </summary>
- public double HeightInches
- {
- get
- {
- return Height * EmusInPixel * EmuToInchFactor;
- }
-
- set
- {
- Height = ( int )( value * InchToEmuFactor / EmusInPixel );
- }
- }
-
-
- #endregion
-
- #region Constructors
-
- /// <summary>
- /// Wraps an XElement as an Image
- /// </summary>
- /// <param name="document"></param>
- /// <param name="i">The XElement i to wrap</param>
- /// <param name="image"></param>
- internal Picture( DocX document, XElement i, Image image ) : base( document, i )
- {
- _picture_rels = new Dictionary<PackagePart, PackageRelationship>();
-
- _img = image;
-
- var imageId =
- (
- 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
- ).SingleOrDefault();
-
- _id = ( imageId != null )
- ? imageId
- : (
- from e in Xml.Descendants()
- where e.Name.LocalName.Equals( "imagedata" )
- select e.Attribute( XName.Get( "id", "http://schemas.openxmlformats.org/officeDocument/2006/relationships" ) ).Value
- ).SingleOrDefault();
-
- var nameToFind =
- (
- from e in Xml.Descendants()
- let a = e.Attribute( XName.Get( "name" ) )
- where ( a != null )
- select a.Value
- ).FirstOrDefault();
-
- _name = ( nameToFind != null )
- ? nameToFind
- : (
- from e in Xml.Descendants()
- let a = e.Attribute( XName.Get( "title" ) )
- where ( a != null )
- select a.Value
- ).FirstOrDefault();
-
- _descr =
- (
- from e in Xml.Descendants()
- let a = e.Attribute( XName.Get( "descr" ) )
- where ( a != null )
- select a.Value
- ).FirstOrDefault();
-
- _cx =
- (
- from e in Xml.Descendants()
- let a = e.Attribute( XName.Get( "cx" ) )
- where ( a != null )
- select int.Parse( a.Value )
- ).FirstOrDefault();
-
- if( _cx == 0 )
- {
- var style =
- (
- from e in Xml.Descendants()
- let a = e.Attribute( XName.Get( "style" ) )
- where ( a != null )
- select a
- ).FirstOrDefault();
-
- if( style != null )
- {
- var widthString = style.Value.Substring( style.Value.IndexOf( "width:" ) + 6 );
- var widthValueString = widthString.Substring( 0, widthString.IndexOf( "pt" ) ).Replace( ".", "," );
- var widthDouble = double.Parse( widthValueString ) * EmusInPixel;
- _cx = System.Convert.ToInt32( widthDouble );
- }
- }
-
- _cy =
- (
- from e in Xml.Descendants()
- let a = e.Attribute( XName.Get( "cy" ) )
- where ( a != null )
- select int.Parse( a.Value )
- ).FirstOrDefault();
-
- if( _cy == 0 )
- {
- var style =
- (
- from e in Xml.Descendants()
- let a = e.Attribute( XName.Get( "style" ) )
- where ( a != null )
- select a
- ).FirstOrDefault();
-
- if( style != null )
- {
- var heightString = style.Value.Substring( style.Value.IndexOf( "height:" ) + 7 );
- var heightValueString = heightString.Substring( 0, heightString.IndexOf( "pt" ) ).Replace( ".", "," );
- var heightDouble = double.Parse( heightValueString ) * EmusInPixel;
- _cy = System.Convert.ToInt32( heightDouble );
- }
- }
-
- _xfrm =
- (
- from d in Xml.Descendants()
- where d.Name.LocalName.Equals( "xfrm" )
- select d
- ).SingleOrDefault();
-
- _prstGeom =
- (
- from d in Xml.Descendants()
- where d.Name.LocalName.Equals( "prstGeom" )
- select d
- ).SingleOrDefault();
-
- if( _xfrm != null )
- {
- _rotation = _xfrm.Attribute( XName.Get( "rot" ) ) == null ? 0 : uint.Parse( _xfrm.Attribute( XName.Get( "rot" ) ).Value );
- }
- }
-
- #endregion
-
- #region Public Methods
-
- /// <summary>
- /// Remove this Picture from this document.
- /// </summary>
- public void Remove()
- {
- Xml.Remove();
- }
-
- /// <summary>
- /// Set the shape of this Picture to one in the BasicShapes enumeration.
- /// </summary>
- /// <param name="shape">A shape from the BasicShapes enumeration.</param>
- public void SetPictureShape( BasicShapes shape )
- {
- SetPictureShape( ( object )shape );
- }
-
- /// <summary>
- /// Set the shape of this Picture to one in the RectangleShapes enumeration.
- /// </summary>
- /// <param name="shape">A shape from the RectangleShapes enumeration.</param>
- public void SetPictureShape( RectangleShapes shape )
- {
- SetPictureShape( ( object )shape );
- }
-
- /// <summary>
- /// Set the shape of this Picture to one in the BlockArrowShapes enumeration.
- /// </summary>
- /// <param name="shape">A shape from the BlockArrowShapes enumeration.</param>
- public void SetPictureShape( BlockArrowShapes shape )
- {
- SetPictureShape( ( object )shape );
- }
-
- /// <summary>
- /// Set the shape of this Picture to one in the EquationShapes enumeration.
- /// </summary>
- /// <param name="shape">A shape from the EquationShapes enumeration.</param>
- public void SetPictureShape( EquationShapes shape )
- {
- SetPictureShape( ( object )shape );
- }
-
- /// <summary>
- /// Set the shape of this Picture to one in the FlowchartShapes enumeration.
- /// </summary>
- /// <param name="shape">A shape from the FlowchartShapes enumeration.</param>
- public void SetPictureShape( FlowchartShapes shape )
- {
- SetPictureShape( ( object )shape );
- }
-
- /// <summary>
- /// Set the shape of this Picture to one in the StarAndBannerShapes enumeration.
- /// </summary>
- /// <param name="shape">A shape from the StarAndBannerShapes enumeration.</param>
- public void SetPictureShape( StarAndBannerShapes shape )
- {
- SetPictureShape( ( object )shape );
- }
-
- /// <summary>
- /// Set the shape of this Picture to one in the CalloutShapes enumeration.
- /// </summary>
- /// <param name="shape">A shape from the CalloutShapes enumeration.</param>
- public void SetPictureShape( CalloutShapes shape )
- {
- SetPictureShape( ( object )shape );
- }
-
- //public void Delete()
- //{
- // // Remove xml
- // i.Remove();
-
- // // Rebuild the image collection for this paragraph
- // // Requires that every Image have a link to its paragraph
-
- //}
-
- #endregion
-
- #region Private Methods
-
- private void SetPictureShape( object shape )
- {
- _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();
- }
-
- #endregion
- }
- }
|