SimpleDocument.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.Drawing;
  7. using System.IO;
  8. using GrapeCity.Documents.Word;
  9. using GrapeCity.Documents.Imaging;
  10.  
  11. namespace DsWordWeb.Demos
  12. {
  13. // This sample demonstrates how to use basic elements
  14. // (text, borders, picture) to create a Word document in code.
  15. public class SimpleDocument
  16. {
  17. public GcWordDocument CreateDocx()
  18. {
  19. var doc = new GcWordDocument();
  20. var sec = doc.Body.Sections.First;
  21. var pars = sec.GetRange().Paragraphs;
  22.  
  23. // Title:
  24. var title = pars.Add("Simple Word Document");
  25. title.Style = doc.Styles[BuiltInStyleId.Title];
  26.  
  27. // Heading:
  28. var heading = pars.Add("Basic Elements");
  29. heading.Style = doc.Styles[BuiltInStyleId.Heading1];
  30.  
  31. // Text:
  32. var p = pars.Add("This document is built using the following basic elements available through the ");
  33. var run = p.GetRange().Runs.Add("DsWord");
  34. run.Style = doc.Styles[BuiltInStyleId.Strong];
  35. p.GetRange().Runs.Add(" object model:");
  36.  
  37. // List:
  38. var listTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "myListTemplate");
  39.  
  40. // Add bullet items:
  41. Action<string> addListItem = t_ => {
  42. var p_ = pars.Add(t_);
  43. p_.ListFormat.Template = listTemplate;
  44. p_.Style = doc.Styles[BuiltInStyleId.ListParagraph];
  45. };
  46. addListItem("Text and text runs");
  47. addListItem("Paragraphs");
  48. addListItem("List templates");
  49. addListItem("Borders");
  50. addListItem("Pictures");
  51. addListItem("Styles");
  52. pars.Add().Style = doc.Styles[BuiltInStyleId.Normal];
  53.  
  54. // Add picture:
  55. var picBytes = File.ReadAllBytes(Path.Combine("Resources", "Images", "road.jpg"));
  56. // Create a GcBitmap so that we can find out the native picture size:
  57. var bmp = new GcBitmap(picBytes);
  58. // Scale picture to fill 80% of the page width:
  59. var width = doc.Body.Sections.Last.PageSetup.ClientWidth * 0.8f;
  60. var height = bmp.Height * (width / bmp.Width);
  61. var pic = pars.Add().GetRange().Runs.Add().GetRange().Pictures.Add(picBytes, "image/jpeg");
  62. pic.Size.Width.Value = width;
  63. pic.Size.Height.Value = height;
  64. pic.WrapFormat.Type = WrapType.TopBottom;
  65. pic.Position.Horizontal.Type = ShapePositionType.Alignment;
  66. pic.Position.Horizontal.Alignment = ShapeHorizontalRelativeAlignment.Center;
  67.  
  68. // Adjust styles:
  69. var font = "Arial";
  70. var hicolor = Color.SteelBlue;
  71. var bcolor = Color.Coral;
  72.  
  73. // Title:
  74. var sTitle = doc.Styles[BuiltInStyleId.Title];
  75. var bb = sTitle.ParagraphFormat.Borders.Bottom;
  76. bb.LineStyle = LineStyle.Single;
  77. bb.LineWidth = 4;
  78. bb.Color.RGB = bcolor;
  79. bb.Space = 10;
  80.  
  81. sTitle.Font.Color.RGB = hicolor;
  82. sTitle.Font.Name = font;
  83. sTitle.Font.Bold = true;
  84. sTitle.Font.Size = 30;
  85.  
  86. // Heading 1:
  87. var sHeading1 = doc.Styles[BuiltInStyleId.Heading1];
  88. sHeading1.Font.Color.RGB = hicolor;
  89. sHeading1.Font.Name = font;
  90. sHeading1.Font.Bold = true;
  91. sHeading1.ParagraphFormat.Spacing.SpaceAfter = 8;
  92.  
  93. // Normal text:
  94. var sNormal = doc.Styles[BuiltInStyleId.Normal];
  95. sNormal.Font.Name = font;
  96. sNormal.Font.Size = 12;
  97.  
  98. // Strong text:
  99. var sStrong = doc.Styles[BuiltInStyleId.Strong];
  100. sStrong.Font.Color.RGB = hicolor;
  101. sStrong.Font.Italic = true;
  102.  
  103. // Done:
  104. return doc;
  105. }
  106. }
  107. }
  108.