[Golang] 纯文本查看 复制代码
package main
import (
"fmt"
"github.com/tiechui1994/gopdf"
"github.com/tiechui1994/gopdf/core"
)
const (
TABLE_IG = "IPAexG"
TABLE_MD = "MPBOLD"
TABLE_MY = "微软雅黑"
)
// CreateSignTable 创建签到表
func CreateSignTable() {
r := core.CreateReport()
font1 := core.FontMap{
FontName: TABLE_IG,
// 要将字体文件,放到程序可以访问的地方
FileName: "E:\\gomodules\\hello\\src\\wenjiancaozuo\\SIMYOU.TTF",
}
font2 := core.FontMap{
FontName: TABLE_MD,
FileName: "E:\\gomodules\\hello\\src\\wenjiancaozuo\\SIMYOU.TTF",
}
font3 := core.FontMap{
FontName: TABLE_MY,
FileName: "E:\\gomodules\\hello\\src\\wenjiancaozuo\\SIMYOU.TTF",
}
r.SetFonts([]*core.FontMap{&font1, &font2, &font3})
r.SetPage("A4", "P")
// 一定要设置字体,否则执行执行r.Cell会报错
r.SetFont(TABLE_IG, 18)
// 这里坐标是不断调整后的结果
r.Cell(240, 50, "培训签到表")
// 注册个表格生成器
r.RegisterExecutor(core.Executor(SimpleTableExecutor), core.Detail)
r.Execute("sign.pdf")
fmt.Println(r.GetCurrentPageNo())
}
func SimpleTableExecutor(report *core.Report) {
lineSpace := 0.0
// 每行文字的间距
lineHeight := 18.0
// 创建一个表格,4列,30行,宽度600
table := gopdf.NewTable(4, 30, 600, lineHeight, report)
table.SetMargin(core.Scope{})
// 合并单元格
c00 := table.NewCellByRange(4, 1) // 第1行,4个单元格合并
c10 := table.NewCellByRange(2, 1) // 第2行,2个单元格合并
c11 := table.NewCellByRange(2, 1) // 第2行,又一个2个单元格合并
f1 := core.Font{Family: TABLE_MY, Size: 15, Style: ""}
// 这里设置的是字内容和边框的距离
border := core.NewScope(4.0, 4.0, 4.0, 4.0)
// table.GetColWidth中, (0,0)第1行,第1列,(1,0),第2行,第1列,(1,2),第2行,第3列,虽然第2行,合并成只有两个单元格,但获取长度还是按原来默认表格计数
// 这里可以设置背景色,字体对齐样式等。
c00.SetElement(gopdf.NewTextCell(table.GetColWidth(0, 0), lineHeight, lineSpace, report).SetFont(f1).SetBorder(border).HorizontalCentered().SetContent("注意:不可以代替他人签到"))
c10.SetElement(gopdf.NewTextCell(table.GetColWidth(1, 0), lineHeight, lineSpace, report).SetFont(f1).SetBorder(border).SetBackColor("205,170,125").VerticalCentered().SetContent("课程:学习go语言"))
c11.SetElement(gopdf.NewTextCell(table.GetColWidth(1, 2), lineHeight, lineSpace, report).SetFont(f1).SetBorder(border).SetBackColor("205,170,125").VerticalCentered().SetContent("网址:[url=http://www.golangcodes.com]www.golangcodes.com[/url]"))
f1 = core.Font{Family: TABLE_MY, Size: 10}
border = core.NewScope(4.0, 4.0, 0, 0)
for i := 0; i < 28; i++ {
cells := make([]*gopdf.TableCell, 4)
for j := 0; j < 4; j++ {
cells[j] = table.NewCell()
}
for j := 0; j < 4; j++ {
w := table.GetColWidth(i+2, j)
e := gopdf.NewTextCell(w, lineHeight, lineSpace, report)
e.SetFont(f1)
if i%2 == 0 {
if i == 0 {
e.SetBackColor("193,205,205")
} else {
e.SetBackColor("224,238,238")
}
}
e.SetBorder(border)
content := ""
if i == 0 && j == 0 {
content = "姓名"
} else if i == 0 && j == 1 {
content = "上午"
} else if i == 0 && j == 2 {
content = "下午"
} else if i == 0 && j == 3 {
content = "晚上"
}
e.SetContent(content)
cells[j].SetElement(e)
}
}
table.GenerateAtomicCell()
}
func main() {
CreateSignTable()
}