样式
文本选项
所有文本样式通过 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.Black | Gray(0) |
pdf.White | Gray(1) |
pdf.Red | RGB(1, 0, 0) |
pdf.Green | RGB(0, 1, 0) |
pdf.Blue | RGB(0, 0, 1) |
pdf.Yellow | RGB(1, 1, 0) |
pdf.Cyan | RGB(0, 1, 1) |
pdf.Magenta | RGB(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)))
十六进制颜色
使用标准的十六进制颜色代码:
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. │
└─────────────────────────────────────────────┘