SimpleDocument.cs
//
// This code is part of Document Solutions for Word demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.Drawing;
using System.IO;
using GrapeCity.Documents.Word;
using GrapeCity.Documents.Imaging;

namespace DsWordWeb.Demos
{
    // This sample demonstrates how to use basic elements
    // (text, borders, picture) to create a Word document in code.
    public class SimpleDocument
    {
        public GcWordDocument CreateDocx()
        {
            var doc = new GcWordDocument();
            var sec = doc.Body.Sections.First;
            var pars = sec.GetRange().Paragraphs;

            // Title:
            var title = pars.Add("Simple Word Document");
            title.Style = doc.Styles[BuiltInStyleId.Title];

            // Heading:
            var heading = pars.Add("Basic Elements");
            heading.Style = doc.Styles[BuiltInStyleId.Heading1];

            // Text:
            var p = pars.Add("This document is built using the following basic elements available through the ");
            var run = p.GetRange().Runs.Add("DsWord");
            run.Style = doc.Styles[BuiltInStyleId.Strong];
            p.GetRange().Runs.Add(" object model:");

            // List:
            var listTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "myListTemplate");

            // Add bullet items:
            Action<string> addListItem = t_ => {
                var p_ = pars.Add(t_);
                p_.ListFormat.Template = listTemplate;
                p_.Style = doc.Styles[BuiltInStyleId.ListParagraph];
            };
            addListItem("Text and text runs");
            addListItem("Paragraphs");
            addListItem("List templates");
            addListItem("Borders");
            addListItem("Pictures");
            addListItem("Styles");
            pars.Add().Style = doc.Styles[BuiltInStyleId.Normal];

            // Add picture:
            var picBytes = File.ReadAllBytes(Path.Combine("Resources", "Images", "road.jpg"));
            // Create a GcBitmap so that we can find out the native picture size:
            var bmp = new GcBitmap(picBytes);
            // Scale picture to fill 80% of the page width:
            var width = doc.Body.Sections.Last.PageSetup.ClientWidth * 0.8f;
            var height = bmp.Height * (width / bmp.Width);
            var pic = pars.Add().GetRange().Runs.Add().GetRange().Pictures.Add(picBytes, "image/jpeg");
            pic.Size.Width.Value = width;
            pic.Size.Height.Value = height;
            pic.WrapFormat.Type = WrapType.TopBottom;
            pic.Position.Horizontal.Type = ShapePositionType.Alignment;
            pic.Position.Horizontal.Alignment = ShapeHorizontalRelativeAlignment.Center;

            // Adjust styles:
            var font = "Arial";
            var hicolor = Color.SteelBlue;
            var bcolor = Color.Coral;

            // Title:
            var sTitle = doc.Styles[BuiltInStyleId.Title];
            var bb = sTitle.ParagraphFormat.Borders.Bottom;
            bb.LineStyle = LineStyle.Single;
            bb.LineWidth = 4;
            bb.Color.RGB = bcolor;
            bb.Space = 10;

            sTitle.Font.Color.RGB = hicolor;
            sTitle.Font.Name = font;
            sTitle.Font.Bold = true;
            sTitle.Font.Size = 30;

            // Heading 1:
            var sHeading1 = doc.Styles[BuiltInStyleId.Heading1];
            sHeading1.Font.Color.RGB = hicolor;
            sHeading1.Font.Name = font;
            sHeading1.Font.Bold = true;
            sHeading1.ParagraphFormat.Spacing.SpaceAfter = 8;

            // Normal text:
            var sNormal = doc.Styles[BuiltInStyleId.Normal];
            sNormal.Font.Name = font;
            sNormal.Font.Size = 12;

            // Strong text:
            var sStrong = doc.Styles[BuiltInStyleId.Strong];
            sStrong.Font.Color.RGB = hicolor;
            sStrong.Font.Italic = true;

            // Done:
            return doc;
        }
    }
}