Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Picture.cs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Linq;
  6. using System.Drawing;
  7. using System.IO.Packaging;
  8. namespace Novacode
  9. {
  10. /// <summary>
  11. /// Represents a Picture in this document, a Picture is a customized view of an Image.
  12. /// </summary>
  13. public class Picture
  14. {
  15. private string id;
  16. private string name;
  17. private string descr;
  18. private int cx, cy;
  19. private uint rotation;
  20. private bool hFlip, vFlip;
  21. private object pictureShape;
  22. // The underlying XElement which this Image wraps
  23. internal XElement i;
  24. private XElement xfrm;
  25. private XElement prstGeom;
  26. /// <summary>
  27. /// Create a new Picture.
  28. /// </summary>
  29. /// <param name="id">A unique id that identifies an Image embedded in this document.</param>
  30. /// <param name="name">The name of this Picture.</param>
  31. /// <param name="descr">The description of this Picture.</param>
  32. internal Picture(DocX document, string id, string name, string descr)
  33. {
  34. PackagePart word_document = document.package.GetPart(new Uri("/word/document.xml", UriKind.Relative));
  35. PackagePart part = document.package.GetPart(word_document.GetRelationship(id).TargetUri);
  36. this.id = id;
  37. this.name = name;
  38. this.descr = descr;
  39. using (System.Drawing.Image img = System.Drawing.Image.FromStream(part.GetStream()))
  40. {
  41. this.cx = img.Width * 4156;
  42. this.cy = img.Height * 4156;
  43. }
  44. XElement e = new XElement(DocX.w + "drawing");
  45. i = XElement.Parse
  46. (string.Format(@"
  47. <drawing xmlns = ""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">
  48. <wp:inline distT=""0"" distB=""0"" distL=""0"" distR=""0"" xmlns:wp=""http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"">
  49. <wp:extent cx=""{0}"" cy=""{1}"" />
  50. <wp:effectExtent l=""0"" t=""0"" r=""0"" b=""0"" />
  51. <wp:docPr id=""1"" name=""{3}"" descr=""{4}"" />
  52. <wp:cNvGraphicFramePr>
  53. <a:graphicFrameLocks xmlns:a=""http://schemas.openxmlformats.org/drawingml/2006/main"" noChangeAspect=""1"" />
  54. </wp:cNvGraphicFramePr>
  55. <a:graphic xmlns:a=""http://schemas.openxmlformats.org/drawingml/2006/main"">
  56. <a:graphicData uri=""http://schemas.openxmlformats.org/drawingml/2006/picture"">
  57. <pic:pic xmlns:pic=""http://schemas.openxmlformats.org/drawingml/2006/picture"">
  58. <pic:nvPicPr>
  59. <pic:cNvPr id=""0"" name=""{3}"" />
  60. <pic:cNvPicPr />
  61. </pic:nvPicPr>
  62. <pic:blipFill>
  63. <a:blip r:embed=""{2}"" xmlns:r=""http://schemas.openxmlformats.org/officeDocument/2006/relationships""/>
  64. <a:stretch>
  65. <a:fillRect />
  66. </a:stretch>
  67. </pic:blipFill>
  68. <pic:spPr>
  69. <a:xfrm>
  70. <a:off x=""0"" y=""0"" />
  71. <a:ext cx=""{0}"" cy=""{1}"" />
  72. </a:xfrm>
  73. <a:prstGeom prst=""rect"">
  74. <a:avLst />
  75. </a:prstGeom>
  76. </pic:spPr>
  77. </pic:pic>
  78. </a:graphicData>
  79. </a:graphic>
  80. </wp:inline>
  81. </drawing>
  82. ", cx, cy, id, name, descr));
  83. this.xfrm =
  84. (
  85. from d in i.Descendants()
  86. where d.Name.LocalName.Equals("xfrm")
  87. select d
  88. ).Single();
  89. this.prstGeom =
  90. (
  91. from d in i.Descendants()
  92. where d.Name.LocalName.Equals("prstGeom")
  93. select d
  94. ).Single();
  95. this.rotation = xfrm.Attribute(XName.Get("rot")) == null ? 0 : uint.Parse(xfrm.Attribute(XName.Get("rot")).Value);
  96. }
  97. /// <summary>
  98. /// Wraps an XElement as an Image
  99. /// </summary>
  100. /// <param name="i">The XElement i to wrap</param>
  101. internal Picture(XElement i)
  102. {
  103. this.i = i;
  104. this.id =
  105. (
  106. from e in i.Descendants()
  107. where e.Name.LocalName.Equals("blip")
  108. select e.Attribute(XName.Get("embed", "http://schemas.openxmlformats.org/officeDocument/2006/relationships")).Value
  109. ).Single();
  110. this.name =
  111. (
  112. from e in i.Descendants()
  113. let a = e.Attribute(XName.Get("name"))
  114. where (a != null)
  115. select a.Value
  116. ).First();
  117. this.descr =
  118. (
  119. from e in i.Descendants()
  120. let a = e.Attribute(XName.Get("descr"))
  121. where (a != null)
  122. select a.Value
  123. ).First();
  124. this.cx =
  125. (
  126. from e in i.Descendants()
  127. let a = e.Attribute(XName.Get("cx"))
  128. where (a != null)
  129. select int.Parse(a.Value)
  130. ).First();
  131. this.cy =
  132. (
  133. from e in i.Descendants()
  134. let a = e.Attribute(XName.Get("cy"))
  135. where (a != null)
  136. select int.Parse(a.Value)
  137. ).First();
  138. this.xfrm =
  139. (
  140. from d in i.Descendants()
  141. where d.Name.LocalName.Equals("xfrm")
  142. select d
  143. ).Single();
  144. this.prstGeom =
  145. (
  146. from d in i.Descendants()
  147. where d.Name.LocalName.Equals("prstGeom")
  148. select d
  149. ).Single();
  150. this.rotation = xfrm.Attribute(XName.Get("rot")) == null ? 0 : uint.Parse(xfrm.Attribute(XName.Get("rot")).Value);
  151. }
  152. private void SetPictureShape(object shape)
  153. {
  154. this.pictureShape = shape;
  155. XAttribute prst = prstGeom.Attribute(XName.Get("prst"));
  156. if (prst == null)
  157. prstGeom.Add(new XAttribute(XName.Get("prst"), "rectangle"));
  158. prstGeom.Attribute(XName.Get("prst")).Value = shape.ToString();
  159. }
  160. /// <summary>
  161. /// Set the shape of this Picture to one in the BasicShapes enumeration.
  162. /// </summary>
  163. /// <param name="shape">A shape from the BasicShapes enumeration.</param>
  164. public void SetPictureShape(BasicShapes shape)
  165. {
  166. SetPictureShape((object)shape);
  167. }
  168. /// <summary>
  169. /// Set the shape of this Picture to one in the RectangleShapes enumeration.
  170. /// </summary>
  171. /// <param name="shape">A shape from the RectangleShapes enumeration.</param>
  172. public void SetPictureShape(RectangleShapes shape)
  173. {
  174. SetPictureShape((object)shape);
  175. }
  176. /// <summary>
  177. /// Set the shape of this Picture to one in the BlockArrowShapes enumeration.
  178. /// </summary>
  179. /// <param name="shape">A shape from the BlockArrowShapes enumeration.</param>
  180. public void SetPictureShape(BlockArrowShapes shape)
  181. {
  182. SetPictureShape((object)shape);
  183. }
  184. /// <summary>
  185. /// Set the shape of this Picture to one in the EquationShapes enumeration.
  186. /// </summary>
  187. /// <param name="shape">A shape from the EquationShapes enumeration.</param>
  188. public void SetPictureShape(EquationShapes shape)
  189. {
  190. SetPictureShape((object)shape);
  191. }
  192. /// <summary>
  193. /// Set the shape of this Picture to one in the FlowchartShapes enumeration.
  194. /// </summary>
  195. /// <param name="shape">A shape from the FlowchartShapes enumeration.</param>
  196. public void SetPictureShape(FlowchartShapes shape)
  197. {
  198. SetPictureShape((object)shape);
  199. }
  200. /// <summary>
  201. /// Set the shape of this Picture to one in the StarAndBannerShapes enumeration.
  202. /// </summary>
  203. /// <param name="shape">A shape from the StarAndBannerShapes enumeration.</param>
  204. public void SetPictureShape(StarAndBannerShapes shape)
  205. {
  206. SetPictureShape((object)shape);
  207. }
  208. /// <summary>
  209. /// Set the shape of this Picture to one in the CalloutShapes enumeration.
  210. /// </summary>
  211. /// <param name="shape">A shape from the CalloutShapes enumeration.</param>
  212. public void SetPictureShape(CalloutShapes shape)
  213. {
  214. SetPictureShape((object)shape);
  215. }
  216. /// <summary>
  217. /// A unique id that identifies an Image embedded in this document.
  218. /// </summary>
  219. public string Id
  220. {
  221. get { return id; }
  222. }
  223. /// <summary>
  224. /// Flip this Picture Horizontally.
  225. /// </summary>
  226. public bool FlipHorizontal
  227. {
  228. get { return hFlip; }
  229. set
  230. {
  231. hFlip = value;
  232. XAttribute flipH = xfrm.Attribute(XName.Get("flipH"));
  233. if (flipH == null)
  234. xfrm.Add(new XAttribute(XName.Get("flipH"), "0"));
  235. xfrm.Attribute(XName.Get("flipH")).Value = hFlip ? "1" : "0";
  236. }
  237. }
  238. /// <summary>
  239. /// Flip this Picture Vertically.
  240. /// </summary>
  241. public bool FlipVertical
  242. {
  243. get { return vFlip; }
  244. set
  245. {
  246. vFlip = value;
  247. XAttribute flipV = xfrm.Attribute(XName.Get("flipV"));
  248. if (flipV == null)
  249. xfrm.Add(new XAttribute(XName.Get("flipV"), "0"));
  250. xfrm.Attribute(XName.Get("flipV")).Value = vFlip ? "1" : "0";
  251. }
  252. }
  253. /// <summary>
  254. /// The rotation in degrees of this image, actual value = value % 360
  255. /// </summary>
  256. public uint Rotation
  257. {
  258. get { return rotation / 60000; }
  259. set
  260. {
  261. rotation = (value % 360) * 60000;
  262. XElement xfrm =
  263. (from d in i.Descendants()
  264. where d.Name.LocalName.Equals("xfrm")
  265. select d).Single();
  266. XAttribute rot = xfrm.Attribute(XName.Get("rot"));
  267. if(rot == null)
  268. xfrm.Add(new XAttribute(XName.Get("rot"), 0));
  269. xfrm.Attribute(XName.Get("rot")).Value = rotation.ToString();
  270. }
  271. }
  272. /// <summary>
  273. /// Gets or sets the name of this Image.
  274. /// </summary>
  275. public string Name
  276. {
  277. get { return name; }
  278. set
  279. {
  280. name = value;
  281. foreach (XAttribute a in i.Descendants().Attributes(XName.Get("name")))
  282. a.Value = name;
  283. }
  284. }
  285. /// <summary>
  286. /// Gets or sets the description for this Image.
  287. /// </summary>
  288. public string Description
  289. {
  290. get { return descr; }
  291. set
  292. {
  293. descr = value;
  294. foreach (XAttribute a in i.Descendants().Attributes(XName.Get("descr")))
  295. a.Value = descr;
  296. }
  297. }
  298. /// <summary>
  299. /// Get or sets the Width of this Image.
  300. /// </summary>
  301. public int Width
  302. {
  303. get { return cx / 4156; }
  304. set
  305. {
  306. cx = value;
  307. foreach (XAttribute a in i.Descendants().Attributes(XName.Get("cx")))
  308. a.Value = (cx * 4156).ToString();
  309. }
  310. }
  311. /// <summary>
  312. /// Get or sets the height of this Image.
  313. /// </summary>
  314. public int Height
  315. {
  316. get { return cy / 4156; }
  317. set
  318. {
  319. cy = value;
  320. foreach (XAttribute a in i.Descendants().Attributes(XName.Get("cy")))
  321. a.Value = (cy * 4156).ToString();
  322. }
  323. }
  324. //public void Delete()
  325. //{
  326. // // Remove xml
  327. // i.Remove();
  328. // // Rebuild the image collection for this paragraph
  329. // // Requires that every Image have a link to its paragraph
  330. //}
  331. }
  332. }