ProcurementLetter.cs
//
// 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.Collections.Generic;
using System.Linq;
using GrapeCity.Documents.Word;
using Range = GrapeCity.Documents.Word.Range;

namespace DsWordWeb.Demos
{
    // This sample builds a nice-looking procurement business letter imports tables to create the layout.
    public class ProcurementLetter
    {
        public GcWordDocument CreateDocx()
        {
            var doc = new GcWordDocument();
            doc.Styles.DefaultFont.Size = 12;
            doc.Styles.DefaultParagraphFormat.Spacing.SpaceAfter = 0;
            doc.Styles[BuiltInStyleId.Normal].Font.Name = "Times New Roman";
            doc.Styles[BuiltInStyleId.Normal].Font.Size = 10;

            var sec = doc.Body.Sections.First;
            sec.PageSetup.Size.PaperSize = PaperSize.PaperA4;
            var footer = sec.Footers[HeaderFooterType.Primary];
            var p = footer.Body.Paragraphs.Add("Residential Homes    |    Commercial Properties    |    Investment Real Estate    |    Financing Specialists");
            p.Format.Alignment = ParagraphAlignment.Center;
            p.GetRange().Runs.First.Font.Size = 8.5f;

            var sr = sec.GetRange();
            var t = sr.Tables.Add(1, 1);
            t.Format.Alignment = TableAlignment.Right;
            t.Format.TableGrid.Add(216);
            t.Format.TableGrid.Add(126);

            var c = t[0, 0];
            var cf = c.Format;
            cf.GridSpan = 2;
            cf.PreferredWidth.Type = PreferredWidthType.Points;
            cf.PreferredWidth.Value = 342;
            cf.Shading.Texture = TexturePattern.Clear;
            cf.Shading.ForegroundPatternColor.RGB = System.Drawing.Color.Empty;
            cf.Shading.BackgroundPatternColor.RGB = System.Drawing.Color.FromArgb(255, 59, 19, 110);
            cf.VerticalAlignment = CellVerticalAlignment.Center;

            var cp = cf.Padding;
            cp.Top = 12;
            cp.Left = 12;
            cp.Bottom = 24;
            cp.Right = 24;

            var cr = c.GetRange();
            p = cr.Paragraphs.First;
            p.Format.Alignment = ParagraphAlignment.Right;
            var f = p.Mark.Font;
            f.Color.RGB = System.Drawing.Color.Empty;
            f.Name = "Arial";
            f.Size = 14;

            var r = p.GetRange().Runs.Add("Nancy Davolio");
            f = r.Font;
            f.Color.RGB = System.Drawing.Color.Empty;
            f.Name = "Arial";
            f.Size = 14;

            p = cr.Paragraphs.Add("Chief Procurement Officer");
            p.Format.Alignment = ParagraphAlignment.Right;
            f = p.Mark.Font;
            f.Color.RGB = System.Drawing.Color.Empty;
            f.Name = "Arial";

            r = p.GetRange().Runs.First;
            f = r.Font;
            f.Color.RGB = System.Drawing.Color.Empty;
            f.Name = "Arial";

            var row = t.Rows.Add();
            row.Format.Height = 92.4f;

            c = row.Cells.Add();
            cf = c.Format;
            cf.PreferredWidth.Type = PreferredWidthType.Points;
            cf.PreferredWidth.Value = 216;
            cf.Shading.Texture = TexturePattern.Clear;
            cf.Shading.ForegroundPatternColor.RGB = System.Drawing.Color.Empty;
            cf.Shading.BackgroundPatternColor.RGB = System.Drawing.Color.FromArgb(255, 242, 242, 242);
            cf.Padding.Top = 12;

            cr = c.GetRange();
            p = cr.Paragraphs.First;
            f = p.Mark.Font;
            f.Color.RGB = System.Drawing.Color.FromArgb(255, 196, 89, 17);
            f.Color.ThemeColor = ThemeColorId.Accent2;
            f.Color.ThemeShade = 191;
            f.Name = "Arial";

            r = p.GetRange().Runs.Add("555-543-5432");
            f = r.Font;
            f.Color.RGB = System.Drawing.Color.FromArgb(255, 196, 89, 17);
            f.Color.ThemeColor = ThemeColorId.Accent2;
            f.Color.ThemeShade = 191;
            f.Name = "Arial";

            p = cr.Paragraphs.Add("www.acmeinc.com");
            p.Mark.Style = doc.Styles[BuiltInStyleId.Hyperlink];
            p.Mark.Font.Name = "Arial";

            r = p.GetRange().Runs.First;
            r.Style = doc.Styles[BuiltInStyleId.Hyperlink];
            r.Font.Name = "Arial";

            p = cr.Paragraphs.Add("5432 Street West, Townsvilla, State 54321");
            p.Mark.Font.Name = "Arial";
            p.GetRange().Runs.First.Font.Name = "Arial";

            c = row.Cells.Add();
            cf = c.Format;
            cf.PreferredWidth.Type = PreferredWidthType.Points;
            cf.PreferredWidth.Value = 126;
            cf.Padding.Bottom = 0;
            cf.VerticalAlignment = CellVerticalAlignment.Center;

            p = c.GetRange().Paragraphs.First;
            p.Format.Alignment = ParagraphAlignment.Center;
            r = p.GetRange().Runs.Add();

            var bytes = File.ReadAllBytes(Path.Combine("Resources", "ImagesBis", "nancy.png"));
            var pic = r.GetRange().Pictures.Add(bytes, "image/png");
            pic.Size.Height.Value = 96;
            pic.Size.Width.Value = 111.75f;

            p = sr.Paragraphs.Add();
            r = p.GetRange().Runs.Add();

            bytes = File.ReadAllBytes(Path.Combine("Resources", "ImagesBis", "acme.png"));
            pic = r.GetRange().Pictures.Add(bytes, "image/png");
            pic.Size.Height.Value = 63;
            pic.Size.Width.Value = 93.75f;
            var wf = pic.WrapFormat;
            wf.Type = WrapType.Square;
            wf.Side = WrapSide.Right;
            wf.DistanceLeft = 9;
            wf.DistanceRight = 9;

            var hp = pic.Position.Horizontal;
            hp.Type = ShapePositionType.Points;
            hp.RelativeTo = ShapeHorizontalRelativePosition.Column;
            hp.Offset = 6;

            var vp = pic.Position.Vertical;
            vp.Type = ShapePositionType.Points;
            vp.RelativeTo = ShapeVerticalRelativePosition.Paragraph;
            vp.Offset = -170.85f;

            AddPara(sr, null);
            AddPara(sr, "Dear Mark,");
            AddPara(sr, null);
            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. ");
            AddPara(sr, null);
            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.");
            AddPara(sr, null);
            AddPara(sr, "Sincerely,");
            AddPara(sr, "Nancy Davolio,");
            AddPara(sr, "Chief Procurement Officer");

            return doc;
        }

        private void AddPara(Range rng, string text)
        {
            Paragraph p = null;
            if (text != null)
                p = rng.Paragraphs.Add(text);
            else
                p = rng.Paragraphs.Add();

            p.Format.Spacing.LineSpacing = 1.3f;
            p.Mark.Font.Name = "Arial";
            p.Mark.Font.Size = 12;

            if (text != null)
            {
                Run r = p.GetRange().Runs.First;
                r.Font.Name = "Arial";
                r.Font.Size = 12;
            }
        }

    }
}