InsertParagraphs.cs
  1. //
  2. // This code is part of Document Solutions for Word demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Linq;
  9. using GrapeCity.Documents.Word;
  10. using GcFont = GrapeCity.Documents.Word.Font;
  11.  
  12. namespace DsWordWeb.Demos
  13. {
  14. // This sample demonstrates how the InsertLocation value affects the result
  15. // when inserting a new paragraph near an existing paragraph (the 'anchor').
  16. //
  17. // In this sample, we first add five anchor paragraphs with different background colors,
  18. // interspersed with 'separator' paragraphs with default formatting.
  19. // We then insert four new paragraphs with default formatting near each of the
  20. // anchor paragraph but using different InsertLocation values (Before,
  21. // at Start, at End, and After). Finally, we add the fifth new paragraph
  22. // using the Add() method.
  23. //
  24. // The different background colors of the anchor paragraphs help visualizing
  25. // what happens when different insert locations are used. In particular note
  26. // how InsertLocation.Start and InsertLocation.End create an additional empty
  27. // paragraph with the same style as the style of the anchor paragraph (note
  28. // the same background color). This happens because these insert locations point
  29. // to just after the start, and to just before the end, respectively, of the anchor
  30. // paragraph, so insertion at these locations also splits the anchor paragraph.
  31. public class InsertParagraphs
  32. {
  33. public GcWordDocument CreateDocx()
  34. {
  35. var doc = new GcWordDocument();
  36. var baseStyle = doc.Styles.Add("Base-style", StyleType.Paragraph);
  37. var codeStyle = doc.Styles[BuiltInStyleId.HtmlCode];
  38.  
  39. const string basePara = "This is anchor paragraph number {0}, with style 'Base-style-{0}'.";
  40. const string sepPara = "\t--- This is separator between anchors {0} and {1} ---";
  41.  
  42. // Add 5 'anchor' paragraphs with different background colors:
  43. doc.Body.Paragraphs.Add(string.Format(sepPara, "start", 1));
  44. var p1style = doc.Styles.Add("Base-style-1", baseStyle);
  45. p1style.ParagraphFormat.Shading.BackgroundPatternColor.RGB = Color.FromArgb(unchecked((int)0xFF7BD3EA));
  46. var p1 = doc.Body.Paragraphs.Add(string.Format(basePara, 1), p1style);
  47.  
  48. doc.Body.Paragraphs.Add(string.Format(sepPara, 1, 2));
  49. var p2style = doc.Styles.Add("Base-style-2", baseStyle);
  50. p2style.ParagraphFormat.Shading.BackgroundPatternColor.RGB = Color.FromArgb(unchecked((int)0xFFC7DB9C));
  51. var p2 = doc.Body.Paragraphs.Add(string.Format(basePara, 2), p2style);
  52.  
  53. doc.Body.Paragraphs.Add(string.Format(sepPara, 2, 3));
  54. var p3style = doc.Styles.Add("Base-style-3", baseStyle);
  55. p3style.ParagraphFormat.Shading.BackgroundPatternColor.RGB = Color.FromArgb(unchecked((int)0xFFFFF08D));
  56. var p3 = doc.Body.Paragraphs.Add(string.Format(basePara, 3), p3style);
  57.  
  58. doc.Body.Paragraphs.Add(string.Format(sepPara, 3, 4));
  59. var p4style = doc.Styles.Add("Base-style-4", baseStyle);
  60. p4style.ParagraphFormat.Shading.BackgroundPatternColor.RGB = Color.FromArgb(unchecked((int)0xFFFDAB9E));
  61. var p4 = doc.Body.Paragraphs.Add(string.Format(basePara, 4), p4style);
  62.  
  63. doc.Body.Paragraphs.Add(string.Format(sepPara, 4, 5));
  64. var p5style = doc.Styles.Add("Base-style-5", baseStyle);
  65. p5style.ParagraphFormat.Shading.BackgroundPatternColor.RGB = Color.FromArgb(unchecked((int)0xFFCDC1FF));
  66. var p5 = doc.Body.Paragraphs.Add(string.Format(basePara, 5), p5style);
  67. doc.Body.Paragraphs.Add(string.Format(sepPara, 5, "end"));
  68.  
  69. // Insert paragraphs with default style using different InsertLocation's:
  70. p1.GetRange().Paragraphs.Insert("Paragraph inserted before paragraph 1 via ", InsertLocation.Before).AddRun("\np1.GetRange().Paragraphs.Insert(..., InsertLocation.Before);", codeStyle);
  71. p2.GetRange().Paragraphs.Insert("Paragraph inserted at start of paragraph 2 via ", InsertLocation.Start).AddRun("\np2.GetRange().Paragraphs.Insert(..., InsertLocation.Start);", codeStyle);
  72. p3.GetRange().Paragraphs.Insert("Paragraph inserted at end of paragraph 3 via ", InsertLocation.End).AddRun("\np3.GetRange().Paragraphs.Insert(..., InsertLocation.End);", codeStyle);
  73. p4.GetRange().Paragraphs.Insert("Paragraph inserted after paragraph 4 via ", InsertLocation.After).AddRun("\np4.GetRange().Paragraphs.Insert(..., InsertLocation.After);", codeStyle);
  74. // Paragraphs.Add() is a shortcut to Insert(InsertLocation.End):
  75. p5.GetRange().Paragraphs.Add("Paragraph added after base paragraph 5 via ").AddRun("\np5.GetRange().Paragraphs.Add();", codeStyle);
  76.  
  77. // Done:
  78. return doc;
  79. }
  80. }
  81. }
  82.