You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_Extensions.cs 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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.Drawing;
  12. using System.Xml.Linq;
  13. namespace Xceed.Words.NET
  14. {
  15. internal static class Extensions
  16. {
  17. internal static string ToHex( this Color source )
  18. {
  19. byte red = source.R;
  20. byte green = source.G;
  21. byte blue = source.B;
  22. string redHex = red.ToString( "X" );
  23. if( redHex.Length < 2 )
  24. redHex = "0" + redHex;
  25. string blueHex = blue.ToString( "X" );
  26. if( blueHex.Length < 2 )
  27. blueHex = "0" + blueHex;
  28. string greenHex = green.ToString( "X" );
  29. if( greenHex.Length < 2 )
  30. greenHex = "0" + greenHex;
  31. return string.Format( "{0}{1}{2}", redHex, greenHex, blueHex );
  32. }
  33. public static void Flatten( this XElement e, XName name, List<XElement> flat )
  34. {
  35. // Add this element (without its children) to the flat list.
  36. XElement clone = CloneElement( e );
  37. clone.Elements().Remove();
  38. // Filter elements using XName.
  39. if( clone.Name == name )
  40. flat.Add( clone );
  41. // Process the children.
  42. if( e.HasElements )
  43. foreach( XElement elem in e.Elements( name ) ) // Filter elements using XName
  44. elem.Flatten( name, flat );
  45. }
  46. public static string GetAttribute( this XElement el, XName name, string defaultValue = "" )
  47. {
  48. var attribute = el.Attribute( name );
  49. if( attribute != null )
  50. return attribute.Value;
  51. return defaultValue;
  52. }
  53. /// <summary>
  54. /// Sets margin for all the pages in a DocX document in inches.
  55. /// </summary>
  56. /// <param name="document"></param>
  57. /// <param name="top">Margin from the top. -1 for no change.</param>
  58. /// <param name="bottom">Margin from the bottom. -1 for no change.</param>
  59. /// <param name="right">Margin from the right. -1 for no change.</param>
  60. /// <param name="left">Margin from the left. -1 for no change.</param>
  61. public static void SetMargin( this DocX document, float top, float bottom, float right, float left )
  62. {
  63. var xNameSpace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
  64. var tempElement = document.PageLayout.Xml.Descendants( xNameSpace + "pgMar" );
  65. var multiplier = 1440;
  66. foreach( var item in tempElement )
  67. {
  68. if( top != -1 )
  69. {
  70. item.SetAttributeValue( xNameSpace + "top", multiplier * top );
  71. }
  72. if( bottom != -1 )
  73. {
  74. item.SetAttributeValue( xNameSpace + "bottom", multiplier * bottom );
  75. }
  76. if( right != -1 )
  77. {
  78. item.SetAttributeValue( xNameSpace + "right", multiplier * right );
  79. }
  80. if( left != -1 )
  81. {
  82. item.SetAttributeValue( xNameSpace + "left", multiplier * left );
  83. }
  84. }
  85. }
  86. private static XElement CloneElement( XElement element )
  87. {
  88. return new XElement( element.Name,
  89. element.Attributes(),
  90. element.Nodes().Select( n =>
  91. {
  92. XElement e = n as XElement;
  93. if( e != null )
  94. return CloneElement( e );
  95. return n;
  96. }
  97. )
  98. );
  99. }
  100. }
  101. }