Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Horizontal aligned text feature #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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(&center, "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.")
Expand Down
10 changes: 9 additions & 1 deletion text_watermark.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
}
}