Wetlands.vb
'' 完毕:
Imports System.IO
Imports System.Drawing
Imports System.Collections.Generic
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Drawing
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing

'' 该示例生成一个文本文档,其中包含一些图像和文本突出显示,
'' 使用 TextLayout 类来排列文本和图像。
Public Class Wetlands
    '' 主要示例驱动程序。
    Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()

        '' 这将保存第一张图像,以便我们可以在保存文档后处理它们:
        Dim disposables As New Generic.List(Of IDisposable)

        '' 页脚:
        Dim ftrImg = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "logo-GC-devsol.png"))
        disposables.Add(ftrImg)
        Dim fx = ftrImg.HorizontalResolution / 72
        Dim fy = ftrImg.VerticalResolution / 72
        Dim ftrRc = New RectangleF(
                doc.PageSize.Width / 2 - ftrImg.Width / fx / 2,
                doc.PageSize.Height - 40,
                ftrImg.Width / fx,
                ftrImg.Height / fy)

        Dim addFtr As Action =
            Sub()
                doc.Pages.Last.Graphics.DrawImage(ftrImg, ftrRc, Nothing, ImageAlign.StretchImage)
            End Sub

        '' 标题颜色:
        Dim colorBlue = Color.FromArgb(&H3B, &H5C, &HAA)
        '' 亮点颜色:
        Dim colorRed = Color.Red
        '' 用于呈现文本的文本布局:
        Dim tl = New TextLayout(72) With {
            .MaxWidth = doc.PageSize.Width,
            .MaxHeight = doc.PageSize.Height,
            .MarginLeft = 72,
            .MarginRight = 72,
            .MarginTop = 72,
            .MarginBottom = 72
        }
        tl.DefaultFormat.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf"))
        tl.DefaultFormat.FontSize = 11

        Dim page = doc.NewPage()
        addFtr()

        Dim g = page.Graphics

        '' 标题:
        tl.TextAlignment = TextAlignment.Center
        tl.Append("Introduction" + vbCrLf, New TextFormat() With {.FontSize = 16, .ForeColor = colorBlue})
        tl.Append("The Importance of Wetlands", New TextFormat() With {.FontSize = 13, .ForeColor = colorBlue})
        tl.PerformLayout(True)
        g.DrawTextLayout(tl, PointF.Empty)

        '' 移至第一段标题下方:
        tl.MarginTop = tl.ContentHeight + 72 * 2
        tl.Clear()
        tl.TextAlignment = TextAlignment.Leading
        tl.ParagraphSpacing = 12

        Dim addPara As Action(Of String) =
            Sub(para)
                '' 我们实现一个原始标记来用红色突出显示一些片段:
                Dim txt = para.Split(New String() {"<red>", "</red>"}, StringSplitOptions.None)
                For i = 0 To txt.Length - 1
                    If i Mod 2 = 0 Then
                        tl.Append(txt(i))
                    Else
                        tl.Append(txt(i), New TextFormat(tl.DefaultFormat) With {.ForeColor = colorRed})
                    End If
                Next
                tl.AppendLine()
            End Sub

        '' 对于第一段,我们想要一个更大的首字母,但没有首行缩进,
        '' 所以我们将其与文本的其余部分分开渲染:
        tl.Append(_paras(0).Substring(0, 1), New TextFormat(tl.DefaultFormat) With {.FontSize = 22})
        addPara(_paras(0).Substring(1))
        tl.PerformLayout(True)
        g.DrawTextLayout(tl, PointF.Empty)

        '' 考虑第一段,并设置文本布局
        '' 对于文本的其余部分(TextLayout 允许您呈现多个段落,
        '' 但它们都必须具有相同的段落格式):
        tl.MarginTop = tl.ContentRectangle.Bottom
        tl.Clear()
        tl.FirstLineIndent = 36

        '' 添加剩余段落:
        For Each para In _paras.Skip(1)
            '' 以“::”开头的段落表示要在页面宽度上呈现的图像:
            If (para.StartsWith("::")) Then
                Dim img = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", para.Substring(2)))
                disposables.Add(img)
                Dim w = tl.MaxWidth.Value - tl.MarginLeft - tl.MarginRight
                Dim h = img.Height / img.Width * w
                tl.AppendInlineObject(img, w, h)
                tl.AppendLine()
            Else
                addPara(para)
            End If
        Next
        '' 安排段落:
        tl.PerformLayout(True)
        '' 文本分割选项允许您实现寡妇和孤儿控制:
        Dim tso = New TextSplitOptions(tl) With {
            .RestMarginTop = 72,
            .MinLinesInFirstParagraph = 2,
            .MinLinesInLastParagraph = 2
        }
        '' 用于渲染图片的图像对齐:
        Dim ia = New ImageAlign(ImageAlignHorz.Left, ImageAlignVert.Top, True, True, True, False, False) With {.BestFit = True}
        '' 在循环中,分割并渲染文本:
        While (True)
            Dim rest As TextLayout = Nothing
            Dim splitResult = tl.Split(tso, rest)
            g = doc.Pages.Last.Graphics
            doc.Pages.Last.Graphics.DrawTextLayout(tl, PointF.Empty)
            '' 渲染此页面上出现的所有图像:
            For Each inlobj In tl.InlineObjects
                doc.Pages.Last.Graphics.DrawImage(DirectCast(inlobj.Object, GCDRAW.Image), inlobj.ObjectRect.ToRectangleF(), Nothing, ia)
            Next
            '' 除非有更多内容要渲染,否则中断:
            If splitResult <> SplitResult.Split Then
                Exit While
            End If
            '' 将剩余文本分配给“主”TextLayout,添加新页面并继续:
            tl = rest
            doc.Pages.Add()
            addFtr()
        End While

        '' 保存 PDF:
        doc.Save(stream)
        '' 处理图像(只能在保存文档后进行):
        disposables.ForEach(Sub(d_) d_.Dispose())
        '' 完毕:
        Return doc.Pages.Count
    End Function

    '' 要呈现的文本段落的列表。
    '' 笔记:
    '' - 如果段落以“::”开头,则字符串的其余部分是要插入的图像文件的名称;
    '' - <red>..</red> 标记要突出显示的文本。
    Shared _paras() As String =
        {
            "Originally there were in excess of <red>two point three (2.3) million</red> hectares of wetlands in southern Ontario. Today there is a mere <red>twelve percent (12%)</red> remaining (Rowntree 1979). Yet, these same areas are vital to the continued existence of a whole host of wildlife species. Grebes,herons, bitterns, rails, shorebirds, gulls, terns, and numerous smaller birds, plus the waterfowl, nest in or use wetlands for feeding and resting. About <red>ninety-five percent (95%)</red> of all furbearers are taken in water (Rowntree 1979). Reptiles and amphibians must return there to breed. ",
            "::Birdswetland.jpg",
            "Several species of game fish live or spawn in wetlands. Hundreds, if not thousands, of invertebrates that form the food of birds also rely on water for most, if not all, phases of their existence. In fact, most all species of animals we have must spend at least part of the year in wetlands. To lose any more of these vital areas is almost unthinkable.",
            "Wetlands enhance and protect water quality in lakes and streams where additional species spend their time and from which we draw our water. Water from drainage may have five (5) times more phosphates or as much as fifty (50) times more nitrates than water from marshes. These nutrient loads act as fertilizers to aquatic plants whose growth may clog rivers, foul shorelines and deplete oxygen in the water making it unsuitable for fish. Wetlands handle as much as <red>fifty percent (50%)</red> of terrestrial denitrification whereby nitrogen is returned to the atmosphere. Wetlands act as settling and filtration basins collecting silt that might build up behind dams or clog navigation channels. Vegetation in wetlands protects shorelines from damage by tides and storms. Wetlands soak up tremendous amounts of rainwater, slowing runoff and decreasing flooding that will help to decrease erosion of streambanks and prevent property damage. Water maintained in wetlands also helps to maintain ground water levels.",
            "Wetlands provide valuable renewable resources of fur, wild rice, fish, bait, cranberries, game, etc. They are rich in plant and animal life and are, therefore, ideal for scientific studies and educational field trips. The recreational potential for wetlands is immense. About <red>eighty percent (80%)</red> of Canadians value wildlife conservation and spend some three (3) billion dollars annually on nonconsumptive wildlife related activities as well as another one (1) billion on consumptive pursuits. Photography, bird-watching, canoeing, nature study, hiking, fishing and hunting are all pursued in wetlands.",
            "::palo_verde.jpg",
            "The economic value of wetlands may far exceed the returns gained from converting them to other uses. In addition to recreational potential, the farming of wildlife for economic return has proven to be viable for many species (Smith et al. 1983). Wetlands may prove valuable to more than fur, rice or cranberries in future.",
            "The greatest threats to our remaining wetlands are from agricultural drainage and industrial or hoimports developments (Brynaert 1983). Vast sums are expended annually by federal and provincial government agencies to implement drainage programs with little or no consideration given to wildlife values. The extensive so-called stream improvements, channeling and ditching, are very much questionable. It is essential now to introduce measures that clearly place the onus on agricultural agencies to prove that drainage projects are economically viable and that they do not jeopardize our wetland habitats (Brynaert 1983).",
            "Wetlands are important to the productivity of the entire biosphere (Sanderson 1977). They are vital to effective management of many wildlife species that depend upon these habitats. Whether a hunter or a naturalist, the preservation of wetlands is an objective that should appeal to everyone (Brynaert 1983). The entire province, country and continent have suffered a great loss in natural resources because of wetland losses. If we cannot succeed in saving wetlands, we shall not be able to meet the greater challenge of safeguarding an environment that man can continue to inhabit (Rowntree 1979)."
        }
End Class