GoPDF.go 889 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package lib
  2. import "github.com/signintech/gopdf"
  3. const (
  4. ValignTop = 1
  5. ValignMiddle = 2
  6. ValignBottom = 3
  7. )
  8. const (
  9. AlignLeft = 4
  10. AlignCenter = 5
  11. AlignRight = 6
  12. )
  13. func RectFillColor(pdf *gopdf.GoPdf,
  14. text string,
  15. fontSize int,
  16. x, y, w, h float64,
  17. r, g, b uint8,
  18. align, valign int,
  19. ) {
  20. pdf.SetLineWidth(0.1)
  21. pdf.SetFillColor(r, g, b) //setup fill color
  22. pdf.SetLineType("") // 线条样式
  23. pdf.RectFromUpperLeftWithStyle(x, y, w, h, "FD")
  24. pdf.SetFillColor(0, 0, 0)
  25. if align == AlignCenter {
  26. textw, _ := pdf.MeasureTextWidth(text)
  27. x = x + (w / 2) - (textw / 2)
  28. } else if align == AlignRight {
  29. textw, _ := pdf.MeasureTextWidth(text)
  30. x = x + w - textw
  31. }
  32. pdf.SetX(x)
  33. if valign == ValignMiddle {
  34. y = y + (h / 2) - (float64(fontSize) / 2)
  35. } else if valign == ValignBottom {
  36. y = y + h - float64(fontSize)
  37. }
  38. pdf.SetY(y)
  39. pdf.Cell(nil, text)
  40. }