PictureWrap.vb
''
'' This code is part of GrapeCity Documents for Word samples.
'' Copyright (c) GrapeCity, Inc. All rights reserved.
''
Imports System.IO
Imports System.Linq
Imports GrapeCity.Documents.Word
Imports GrapeCity.Documents.Imaging

'' This sample adds a paragraph of text, spllits it in the middle,
'' And inserts a picture into the split position.
'' This sequence Is repeated several times with the same text And picture,
'' but with different picture wrapping styles And alignments.
Public Class PictureWrap
    Function CreateDocx() As GcWordDocument
        Dim doc = New GcWordDocument()
        doc.Styles.DefaultParagraphFormat.Alignment = ParagraphAlignment.Both

        '' Load picture data:
        Dim picBytes = File.ReadAllBytes(Path.Combine("Resources", "Images", "road.jpg"))
        '' Create a GcBitmap so that we can find out the native picture size:
        Dim image = New GcBitmap(picBytes)
        '' Scale picture to a smallish size:
        Dim width = doc.Body.Sections.Last.PageSetup.ClientWidth / 6
        Dim height = image.Height * (width / image.Width)

        '' The section
        Dim sec = doc.Body.Sections.First
        '' The paragraphs collection:
        Dim pars = sec.GetRange().Paragraphs
        '' Generate a sample string (used in all sample paragraphs):
        Dim str = LoremIpsumPar(10, 10, 10, 10)
        '' Header:
        pars.Add("Different picture wrapping styles and alignments.", doc.Styles(BuiltInStyleId.Heading1))

        '' Function to insert a sized picture into the middle of a paragraph
        Dim addPicture As Func(Of String, Picture) =
            Function(caption_)
                pars.Add(caption_, doc.Styles(BuiltInStyleId.Heading2))
                Dim par_ = pars.Add(str)
                Dim text_ = par_.GetRange().Texts.First.Split(str.Length / 2)
                Dim run_ = text_.ParentRun.Split(text_, InsertLocation.Before)
                run_ = run_.Previous
                Dim pic_ = run_.GetRange().Pictures.Add(picBytes, "image/jpeg")
                pic_.Size.Width.Value = width
                pic_.Size.Height.Value = height
                Return pic_
            End Function

        '' Add picture using different wrapping styles And alignments:
        Dim pic = addPicture("Wrapping style 'In line with text':")

        pic = addPicture("Wrapping style 'Square', centered:")
        pic.WrapFormat.Type = WrapType.Square
        pic.Position.Horizontal.Type = ShapePositionType.Alignment
        pic.Position.Horizontal.Alignment = ShapeHorizontalRelativeAlignment.Center

        pic = addPicture("Wrapping style 'Tight', centered:")
        pic.WrapFormat.Type = WrapType.Tight
        pic.Position.Horizontal.Type = ShapePositionType.Alignment
        pic.Position.Horizontal.Alignment = ShapeHorizontalRelativeAlignment.Center

        '' Add page break
        sec.GetRange().Runs.Last.GetRange().Texts.AddBreak(BreakType.Page)

        pic = addPicture("Wrapping style 'Through', left aligned:")
        pic.WrapFormat.Type = WrapType.Through
        pic.Position.Horizontal.Type = ShapePositionType.Alignment
        pic.Position.Horizontal.Alignment = ShapeHorizontalRelativeAlignment.Left

        pic = addPicture("Wrapping style 'Top and Bottom', right aligned:")
        pic.WrapFormat.Type = WrapType.TopBottom
        pic.Position.Horizontal.Type = ShapePositionType.Alignment
        pic.Position.Horizontal.Alignment = ShapeHorizontalRelativeAlignment.Right

        pic = addPicture("Wrapping style 'Behind text', left aligned:")
        pic.WrapFormat.BehindText = True
        pic.WrapFormat.Type = WrapType.None
        pic.Position.Horizontal.Type = ShapePositionType.Alignment
        pic.Position.Horizontal.Alignment = ShapeHorizontalRelativeAlignment.Left

        '' Add page break
        sec.GetRange().Runs.Last.GetRange().Texts.AddBreak(BreakType.Page)

        pic = addPicture("Wrapping style 'In front of text', right aligned:")
        pic.WrapFormat.BehindText = False
        pic.WrapFormat.Type = WrapType.None
        pic.Position.Horizontal.Type = ShapePositionType.Alignment
        pic.Position.Horizontal.Alignment = ShapeHorizontalRelativeAlignment.Right

        pars.Add("The End.")

        '' Done
        Return doc
    End Function
End Class