diff --git a/README.md b/README.md index 86a4636..7bddc9e 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,13 @@ markpdf "path/to/source.pdf" "The Company Name" "path/to/output.pdf" -x -20 -y 2 markpdf "path/to/source.pdf" "The Company Name" "path/to/output.pdf" --center --font=times_bold_italic --color=0000FF markpdf "path/to/source.pdf" "The Company Name" "path/to/output.pdf" -cf times_bold_italic -l 0000FF +# Place text horizontally center with 50px offset vertically from edge +markpdf "path/to/source.pdf" "The Company Name" "path/to/output.pdf" --horizontal-b-center -y 50 + +# Place text horizontally center with 50px offset vertically +# with bold-italic "Times Roman" font +markpdf "path/to/source.pdf" "The Company Name" "path/to/output.pdf" -by 50 + # Place text at center with large bold-italic "Times Roman" font in blue color markpdf "path/to/source.pdf" "The Company Name" "path/to/output.pdf" --center --font=times_bold_italic --font-size=24.0 --color=0000FF markpdf "path/to/source.pdf" "The Company Name" "path/to/output.pdf" -cf times_bold_italic -s 24.0 -l 0000FF diff --git a/main.go b/main.go index 96079a0..a45a127 100644 --- a/main.go +++ b/main.go @@ -13,7 +13,7 @@ import ( ) var offsetX, offsetY, scaleImage, fontSize, spacing float64 -var scaleH, scaleW, scaleHCenter, scaleWCenter, center, tiles, verbose, version bool +var scaleH, scaleW, scaleHCenter, scaleWCenter, center, horizontalCenter, tiles, verbose, version bool var opacity, angle float64 var font, color string @@ -26,6 +26,7 @@ func init() { flag.Float64VarP(&offsetY, "offset-y", "y", 0, "Offset from top (or bottom for negative number).") flag.Float64VarP(&scaleImage, "scale", "p", 100, "Scale Image to desired percentage.") flag.BoolVarP(¢er, "center", "c", false, "Set position at page center. Offset X and Y will be ignored.") + flag.BoolVarP(&horizontalCenter, "horizontal-b-center", "b", false, "Set horizontal position at page center. Offset X will be ignored.") flag.BoolVarP(&scaleW, "scale-width", "w", false, "Scale Image to page width. If set, offset X will be ignored.") flag.BoolVarP(&scaleH, "scale-height", "h", false, "Scale Image to page height. If set, top offset Y will be ignored.") flag.BoolVarP(&scaleWCenter, "scale-width-center", "W", false, "Scale Image to page width and Y will be set at middle.") diff --git a/text_watermark.go b/text_watermark.go index e9d61aa..182a009 100644 --- a/text_watermark.go +++ b/text_watermark.go @@ -47,6 +47,14 @@ func adjustTextPosition(p *creator.Paragraph, c *creator.Creator) { offsetX = (c.Context().PageWidth / 2) - (p.Width() / 2) offsetY = (c.Context().PageHeight / 2) - (p.Height() / 2) + } else if horizontalCenter { + p.SetWidth(p.Width()) // Not working without setting it manually + p.SetTextAlignment(creator.TextAlignmentCenter) + offsetX = (c.Context().PageWidth / 2) - (p.Width() / 2) + if offsetY < 0 { + offsetY = c.Context().PageHeight - (math.Abs(offsetY) + p.Height()) + } + } else { if offsetX < 0 { p.SetWidth(p.Width()) // Not working without setting it manually @@ -114,4 +122,4 @@ func applyTemplate(t *template.Template, rec *Recipient, para *creator.Paragraph err := t.Execute(buf, rec) fatalIfError(err, fmt.Sprintf("Failed to execute watermark template: [%s]", err)) para.SetText(buf.String()) -} \ No newline at end of file +}