ProcurementLetter.vb
''
'' This code is part of Document Solutions for Word demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Word

'' This sample builds a nice-looking procurement business letter using tables to create the layout.
Public Class ProcurementLetter
    Function CreateDocx() As GcWordDocument
        Dim 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

        Dim sec = doc.Body.Sections.First
        sec.PageSetup.Size.PaperSize = PaperSize.PaperA4
        Dim footer = sec.Footers(HeaderFooterType.Primary)
        Dim 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

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

        Dim c = t(0, 0)
        Dim 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

        Dim cp = cf.Padding
        cp.Top = 12
        cp.Left = 12
        cp.Bottom = 24
        cp.Right = 24

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

        Dim r = p.GetRange().Runs.Add("Nancy Davolio")
        f = r.Font
        f.Color.RGB = 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 = Color.Empty
        f.Name = "Arial"

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

        Dim 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()

        Dim bytes = File.ReadAllBytes(Path.Combine("Resources", "ImagesBis", "nancy.png"))
        Dim 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
        Dim wf = pic.WrapFormat
        wf.Type = WrapType.Square
        wf.Side = WrapSide.Right
        wf.DistanceLeft = 9
        wf.DistanceRight = 9

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

        Dim vp = pic.Position.Vertical
        vp.Type = ShapePositionType.Points
        vp.RelativeTo = ShapeVerticalRelativePosition.Paragraph
        vp.Offset = -170.85F

        AddPara(sr, Nothing)
        AddPara(sr, "Dear Mark,")
        AddPara(sr, Nothing)
        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, Nothing)
        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, Nothing)
        AddPara(sr, "Sincerely,")
        AddPara(sr, "Nancy Davolio,")
        AddPara(sr, "Chief Procurement Officer")
        '' Done:
        Return doc
    End Function

    Private Sub AddPara(ByVal rng As Range, ByVal text As String)
        Dim p As Paragraph = Nothing
        If text IsNot Nothing Then
            p = rng.Paragraphs.Add(text)
        Else
            p = rng.Paragraphs.Add()
        End If
        p.Format.Spacing.LineSpacing = 1.3F
        p.Mark.Font.Name = "Arial"
        p.Mark.Font.Size = 12
        If text IsNot Nothing Then
            Dim r = p.GetRange().Runs.First
            r.Font.Name = "Arial"
            r.Font.Size = 12
        End If
    End Sub
End Class