VerticalTextJP.vb
''
'' このコードは、DioDocs for PDF のサンプルの一部として提供されています。
'' Copyright (c) GrapeCity inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Drawing
'' 水平方向の列を持つレイアウトを使用して、右から左に縦書きのテキストを描画します。
'' ArabicColumns、MultiLang、VerticalText も参照してください。
Public Class VerticalTextJP
Const text = "日本語(にほんご、にっぽんご)は、主として、日本列島で使用されてきた言語である。日本手話を母語とする者などを除いて、ほぼ全ての日本在住者は日本語を第一言語とする。日本国は法令上、公用語を明記していないが、事実上の公用語となっており、学校教育の「国語」で教えられる。使用者は、日本国内を主として約\uFF11億\uFF13千万人。日本語の文法体系や音韻体系を反映する手話として日本語対応手話がある。"
Function CreatePDF(ByVal stream As Stream) As Integer
Using clouds As Image = Image.FromFile(Path.Combine("Resources", "Images", "clouds.jpg")),
firth As Image = Image.FromFile(Path.Combine("Resources", "Images", "firth.jpg")),
lavender As Image = Image.FromFile(Path.Combine("Resources", "Images", "lavender.jpg"))
Dim yumin = Font.FromFile(Path.Combine("Resources", "Fonts", "ipag.ttc"))
Dim ia = New ImageAlign(ImageAlignHorz.Left, ImageAlignVert.Top, True, True, True, False, False)
Dim doc = New GcPdfDocument()
'' テキストを保持および描画する TextLayout。
Dim tl = New TextLayout(72)
tl.FirstLineIndent = 18
tl.ParagraphSpacing = 6
tl.FlowDirection = FlowDirection.VerticalRightToLeft
tl.TextAlignment = TextAlignment.Justified
tl.AlignmentDelayToSplit = True
Dim tf = New TextFormat() With {.Font = yumin, .FontSize = 12}
'' いくつかのページを埋めるためにテストテキストを繰り返します。
For i = 0 To 25
tl.Append(text, tf)
tl.AppendLine()
Next
'' 4つの水平な列のレイアウトテキスト。
'' (このサンプルのロジック/コードは ArabicColumns と同じです)
Const NCOLS = 4
Dim margin = 36.0F
Dim gap = 18.0F
Dim page = doc.NewPage()
page.Landscape = True
Dim colHeight = (page.Size.Height - margin * 2 - gap * (NCOLS - 1)) / NCOLS
tl.MaxWidth = page.Size.Width
tl.MaxHeight = page.Size.Height
tl.MarginLeft = margin
tl.MarginRight = margin
tl.MarginTop = margin
tl.MarginBottom = margin + (colHeight + gap) * (NCOLS - 1)
'' 外周にテキストがフローする矩形を任意に指定することができます。
'' このケースでは、画像を描画するために3つの領域を追加します。
tl.ObjectRects = New List(Of ObjectRect) From {
New ObjectRect(page.Size.Width - margin - 267, margin, 267, 200),
New ObjectRect(margin + 100, margin + 60, 133, 100),
New ObjectRect(margin, page.Size.Height - margin - 301, 200, 301)
}
'' オブジェクトをイメージ領域に変換し、調整して見栄えの良いパディングを提供します。
Dim rClouds = tl.ObjectRects(0).ToRectangleF()
rClouds.Inflate(-4, -3)
Dim rFirth = tl.ObjectRects(1).ToRectangleF()
rFirth.Inflate(-4, -3)
Dim rLavender = tl.ObjectRects(2).ToRectangleF()
rLavender.Inflate(-4, -3)
page.Graphics.DrawImage(clouds, rClouds, Nothing, ia)
page.Graphics.DrawImage(firth, rFirth, Nothing, ia)
page.Graphics.DrawImage(lavender, rLavender, Nothing, ia)
'' 呼び出し:テキストを描画するのに必要なグリフを計算し、それをレイアウトします。
tl.PerformLayout(True)
'' 描画するテキストがまだある間はループします。
Dim done = False
While Not done '' レンダリングするテキストがまだある間にループする
For col = 1 To NCOLS
Dim nextcol = If(col < NCOLS, col, 0)
'' TextSplitOptions は、残りのテキストのレイアウト方法を TextLayout.Split() に伝えます。
'' 今回のケースでは、上下のマージンを更新することによって、列ごとに進めます。
Dim tso = New TextSplitOptions(tl) With {
.RestMarginTop = margin + (colHeight + gap) * nextcol,
.RestMarginBottom = margin + (colHeight + gap) * (NCOLS - 1 - nextcol)
}
Dim rest As TextLayout = Nothing
Dim split = tl.Split(tso, rest)
page.Graphics.DrawTextLayout(tl, PointF.Empty)
If split <> SplitResult.Split Then
done = True
Exit For
End If
tl = rest
Next
If Not done Then
page = doc.NewPage()
page.Landscape = True
'' 最初のページに画像を描画したいだけなので、ObjectRects をクリアします。
If tl.ObjectRects IsNot Nothing Then
tl.ObjectRects = Nothing
'' レイアウトをやり直す必要がありますが、グリフを再計算する必要はありません。
tl.PerformLayout(False)
End If
End If
End While
''
'' PDF ドキュメントを保存します。
doc.Save(stream)
Return doc.Pages.Count
End Using
End Function
End Class