ProcurementLetter.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.Collections.Generic;
  9. using System.Linq;
  10. using GrapeCity.Documents.Word;
  11. using Range = GrapeCity.Documents.Word.Range;
  12.  
  13. namespace DsWordWeb.Demos
  14. {
  15. // This sample builds a nice-looking procurement business letter imports tables to create the layout.
  16. public class ProcurementLetter
  17. {
  18. public GcWordDocument CreateDocx()
  19. {
  20. var doc = new GcWordDocument();
  21. doc.Styles.DefaultFont.Size = 12;
  22. doc.Styles.DefaultParagraphFormat.Spacing.SpaceAfter = 0;
  23. doc.Styles[BuiltInStyleId.Normal].Font.Name = "Times New Roman";
  24. doc.Styles[BuiltInStyleId.Normal].Font.Size = 10;
  25.  
  26. var sec = doc.Body.Sections.First;
  27. sec.PageSetup.Size.PaperSize = PaperSize.PaperA4;
  28. var footer = sec.Footers[HeaderFooterType.Primary];
  29. var p = footer.Body.Paragraphs.Add("Residential Homes | Commercial Properties | Investment Real Estate | Financing Specialists");
  30. p.Format.Alignment = ParagraphAlignment.Center;
  31. p.GetRange().Runs.First.Font.Size = 8.5f;
  32.  
  33. var sr = sec.GetRange();
  34. var t = sr.Tables.Add(1, 1);
  35. t.Format.Alignment = TableAlignment.Right;
  36. t.Format.TableGrid.Add(216);
  37. t.Format.TableGrid.Add(126);
  38.  
  39. var c = t[0, 0];
  40. var cf = c.Format;
  41. cf.GridSpan = 2;
  42. cf.PreferredWidth.Type = PreferredWidthType.Points;
  43. cf.PreferredWidth.Value = 342;
  44. cf.Shading.Texture = TexturePattern.Clear;
  45. cf.Shading.ForegroundPatternColor.RGB = System.Drawing.Color.Empty;
  46. cf.Shading.BackgroundPatternColor.RGB = System.Drawing.Color.FromArgb(255, 59, 19, 110);
  47. cf.VerticalAlignment = CellVerticalAlignment.Center;
  48.  
  49. var cp = cf.Padding;
  50. cp.Top = 12;
  51. cp.Left = 12;
  52. cp.Bottom = 24;
  53. cp.Right = 24;
  54.  
  55. var cr = c.GetRange();
  56. p = cr.Paragraphs.First;
  57. p.Format.Alignment = ParagraphAlignment.Right;
  58. var f = p.Mark.Font;
  59. f.Color.RGB = System.Drawing.Color.Empty;
  60. f.Name = "Arial";
  61. f.Size = 14;
  62.  
  63. var r = p.GetRange().Runs.Add("Nancy Davolio");
  64. f = r.Font;
  65. f.Color.RGB = System.Drawing.Color.Empty;
  66. f.Name = "Arial";
  67. f.Size = 14;
  68.  
  69. p = cr.Paragraphs.Add("Chief Procurement Officer");
  70. p.Format.Alignment = ParagraphAlignment.Right;
  71. f = p.Mark.Font;
  72. f.Color.RGB = System.Drawing.Color.Empty;
  73. f.Name = "Arial";
  74.  
  75. r = p.GetRange().Runs.First;
  76. f = r.Font;
  77. f.Color.RGB = System.Drawing.Color.Empty;
  78. f.Name = "Arial";
  79.  
  80. var row = t.Rows.Add();
  81. row.Format.Height = 92.4f;
  82.  
  83. c = row.Cells.Add();
  84. cf = c.Format;
  85. cf.PreferredWidth.Type = PreferredWidthType.Points;
  86. cf.PreferredWidth.Value = 216;
  87. cf.Shading.Texture = TexturePattern.Clear;
  88. cf.Shading.ForegroundPatternColor.RGB = System.Drawing.Color.Empty;
  89. cf.Shading.BackgroundPatternColor.RGB = System.Drawing.Color.FromArgb(255, 242, 242, 242);
  90. cf.Padding.Top = 12;
  91.  
  92. cr = c.GetRange();
  93. p = cr.Paragraphs.First;
  94. f = p.Mark.Font;
  95. f.Color.RGB = System.Drawing.Color.FromArgb(255, 196, 89, 17);
  96. f.Color.ThemeColor = ThemeColorId.Accent2;
  97. f.Color.ThemeShade = 191;
  98. f.Name = "Arial";
  99.  
  100. r = p.GetRange().Runs.Add("555-543-5432");
  101. f = r.Font;
  102. f.Color.RGB = System.Drawing.Color.FromArgb(255, 196, 89, 17);
  103. f.Color.ThemeColor = ThemeColorId.Accent2;
  104. f.Color.ThemeShade = 191;
  105. f.Name = "Arial";
  106.  
  107. p = cr.Paragraphs.Add("www.acmeinc.com");
  108. p.Mark.Style = doc.Styles[BuiltInStyleId.Hyperlink];
  109. p.Mark.Font.Name = "Arial";
  110.  
  111. r = p.GetRange().Runs.First;
  112. r.Style = doc.Styles[BuiltInStyleId.Hyperlink];
  113. r.Font.Name = "Arial";
  114.  
  115. p = cr.Paragraphs.Add("5432 Street West, Townsvilla, State 54321");
  116. p.Mark.Font.Name = "Arial";
  117. p.GetRange().Runs.First.Font.Name = "Arial";
  118.  
  119. c = row.Cells.Add();
  120. cf = c.Format;
  121. cf.PreferredWidth.Type = PreferredWidthType.Points;
  122. cf.PreferredWidth.Value = 126;
  123. cf.Padding.Bottom = 0;
  124. cf.VerticalAlignment = CellVerticalAlignment.Center;
  125.  
  126. p = c.GetRange().Paragraphs.First;
  127. p.Format.Alignment = ParagraphAlignment.Center;
  128. r = p.GetRange().Runs.Add();
  129.  
  130. var bytes = File.ReadAllBytes(Path.Combine("Resources", "ImagesBis", "nancy.png"));
  131. var pic = r.GetRange().Pictures.Add(bytes, "image/png");
  132. pic.Size.Height.Value = 96;
  133. pic.Size.Width.Value = 111.75f;
  134.  
  135. p = sr.Paragraphs.Add();
  136. r = p.GetRange().Runs.Add();
  137.  
  138. bytes = File.ReadAllBytes(Path.Combine("Resources", "ImagesBis", "acme.png"));
  139. pic = r.GetRange().Pictures.Add(bytes, "image/png");
  140. pic.Size.Height.Value = 63;
  141. pic.Size.Width.Value = 93.75f;
  142. var wf = pic.WrapFormat;
  143. wf.Type = WrapType.Square;
  144. wf.Side = WrapSide.Right;
  145. wf.DistanceLeft = 9;
  146. wf.DistanceRight = 9;
  147.  
  148. var hp = pic.Position.Horizontal;
  149. hp.Type = ShapePositionType.Points;
  150. hp.RelativeTo = ShapeHorizontalRelativePosition.Column;
  151. hp.Offset = 6;
  152.  
  153. var vp = pic.Position.Vertical;
  154. vp.Type = ShapePositionType.Points;
  155. vp.RelativeTo = ShapeVerticalRelativePosition.Paragraph;
  156. vp.Offset = -170.85f;
  157.  
  158. AddPara(sr, null);
  159. AddPara(sr, "Dear Mark,");
  160. AddPara(sr, null);
  161. AddPara(sr, "The first shipment of equipment from AMA Ltd has arrived. We are delighted with every piece. Therefore, we decided to make our initial purchase larger than anticipated. I am attaching our purchase order No. 8393 for additional goods. ");
  162. AddPara(sr, null);
  163. AddPara(sr, "Since you already have a copy of our Procurement Guidelines, I shall not attach them to this order. Please inform me of shipping dates.");
  164. AddPara(sr, null);
  165. AddPara(sr, "Sincerely,");
  166. AddPara(sr, "Nancy Davolio,");
  167. AddPara(sr, "Chief Procurement Officer");
  168.  
  169. return doc;
  170. }
  171.  
  172. private void AddPara(Range rng, string text)
  173. {
  174. Paragraph p = null;
  175. if (text != null)
  176. p = rng.Paragraphs.Add(text);
  177. else
  178. p = rng.Paragraphs.Add();
  179.  
  180. p.Format.Spacing.LineSpacing = 1.3f;
  181. p.Mark.Font.Name = "Arial";
  182. p.Mark.Font.Size = 12;
  183.  
  184. if (text != null)
  185. {
  186. Run r = p.GetRange().Runs.First;
  187. r.Font.Name = "Arial";
  188. r.Font.Size = 12;
  189. }
  190. }
  191.  
  192. }
  193. }
  194.