- //
- // This code is part of Document Solutions for Word demos.
- // Copyright (c) MESCIUS inc. All rights reserved.
- //
- using System;
- using System.IO;
- using System.Drawing;
- using System.Linq;
- using GrapeCity.Documents.Word;
- using GcFont = GrapeCity.Documents.Word.Font;
- namespace DsWordWeb.Demos
- {
- // This sample demonstrates how the InsertLocation value affects the result
- // when inserting a new paragraph near an existing paragraph (the 'anchor').
- //
- // In this sample, we first add five anchor paragraphs with different background colors,
- // interspersed with 'separator' paragraphs with default formatting.
- // We then insert four new paragraphs with default formatting near each of the
- // anchor paragraph but using different InsertLocation values (Before,
- // at Start, at End, and After). Finally, we add the fifth new paragraph
- // using the Add() method.
- //
- // The different background colors of the anchor paragraphs help visualizing
- // what happens when different insert locations are used. In particular note
- // how InsertLocation.Start and InsertLocation.End create an additional empty
- // paragraph with the same style as the style of the anchor paragraph (note
- // the same background color). This happens because these insert locations point
- // to just after the start, and to just before the end, respectively, of the anchor
- // paragraph, so insertion at these locations also splits the anchor paragraph.
- public class InsertParagraphs
- {
- public GcWordDocument CreateDocx()
- {
- var doc = new GcWordDocument();
- var baseStyle = doc.Styles.Add("Base-style", StyleType.Paragraph);
- var codeStyle = doc.Styles[BuiltInStyleId.HtmlCode];
- const string basePara = "This is anchor paragraph number {0}, with style 'Base-style-{0}'.";
- const string sepPara = "\t--- This is separator between anchors {0} and {1} ---";
- // Add 5 'anchor' paragraphs with different background colors:
- doc.Body.Paragraphs.Add(string.Format(sepPara, "start", 1));
- var p1style = doc.Styles.Add("Base-style-1", baseStyle);
- p1style.ParagraphFormat.Shading.BackgroundPatternColor.RGB = Color.FromArgb(unchecked((int)0xFF7BD3EA));
- var p1 = doc.Body.Paragraphs.Add(string.Format(basePara, 1), p1style);
- doc.Body.Paragraphs.Add(string.Format(sepPara, 1, 2));
- var p2style = doc.Styles.Add("Base-style-2", baseStyle);
- p2style.ParagraphFormat.Shading.BackgroundPatternColor.RGB = Color.FromArgb(unchecked((int)0xFFC7DB9C));
- var p2 = doc.Body.Paragraphs.Add(string.Format(basePara, 2), p2style);
- doc.Body.Paragraphs.Add(string.Format(sepPara, 2, 3));
- var p3style = doc.Styles.Add("Base-style-3", baseStyle);
- p3style.ParagraphFormat.Shading.BackgroundPatternColor.RGB = Color.FromArgb(unchecked((int)0xFFFFF08D));
- var p3 = doc.Body.Paragraphs.Add(string.Format(basePara, 3), p3style);
- doc.Body.Paragraphs.Add(string.Format(sepPara, 3, 4));
- var p4style = doc.Styles.Add("Base-style-4", baseStyle);
- p4style.ParagraphFormat.Shading.BackgroundPatternColor.RGB = Color.FromArgb(unchecked((int)0xFFFDAB9E));
- var p4 = doc.Body.Paragraphs.Add(string.Format(basePara, 4), p4style);
- doc.Body.Paragraphs.Add(string.Format(sepPara, 4, 5));
- var p5style = doc.Styles.Add("Base-style-5", baseStyle);
- p5style.ParagraphFormat.Shading.BackgroundPatternColor.RGB = Color.FromArgb(unchecked((int)0xFFCDC1FF));
- var p5 = doc.Body.Paragraphs.Add(string.Format(basePara, 5), p5style);
- doc.Body.Paragraphs.Add(string.Format(sepPara, 5, "end"));
- // Insert paragraphs with default style using different InsertLocation's:
- p1.GetRange().Paragraphs.Insert("Paragraph inserted before paragraph 1 via ", InsertLocation.Before).AddRun("\np1.GetRange().Paragraphs.Insert(..., InsertLocation.Before);", codeStyle);
- p2.GetRange().Paragraphs.Insert("Paragraph inserted at start of paragraph 2 via ", InsertLocation.Start).AddRun("\np2.GetRange().Paragraphs.Insert(..., InsertLocation.Start);", codeStyle);
- p3.GetRange().Paragraphs.Insert("Paragraph inserted at end of paragraph 3 via ", InsertLocation.End).AddRun("\np3.GetRange().Paragraphs.Insert(..., InsertLocation.End);", codeStyle);
- p4.GetRange().Paragraphs.Insert("Paragraph inserted after paragraph 4 via ", InsertLocation.After).AddRun("\np4.GetRange().Paragraphs.Insert(..., InsertLocation.After);", codeStyle);
- // Paragraphs.Add() is a shortcut to Insert(InsertLocation.End):
- p5.GetRange().Paragraphs.Add("Paragraph added after base paragraph 5 via ").AddRun("\np5.GetRange().Paragraphs.Add();", codeStyle);
- // Done:
- return doc;
- }
- }
- }