Posted By

user982375982 on 07/23/12


Tagged


Versions (?)

DocX - SetFont


 / Published in: C#
 

DocX ApplyTextFormattingProperty method - test

  1. public Paragraph Font(FontFamily fontFamily)
  2. {
  3. ApplyTextFormattingProperty
  4. (
  5. XName.Get("rFonts", DocX.w.NamespaceName),
  6. string.Empty,
  7. new XAttribute(XName.Get("ascii", DocX.w.NamespaceName), fontFamily.Name),
  8. new XAttribute(XName.Get("hAnsi", DocX.w.NamespaceName), fontFamily.Name), // Added by Maurits Elbers to support non-standard characters. See http://docx.codeplex.com/Thread/View.aspx?ThreadId=70097&ANCHOR#Post453865
  9. new XAttribute(XName.Get("cs", DocX.w.NamespaceName), fontFamily.Name) // Added by Maurits Elbers to support non-standard characters. See http://docx.codeplex.com/Thread/View.aspx?ThreadId=70097&ANCHOR#Post453865
  10. );
  11.  
  12. return this;
  13. }
  14.  
  15. internal void ApplyTextFormattingProperty(XName textFormatPropName, string value, params XAttribute[] content)
  16. {
  17. XElement rPr;
  18.  
  19. if (Runs.Count == 0)
  20. {
  21. XElement pPr = Xml.Element(XNameW.Get("pPr"));
  22. if (pPr == null)
  23. {
  24. Xml.AddFirst(new XElement(XNameW.Get("pPr")));
  25. pPr = Xml.Element(XNameW.Get("pPr"));
  26. }
  27.  
  28. rPr = pPr.Element(XNameW.Get("rPr"));
  29. if (rPr == null)
  30. {
  31. pPr.AddFirst(new XElement(XNameW.Get("rPr")));
  32. rPr = pPr.Element(XNameW.Get("rPr"));
  33. }
  34.  
  35. rPr.SetElementValue(textFormatPropName, value);
  36. return;
  37. }
  38.  
  39. foreach (var run in Runs)
  40. {
  41. rPr = run.Element(XNameW.Get("rPr"));
  42. if (rPr == null)
  43. {
  44. run.AddFirst(new XElement(XNameW.Get("rPr")));
  45. rPr = run.Element(XNameW.Get("rPr"));
  46. }
  47.  
  48. rPr.SetElementValue(textFormatPropName, value);
  49.  
  50. if (content != null)
  51. {
  52. XElement last = rPr.Elements().Last();
  53. foreach (XAttribute o in content)
  54. {
  55. if (last.Attribute(o.Name) == null)
  56. {
  57. last.Add(o); //Add this attribute if element doesn't have it
  58. }
  59. else
  60. {
  61. last.Attribute(o.Name).Value = o.Value; //Apply value only if element already has it
  62. }
  63. }
  64. }
  65. }
  66. }

Report this snippet  

You need to login to post a comment.