InlineImages.vb
''
'' This code is part of Document Solutions for PDF demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Drawing
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing

'' This sample shows how to insert arbitrary objecs (images in this sample)
'' into a block of text so that those objects keep their positions relative
'' to the surrounding text, and are laid out exactly like other text runs,
'' and can participate in text flow.
Public Class InlineImages
    Function CreatePDF(ByVal stream As Stream) As Integer
        '' Get the images to use as inline objects:
        Using imgPuffins As GCDRAW.Image = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "puffins-small.jpg")),
            imgFerns As GCDRAW.Image = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "ferns-small.jpg"))
            '' The image alignment to use:
            Dim ia = New ImageAlign(ImageAlignHorz.Center, ImageAlignVert.Bottom, True, True, True, False, False)
            '' Create and set up the document:
            Dim doc = New GcPdfDocument()
            Dim page = doc.NewPage()
            Dim g = page.Graphics
            '' Create and set up a TextLayout object to print the text:
            Dim tl = g.CreateTextLayout()
            tl.MaxWidth = page.Size.Width
            tl.MaxHeight = page.Size.Height
            tl.MarginLeft = 36
            tl.MarginRight = 36
            tl.MarginTop = 36
            tl.MarginBottom = 36
            tl.DefaultFormat.Font = StandardFonts.Times
            tl.DefaultFormat.FontSize = 12
            tl.DefaultFormat.BackColor = Color.LightGoldenrodYellow
            tl.TextAlignment = TextAlignment.Justified
            '' Create inline objects using the images and arbitrary sizes:
            Dim ioPuffins = New InlineObject(imgPuffins, 36, 24)
            Dim ioFerns = New InlineObject(imgFerns, 36, 24)
            '' Build up the text:
            tl.Append("The 'Inline objects' feature of the TextLayout class allows inserting arbitrary objects" +
                "into a block of text. Those objects are then treated exactly like other text runs, " +
                "and keep their positions relative to the surrounding text. " +
                "In this sample, we insert some images into the text as inline objects, " +
                "use the TextLayout class to position them along with text, and draw them " +
                "using the GcGraphics.DrawImage method.")
            tl.Append("Here are some puffins:")
            tl.Append(ioPuffins)
            tl.Append("and here are some ferns:")
            tl.Append(ioFerns)
            tl.Append("The end.")
            ''
            Debug.Assert(tl.InlineObjects.Count = 0, "InlineObjects is filled by RecalculateGlyphs")
            '' This method fetches and measures the glyphs needed to render the text,
            '' because we draw the same text a few times with different layout,
            '' we call this once before the loop below:
            tl.RecalculateGlyphs()
            ''
            Debug.Assert(tl.InlineObjects.Count = 2, "InlineObjects is filled by RecalculateGlyphs")
            '' In a loop, draw the text and inline images in 3 different locations
            '' and bounds on the page:
            For i = 0 To 2
                tl.MarginTop = tl.ContentRectangle.Bottom + 36
                tl.MarginLeft = 36 + 72 * i
                tl.MarginRight = 36 + 72 * i
                '' Note passing 'false' here, we do not need to recalc the glyphs because
                '' the text has not changed:
                tl.PerformLayout(False)
                '' Draw the text and images:
                g.DrawTextLayout(tl, PointF.Empty)
                For Each inlineObj In tl.InlineObjects
                    g.DrawImage(CType(inlineObj.Object, GCDRAW.Image), inlineObj.ObjectRect.ToRectangleF(), Nothing, ia)
                Next
            Next
            ''
            '' Done:
            doc.Save(stream)
            Return doc.Pages.Count
        End Using
    End Function
End Class