Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

_Extensions.cs 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Drawing;
  4. using System.Xml.Linq;
  5. namespace Novacode
  6. {
  7. internal static class Extensions
  8. {
  9. internal static string ToHex(this Color source)
  10. {
  11. byte red = source.R;
  12. byte green = source.G;
  13. byte blue = source.B;
  14. string redHex = red.ToString("X");
  15. if (redHex.Length < 2)
  16. redHex = "0" + redHex;
  17. string blueHex = blue.ToString("X");
  18. if (blueHex.Length < 2)
  19. blueHex = "0" + blueHex;
  20. string greenHex = green.ToString("X");
  21. if (greenHex.Length < 2)
  22. greenHex = "0" + greenHex;
  23. return string.Format("{0}{1}{2}", redHex, greenHex, blueHex);
  24. }
  25. public static void Flatten(this XElement e, XName name, List<XElement> flat)
  26. {
  27. // Add this element (without its children) to the flat list.
  28. XElement clone = CloneElement(e);
  29. clone.Elements().Remove();
  30. // Filter elements using XName.
  31. if (clone.Name == name)
  32. flat.Add(clone);
  33. // Process the children.
  34. if (e.HasElements)
  35. foreach (XElement elem in e.Elements(name)) // Filter elements using XName
  36. elem.Flatten(name, flat);
  37. }
  38. static XElement CloneElement(XElement element)
  39. {
  40. return new XElement(element.Name,
  41. element.Attributes(),
  42. element.Nodes().Select(n =>
  43. {
  44. XElement e = n as XElement;
  45. if (e != null)
  46. return CloneElement(e);
  47. return n;
  48. }
  49. )
  50. );
  51. }
  52. public static string GetAttribute(this XElement el, XName name, string defaultValue = "")
  53. {
  54. var attr = el.Attribute(name);
  55. if (attr != null)
  56. return attr.Value;
  57. return defaultValue;
  58. }
  59. /// <summary>
  60. /// Sets margin for all the pages in a Dox document in Inches. (Written by Shashwat Tripathi)
  61. /// </summary>
  62. /// <param name="document"></param>
  63. /// <param name="top">Margin from the Top. Leave -1 for no change</param>
  64. /// <param name="bottom">Margin from the Bottom. Leave -1 for no change</param>
  65. /// <param name="right">Margin from the Right. Leave -1 for no change</param>
  66. /// <param name="left">Margin from the Left. Leave -1 for no change</param>
  67. public static void SetMargin(this DocX document, float top, float bottom, float right, float left)
  68. {
  69. XNamespace ab = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
  70. var tempElement = document.PageLayout.Xml.Descendants(ab + "pgMar");
  71. foreach (var item in tempElement)
  72. {
  73. if (left != -1)
  74. item.SetAttributeValue(ab + "left", (1440 * left) / 1);
  75. if (right != -1)
  76. item.SetAttributeValue(ab + "right", (1440 * right) / 1);
  77. if (top != -1)
  78. item.SetAttributeValue(ab + "top", (1440 * top) / 1);
  79. if (bottom != -1)
  80. item.SetAttributeValue(ab + "bottom", (1440 * bottom) / 1);
  81. }
  82. }
  83. }
  84. }