cmc-sales/go/internal/cmc/pdf/page_numbers.go

42 lines
1.1 KiB
Go

package pdf
import (
"fmt"
"github.com/pdfcpu/pdfcpu/pkg/api"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/color"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types"
)
// AddPageNumbers adds page numbers to a PDF file
// This should be called after all merging is complete to show accurate page counts
func AddPageNumbers(pdfPath string) error {
// Read the PDF to get page count
pageCount, err := api.PageCountFile(pdfPath)
if err != nil {
return fmt.Errorf("failed to get page count: %w", err)
}
// Create watermark configuration for page numbers
// Position: bottom right, slightly above footer
wm := &model.Watermark{
TextString: fmt.Sprintf("Page $p of %d", pageCount),
FontName: "Helvetica",
FontSize: 9,
ScaleAbs: true,
Color: color.Black,
Pos: types.BottomRight,
Dx: -15, // 15mm from right
Dy: 25, // 25mm from bottom
Rotation: 0,
}
// Apply watermark (page numbers) to all pages
if err := api.AddWatermarksFile(pdfPath, pdfPath, nil, wm, nil); err != nil {
return fmt.Errorf("failed to add page numbers: %w", err)
}
return nil
}