Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Picture.cs 12KB

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