スタイリング

テキストオプション

すべてのテキストスタイリングは c.Text() のファンクショナルオプションで適用されます:

c.Text("Styled text",
    template.FontSize(18),
    template.Bold(),
    template.TextColor(pdf.RGBHex(0x1A237E)),
)

利用可能なテキストオプション

オプション説明
FontSize(size)フォントサイズ(ポイント単位、デフォルト: 12)
Bold()太字
Italic()イタリック
FontFamily(name)登録済みフォントファミリーを使用
TextColor(color)テキスト前景色
BgColor(color)テキスト背景色
AlignLeft()左揃え(デフォルト)
AlignCenter()中央揃え
AlignRight()右揃え
Underline()下線装飾
Strikethrough()取り消し線装飾
LetterSpacing(pts)文字間の追加スペース
TextIndent(value)先頭行インデント

カラー

gpdfは複数のカラーモデルをサポートしています:

定義済みカラー

c.Text("Red", template.TextColor(pdf.Red))
c.Text("Green", template.TextColor(pdf.Green))
c.Text("Blue", template.TextColor(pdf.Blue))
c.Text("Yellow", template.TextColor(pdf.Yellow))
c.Text("Cyan", template.TextColor(pdf.Cyan))
c.Text("Magenta", template.TextColor(pdf.Magenta))
┌─────────────────────────────────────────┐
│  Red       ← red                        │
│  Green     ← green                      │
│  Blue      ← blue                       │
│  Yellow    ← yellow                     │
│  Cyan      ← cyan                       │
│  Magenta   ← magenta                    │
└─────────────────────────────────────────┘
定数
pdf.BlackGray(0)
pdf.WhiteGray(1)
pdf.RedRGB(1, 0, 0)
pdf.GreenRGB(0, 1, 0)
pdf.BlueRGB(0, 0, 1)
pdf.YellowRGB(1, 1, 0)
pdf.CyanRGB(0, 1, 1)
pdf.MagentaRGB(1, 0, 1)

RGBカラー(浮動小数点)

RGB値の範囲は0.0から1.0です:

c.Text("Orange", template.TextColor(pdf.RGB(1.0, 0.5, 0.0)))
c.Text("Purple", template.TextColor(pdf.RGB(0.5, 0.0, 0.5)))
c.Text("Teal", template.TextColor(pdf.RGB(0.0, 0.5, 0.5)))

16進カラー

標準の16進カラーコードを使用できます:

c.Text("Coral", template.TextColor(pdf.RGBHex(0xFF6B6B)))
c.Text("Turquoise", template.TextColor(pdf.RGBHex(0x4ECDC4)))
c.Text("Sky Blue", template.TextColor(pdf.RGBHex(0x45B7D1)))
c.Text("Sage", template.TextColor(pdf.RGBHex(0x96CEB4)))

グレースケール

値の範囲は0.0(黒)から1.0(白)です:

c.Text("Black", template.TextColor(pdf.Gray(0.0)))
c.Text("Dark gray", template.TextColor(pdf.Gray(0.3)))
c.Text("Medium gray", template.TextColor(pdf.Gray(0.5)))
c.Text("Light gray", template.TextColor(pdf.Gray(0.7)))

CMYKカラー

印刷最適化出力用:

c.Text("CMYK text", template.TextColor(pdf.CMYK(0, 1, 1, 0))) // Red in CMYK

背景色

BgColor() で背景色を適用します:

page.AutoRow(func(r *template.RowBuilder) {
    r.Col(3, func(c *template.ColBuilder) {
        c.Text(" Red ", template.TextColor(pdf.White), template.BgColor(pdf.Red))
    })
    r.Col(3, func(c *template.ColBuilder) {
        c.Text(" Green ", template.TextColor(pdf.White), template.BgColor(pdf.Green))
    })
    r.Col(3, func(c *template.ColBuilder) {
        c.Text(" Blue ", template.TextColor(pdf.White), template.BgColor(pdf.Blue))
    })
    r.Col(3, func(c *template.ColBuilder) {
        c.Text(" Yellow ", template.BgColor(pdf.Yellow))
    })
})
┌───────────┬───────────┬───────────┬───────────┐
│ ██ Red ██ │ █ Green █ │ ██ Blue █ │  Yellow   │
│  (white)  │  (white)  │  (white)  │  (black)  │
└───────────┴───────────┴───────────┴───────────┘

テキスト装飾

下線

c.Text("Underlined text for emphasis", template.Underline())

取り消し線

c.Text("Strikethrough text for deletions", template.Strikethrough())

装飾の組み合わせ

c.Text("Combined underline and strikethrough",
    template.Underline(), template.Strikethrough())

c.Text("Colored underlined text",
    template.Underline(),
    template.TextColor(pdf.RGBHex(0x1565C0)),
    template.FontSize(14))

c.Text("Bold underlined heading",
    template.Bold(), template.Underline(), template.FontSize(16))
┌─────────────────────────────────────────────┐
│  Normal text without decoration             │
│  Underlined text for emphasis               │  ← 下線
│  Strikethrough text for deletions           │  ← 取り消し線
│  Combined underline and strikethrough       │  ← 両方
│  Colored underlined text                    │  ← 青、下線
│  Bold underlined heading                    │  ← 太字、下線
└─────────────────────────────────────────────┘

字間

文字間のスペースを制御します:

c.Text("Normal spacing (default)")
c.Text("1pt letter spacing", template.LetterSpacing(1))
c.Text("3pt letter spacing", template.LetterSpacing(3))
c.Text("5pt letter spacing", template.LetterSpacing(5))
c.Text("Tight spacing (-0.5pt)", template.LetterSpacing(-0.5))
┌─────────────────────────────────────────────┐
│  Normal spacing (default)                   │
│  1 p t  l e t t e r  s p a c i n g         │  ← やや広い
│  3 p t   l e t t e r   s p a c i n g       │  ← より広い
│  5 p t    l e t t e r    s p a c i n g     │  ← とても広い
│  Tight spacing (-0.5pt)                     │  ← 圧縮
└─────────────────────────────────────────────┘

テキストインデント

段落に先頭行インデントを追加します:

c.Text("This paragraph has a 24pt first-line indent. "+
    "The rest of the text wraps normally without indentation.",
    template.TextIndent(document.Pt(24)))

c.Text("This paragraph has a larger 48pt indent. "+
    "Useful for formal or academic document styling.",
    template.TextIndent(document.Pt(48)))
┌─────────────────────────────────────────────┐
│      This paragraph has a 24pt first-line   │
│  indent. The rest of the text wraps         │
│  normally without indentation.              │
│                                             │
│              This paragraph has a larger    │
│  48pt indent. Useful for formal or          │
│  academic document styling.                 │
└─────────────────────────────────────────────┘

次のステップ