Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

_Extensions.cs 3.6KB

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