''
'' This code is part of Document Solutions for Word demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.Drawing
Imports System.IO
Imports GrapeCity.Documents.Word
Imports GrapeCity.Documents.Imaging
'' This sample demonstrates how to use basic elements
'' (text, borders, picture) to create a Word document in code.
Public Class SimpleDocument
Function CreateDocx() As GcWordDocument
Dim doc = New GcWordDocument()
Dim sec = doc.Body.Sections.First
Dim pars = sec.GetRange().Paragraphs
'' Title:
Dim title = pars.Add("Simple Word Document")
title.Style = doc.Styles(BuiltInStyleId.Title)
'' Heading:
Dim heading = pars.Add("Basic Elements")
heading.Style = doc.Styles(BuiltInStyleId.Heading1)
'' Text:
Dim p = pars.Add("This document is built using the following basic elements available through the ")
Dim run = p.GetRange().Runs.Add("DsWord")
run.Style = doc.Styles(BuiltInStyleId.Strong)
p.GetRange().Runs.Add(" object model:")
'' List:
Dim listTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "myListTemplate")
'' Add bullet items
Dim addListItem As Action(Of String) =
Sub(t_)
Dim p_ = pars.Add(t_)
p_.ListFormat.Template = listTemplate
p_.Style = doc.Styles(BuiltInStyleId.ListParagraph)
End Sub
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:
Dim picBytes = File.ReadAllBytes(Path.Combine("Resources", "Images", "road.jpg"))
'' Create a GcBitmap so that we can find out the native picture size:
Dim bmp = New GcBitmap(picBytes)
'' Scale picture to fill 80% of the page width:
Dim width = doc.Body.Sections.Last.PageSetup.ClientWidth * 0.8F
Dim height = bmp.Height * (width / bmp.Width)
Dim 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:
Dim font = "Arial"
Dim hicolor = Color.SteelBlue
Dim bcolor = Color.Coral
'' Title:
Dim sTitle = doc.Styles(BuiltInStyleId.Title)
Dim 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:
Dim 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:
Dim sNormal = doc.Styles(BuiltInStyleId.Normal)
sNormal.Font.Name = font
sNormal.Font.Size = 12
'' Strong text:
Dim sStrong = doc.Styles(BuiltInStyleId.Strong)
sStrong.Font.Color.RGB = hicolor
sStrong.Font.Italic = True
'' Done
Return doc
End Function
End Class