Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Picture.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*************************************************************************************
  2. DocX – DocX is the community edition of Xceed Words for .NET
  3. Copyright (C) 2009-2016 Xceed Software Inc.
  4. This program is provided to you under the terms of the Microsoft Public
  5. License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
  6. For more features and fast professional support,
  7. pick up Xceed Words for .NET at https://xceed.com/xceed-words-for-net/
  8. ***********************************************************************************/
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Xml.Linq;
  12. using System.IO.Packaging;
  13. namespace Xceed.Words.NET
  14. {
  15. /// <summary>
  16. /// Represents a Picture in this document, a Picture is a customized view of an Image.
  17. /// </summary>
  18. public class Picture : DocXElement
  19. {
  20. #region Private Members
  21. private const int EmusInPixel = 9525;
  22. private string _id;
  23. private string _name;
  24. private string _descr;
  25. private int _cx, _cy;
  26. private uint _rotation;
  27. private bool _hFlip, _vFlip;
  28. private object _pictureShape;
  29. private XElement _xfrm;
  30. private XElement _prstGeom;
  31. #endregion
  32. #region Internal Members
  33. internal Dictionary<PackagePart, PackageRelationship> _picture_rels;
  34. internal Image _img;
  35. #endregion
  36. #region Public Properties
  37. /// <summary>
  38. /// A unique id that identifies an Image embedded in this document.
  39. /// </summary>
  40. public string Id
  41. {
  42. get
  43. {
  44. return _id;
  45. }
  46. }
  47. /// <summary>
  48. /// Flip this Picture Horizontally.
  49. /// </summary>
  50. public bool FlipHorizontal
  51. {
  52. get
  53. {
  54. return _hFlip;
  55. }
  56. set
  57. {
  58. _hFlip = value;
  59. var flipH = _xfrm.Attribute( XName.Get( "flipH" ) );
  60. if( flipH == null )
  61. {
  62. _xfrm.Add( new XAttribute( XName.Get( "flipH" ), "0" ) );
  63. }
  64. _xfrm.Attribute( XName.Get( "flipH" ) ).Value = _hFlip ? "1" : "0";
  65. }
  66. }
  67. /// <summary>
  68. /// Flip this Picture Vertically.
  69. /// </summary>
  70. public bool FlipVertical
  71. {
  72. get
  73. {
  74. return _vFlip;
  75. }
  76. set
  77. {
  78. _vFlip = value;
  79. var flipV = _xfrm.Attribute( XName.Get( "flipV" ) );
  80. if( flipV == null )
  81. {
  82. _xfrm.Add( new XAttribute( XName.Get( "flipV" ), "0" ) );
  83. }
  84. _xfrm.Attribute( XName.Get( "flipV" ) ).Value = _vFlip ? "1" : "0";
  85. }
  86. }
  87. /// <summary>
  88. /// The rotation in degrees of this image, actual value = value % 360
  89. /// </summary>
  90. public uint Rotation
  91. {
  92. get
  93. {
  94. return _rotation / 60000;
  95. }
  96. set
  97. {
  98. _rotation = ( value % 360 ) * 60000;
  99. var xfrm =
  100. ( from d in Xml.Descendants()
  101. where d.Name.LocalName.Equals( "xfrm" )
  102. select d ).Single();
  103. var rot = xfrm.Attribute( XName.Get( "rot" ) );
  104. if( rot == null )
  105. {
  106. xfrm.Add( new XAttribute( XName.Get( "rot" ), 0 ) );
  107. }
  108. xfrm.Attribute( XName.Get( "rot" ) ).Value = _rotation.ToString();
  109. }
  110. }
  111. /// <summary>
  112. /// Gets or sets the name of this Image.
  113. /// </summary>
  114. public string Name
  115. {
  116. get
  117. {
  118. return _name;
  119. }
  120. set
  121. {
  122. _name = value;
  123. foreach( XAttribute a in Xml.Descendants().Attributes( XName.Get( "name" ) ) )
  124. {
  125. a.Value = _name;
  126. }
  127. }
  128. }
  129. /// <summary>
  130. /// Gets or sets the description for this Image.
  131. /// </summary>
  132. public string Description
  133. {
  134. get
  135. {
  136. return _descr;
  137. }
  138. set
  139. {
  140. _descr = value;
  141. foreach( XAttribute a in Xml.Descendants().Attributes( XName.Get( "descr" ) ) )
  142. {
  143. a.Value = _descr;
  144. }
  145. }
  146. }
  147. ///<summary>
  148. /// Returns the name of the image file for the picture.
  149. ///</summary>
  150. public string FileName
  151. {
  152. get
  153. {
  154. return _img.FileName;
  155. }
  156. }
  157. /// <summary>
  158. /// Get or sets the Width of this Image.
  159. /// </summary>
  160. public int Width
  161. {
  162. get
  163. {
  164. return _cx / EmusInPixel;
  165. }
  166. set
  167. {
  168. _cx = value * EmusInPixel;
  169. foreach( XAttribute a in Xml.Descendants().Attributes( XName.Get( "cx" ) ) )
  170. a.Value = _cx.ToString();
  171. }
  172. }
  173. /// <summary>
  174. /// Get or sets the height of this Image.
  175. /// </summary>
  176. public int Height
  177. {
  178. get
  179. {
  180. return _cy / EmusInPixel;
  181. }
  182. set
  183. {
  184. _cy = value * EmusInPixel;
  185. foreach( XAttribute a in Xml.Descendants().Attributes( XName.Get( "cy" ) ) )
  186. a.Value = _cy.ToString();
  187. }
  188. }
  189. #endregion
  190. #region Constructors
  191. /// <summary>
  192. /// Wraps an XElement as an Image
  193. /// </summary>
  194. /// <param name="document"></param>
  195. /// <param name="i">The XElement i to wrap</param>
  196. /// <param name="image"></param>
  197. internal Picture( DocX document, XElement i, Image image ) : base( document, i )
  198. {
  199. _picture_rels = new Dictionary<PackagePart, PackageRelationship>();
  200. _img = image;
  201. var imageId =
  202. (
  203. from e in Xml.Descendants()
  204. where e.Name.LocalName.Equals( "blip" )
  205. select e.Attribute( XName.Get( "embed", "http://schemas.openxmlformats.org/officeDocument/2006/relationships" ) ).Value
  206. ).SingleOrDefault();
  207. _id = ( imageId != null )
  208. ? imageId
  209. : (
  210. from e in Xml.Descendants()
  211. where e.Name.LocalName.Equals( "imagedata" )
  212. select e.Attribute( XName.Get( "id", "http://schemas.openxmlformats.org/officeDocument/2006/relationships" ) ).Value
  213. ).SingleOrDefault();
  214. var nameToFind =
  215. (
  216. from e in Xml.Descendants()
  217. let a = e.Attribute( XName.Get( "name" ) )
  218. where ( a != null )
  219. select a.Value
  220. ).FirstOrDefault();
  221. _name = ( nameToFind != null )
  222. ? nameToFind
  223. : (
  224. from e in Xml.Descendants()
  225. let a = e.Attribute( XName.Get( "title" ) )
  226. where ( a != null )
  227. select a.Value
  228. ).FirstOrDefault();
  229. _descr =
  230. (
  231. from e in Xml.Descendants()
  232. let a = e.Attribute( XName.Get( "descr" ) )
  233. where ( a != null )
  234. select a.Value
  235. ).FirstOrDefault();
  236. _cx =
  237. (
  238. from e in Xml.Descendants()
  239. let a = e.Attribute( XName.Get( "cx" ) )
  240. where ( a != null )
  241. select int.Parse( a.Value )
  242. ).FirstOrDefault();
  243. if( _cx == 0 )
  244. {
  245. var style =
  246. (
  247. from e in Xml.Descendants()
  248. let a = e.Attribute( XName.Get( "style" ) )
  249. where ( a != null )
  250. select a
  251. ).FirstOrDefault();
  252. if( style != null )
  253. {
  254. var widthString = style.Value.Substring( style.Value.IndexOf( "width:" ) + 6 );
  255. var widthValueString = widthString.Substring( 0, widthString.IndexOf( "pt" ) ).Replace( ".", "," );
  256. var widthDouble = ( double.Parse( widthValueString ) / 72d ) * 914400;
  257. _cx = System.Convert.ToInt32( widthDouble );
  258. }
  259. }
  260. _cy =
  261. (
  262. from e in Xml.Descendants()
  263. let a = e.Attribute( XName.Get( "cy" ) )
  264. where ( a != null )
  265. select int.Parse( a.Value )
  266. ).FirstOrDefault();
  267. if( _cy == 0 )
  268. {
  269. var style =
  270. (
  271. from e in Xml.Descendants()
  272. let a = e.Attribute( XName.Get( "style" ) )
  273. where ( a != null )
  274. select a
  275. ).FirstOrDefault();
  276. if( style != null )
  277. {
  278. var heightString = style.Value.Substring( style.Value.IndexOf( "height:" ) + 7 );
  279. var heightValueString = heightString.Substring( 0, heightString.IndexOf( "pt" ) ).Replace( ".", "," );
  280. var heightDouble = ( double.Parse( heightValueString ) / 72d ) * 914400;
  281. _cy = System.Convert.ToInt32( heightDouble );
  282. }
  283. }
  284. _xfrm =
  285. (
  286. from d in Xml.Descendants()
  287. where d.Name.LocalName.Equals( "xfrm" )
  288. select d
  289. ).SingleOrDefault();
  290. _prstGeom =
  291. (
  292. from d in Xml.Descendants()
  293. where d.Name.LocalName.Equals( "prstGeom" )
  294. select d
  295. ).SingleOrDefault();
  296. if( _xfrm != null )
  297. {
  298. _rotation = _xfrm.Attribute( XName.Get( "rot" ) ) == null ? 0 : uint.Parse( _xfrm.Attribute( XName.Get( "rot" ) ).Value );
  299. }
  300. }
  301. #endregion
  302. #region Public Methods
  303. /// <summary>
  304. /// Remove this Picture from this document.
  305. /// </summary>
  306. public void Remove()
  307. {
  308. Xml.Remove();
  309. }
  310. /// <summary>
  311. /// Set the shape of this Picture to one in the BasicShapes enumeration.
  312. /// </summary>
  313. /// <param name="shape">A shape from the BasicShapes enumeration.</param>
  314. public void SetPictureShape( BasicShapes shape )
  315. {
  316. SetPictureShape( ( object )shape );
  317. }
  318. /// <summary>
  319. /// Set the shape of this Picture to one in the RectangleShapes enumeration.
  320. /// </summary>
  321. /// <param name="shape">A shape from the RectangleShapes enumeration.</param>
  322. public void SetPictureShape( RectangleShapes shape )
  323. {
  324. SetPictureShape( ( object )shape );
  325. }
  326. /// <summary>
  327. /// Set the shape of this Picture to one in the BlockArrowShapes enumeration.
  328. /// </summary>
  329. /// <param name="shape">A shape from the BlockArrowShapes enumeration.</param>
  330. public void SetPictureShape( BlockArrowShapes shape )
  331. {
  332. SetPictureShape( ( object )shape );
  333. }
  334. /// <summary>
  335. /// Set the shape of this Picture to one in the EquationShapes enumeration.
  336. /// </summary>
  337. /// <param name="shape">A shape from the EquationShapes enumeration.</param>
  338. public void SetPictureShape( EquationShapes shape )
  339. {
  340. SetPictureShape( ( object )shape );
  341. }
  342. /// <summary>
  343. /// Set the shape of this Picture to one in the FlowchartShapes enumeration.
  344. /// </summary>
  345. /// <param name="shape">A shape from the FlowchartShapes enumeration.</param>
  346. public void SetPictureShape( FlowchartShapes shape )
  347. {
  348. SetPictureShape( ( object )shape );
  349. }
  350. /// <summary>
  351. /// Set the shape of this Picture to one in the StarAndBannerShapes enumeration.
  352. /// </summary>
  353. /// <param name="shape">A shape from the StarAndBannerShapes enumeration.</param>
  354. public void SetPictureShape( StarAndBannerShapes shape )
  355. {
  356. SetPictureShape( ( object )shape );
  357. }
  358. /// <summary>
  359. /// Set the shape of this Picture to one in the CalloutShapes enumeration.
  360. /// </summary>
  361. /// <param name="shape">A shape from the CalloutShapes enumeration.</param>
  362. public void SetPictureShape( CalloutShapes shape )
  363. {
  364. SetPictureShape( ( object )shape );
  365. }
  366. //public void Delete()
  367. //{
  368. // // Remove xml
  369. // i.Remove();
  370. // // Rebuild the image collection for this paragraph
  371. // // Requires that every Image have a link to its paragraph
  372. //}
  373. #endregion
  374. #region Private Methods
  375. private void SetPictureShape( object shape )
  376. {
  377. _pictureShape = shape;
  378. XAttribute prst = _prstGeom.Attribute( XName.Get( "prst" ) );
  379. if( prst == null )
  380. {
  381. _prstGeom.Add( new XAttribute( XName.Get( "prst" ), "rectangle" ) );
  382. }
  383. _prstGeom.Attribute( XName.Get( "prst" ) ).Value = shape.ToString();
  384. }
  385. #endregion
  386. }
  387. }