Help us shape the future of Go! Take the 2026 Go Developer Survey

watermark

package
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2026 License: Apache-2.0 Imports: 22 Imported by: 1

Documentation

Overview

Package watermark 提供 OFD 水印注解的添加、替换与删除能力。

水印以页面注解文件(<PageAnnot>)中的 Type="Watermark" 注解表示。本包 复用 pkg/replace 的 ZIP 重建框架:先按包内结构定位目标页面与注解文件, 计算出一组条目操作后交给 replace.Files 执行。

签名处理由内嵌的 replace.Options 决定,未额外包装。加水印会改动页面内容, 已有签名的摘要随即失效,而 replace.Options.Signatures 的零值等价于 SignatureDrop,即签名条目会被移除——处理已签名文档时若要保留签名作为原始 证据,必须显式设置:

options.Options = replace.Options{Signatures: creator.SignaturePreserve}

内嵌字段不能在复合字面量里直接赋值,因此上面写成先构造再赋给内嵌字段。 SignaturePreserve 会保留签名条目并逐条提示摘要可能失效。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(input any, target Target, wm Watermark, w io.Writer, options Options) error

Add 在 target 覆盖的每个页面各添加一条水印注解。

func ImageAppearance

func ImageAppearance(options ImageOptions) ([]byte, error)

ImageAppearance 生成水印外观中的图片对象 XML 片段,结果可直接用作 Watermark.Appearance。

func Remove

func Remove(input any, target Target, w io.Writer, options Options) error

Remove 删除 target 内匹配的水印注解;清理产生的空注解文件与索引。

func Replace

func Replace(input any, target Target, wm Watermark, w io.Writer, options Options) error

Replace 替换 target 内匹配的水印注解内容(保留其 ID)。

func TextAppearance

func TextAppearance(options TextOptions) ([]byte, error)

TextAppearance 生成水印外观中的文本对象 XML 片段,结果可直接用作 Watermark.Appearance。

Types

type Image

type Image struct {
	// Data 是图片字节(如 PNG)。设置了 Opacity 时仅支持 PNG,并按不透明度
	// 烘焙进图片自身的 alpha 通道后嵌入。
	Data []byte
	// Format 是 DocumentRes.xml 的 Format 属性值(如 "PNG"、"JPEG"),空值默认 "PNG"。
	Format string
	// Width 是单张图片显示宽度(毫米),0 使用默认值 40。
	Width float64
	// Height 是单张图片显示高度(毫米),0 时按图片像素宽高比推算。
	Height float64
	// Layout 指定水印在页面上的布局方式,默认 LayoutManual。
	Layout TextLayout
	// Opacity 是整体不透明度(0 到 255),设置后烘焙进 PNG 的 alpha 通道。
	Opacity *uint8
}

Image 描述一条图片水印及其嵌入方式。

type ImageOptions

type ImageOptions struct {
	// ID 是首个图片对象 ID;平铺时自增。0 使用默认值 1。
	ID uint64
	// ImageID 是被引用的媒体资源 ID,必填。
	ImageID uint64
	// Boundary 是布局区域(平铺/居中计算使用);零尺寸时使用整页 A4 边界。
	Boundary creator.Box
	// Width 是单张图片显示宽度(毫米);0 使用默认值 40。
	Width float64
	// Height 是单张图片显示高度(毫米);0 时按 Aspect 推算。
	Height float64
	// Aspect 是图片宽高比(宽/高);Height 为 0 且 Aspect 为 0 时按正方形处理。
	Aspect float64
	// X、Y 是布局偏移。LayoutManual 时是单张图片的起始位置;
	// 平铺/居中模式下作为相对计算位置的偏移。
	X, Y float64
	// Gap 是平铺行列间距(毫米);0 使用默认值 5。
	Gap float64
	// Layout 指定布局方式,默认 LayoutManual。
	Layout TextLayout
}

ImageOptions 描述 ImageAppearance 生成的图片对象外观。

type Options

type Options struct {
	replace.Options

	// SkipPermissionsCheck 跳过文档级水印权限检查(Permissions/Watermark=false 时默认拒绝)。
	SkipPermissionsCheck bool
	// SkipReadOnlyCheck 跳过只读检查(ReadOnly 缺省或为 true 的水印默认拒绝 Replace/Remove)。
	SkipReadOnlyCheck bool
}

Options 控制水印编辑行为。

type Target

type Target struct {
	// Document 是文档体下标,-1 表示全部文档体,0 表示第一个文档体。
	Document int
	// Pages 是页面下标列表,相对于 Document.xml 的 <Pages> 顺序;空表示全部页面。
	Pages []int
	// MatchIDs 是仅匹配的注解 ID;Replace/Remove 使用,空表示匹配全部水印注解。
	MatchIDs []uint64
}

Target 描述水印操作的目标范围。

type TextLayout

type TextLayout int

TextLayout 描述水印外观在页面区域中的布局方式。

const (
	// LayoutManual 使用显式位置(X/Y),保持默认的单条放置。
	LayoutManual TextLayout = iota
	// LayoutTile 把水印平铺铺满整个布局区域。
	LayoutTile
	// LayoutCenter 把水印居中放置。
	LayoutCenter
)

type TextOptions

type TextOptions struct {
	// ID 是文本对象 ID;0 使用默认值 1。
	ID uint64
	// Font 是 Font 属性值(字体 ID 或名称);空使用 "0"。
	Font string
	// Size 是字号;0 使用默认值 9。
	Size float64
	// Text 是 TextCode 内容,必填。
	Text string
	// X、Y 是布局偏移。LayoutManual 时就是文字起始位置;
	// 平铺/居中模式下作为相对计算位置的偏移。
	X, Y float64
	// Boundary 是布局区域(平铺/居中计算使用);零尺寸时使用整页 A4 边界。
	Boundary creator.Box
	// CTM 是文本对象变换矩阵;零值表示不输出 CTM。
	CTM [6]float64
	// Color 是 FillColor;nil 时不输出 FillColor。
	Color *creator.Color
	// Fill 指定是否填充;nil 时不输出 Fill 属性。
	Fill *bool
	// Opacity 是整体不透明度(Alpha 属性),取值范围 0 到 255;nil 时不输出。
	Opacity *uint8
	// Layout 指定布局方式,默认 LayoutManual。
	Layout TextLayout
	// Rotation 是文字旋转角度(度),正值在屏幕上为顺时针(左边往上、右边向下),
	// 绕每个文本实例自身中心倾斜、位置不偏移。与 CTM 同时指定时以 CTM 为准。
	Rotation float64
	// LineGap 是平铺行间距(毫米);0 使用默认值 size*0.6。
	LineGap float64
	// ColumnGap 是平铺列间距(毫米);0 使用默认值 size*0.6。
	ColumnGap float64
}

TextOptions 描述 TextAppearance 生成的文本对象外观。

type Watermark

type Watermark struct {
	// ID 是注解 ID,0 表示自动分配(取文档最大对象标识与页面内既有注解 ID 之后的空闲值)。
	ID uint64
	// Creator 是创建者名称,空值使用默认名称。
	Creator string
	// LastModDate 是最后修改日期,零值使用当天。
	LastModDate time.Time
	// Visible 指定注解是否可见;空值按 XSD 默认 true。
	Visible *bool
	// Subtype 是注解子类型。
	Subtype string
	// Print 指定是否随文档打印;空值按 XSD 默认 true。
	Print *bool
	// NoZoom 指定注解不随页面缩放。
	NoZoom bool
	// NoRotate 指定注解不随页面旋转。
	NoRotate bool
	// ReadOnly 指定注解为只读;空值按 XSD 默认 true。
	ReadOnly *bool
	// Remark 是注解备注。
	Remark string
	// Parameters 是注解自定义参数。
	Parameters []creator.AnnotationParameter
	// Boundary 是注解外观边界(毫米),为空时 `<Appearance>` 不写 Boundary 属性。
	Boundary *creator.Box
	// Appearance 是 `<Appearance>` 内容(页面对象片段)的原始 XML;为空时外观为空元素。
	// 设置了 Image 时本字段被忽略,外观由引擎根据图片资源自动生成。
	Appearance []byte
	// Image 指定图片水印:引擎会把图片写入文档资源、注册到 DocumentRes.xml,
	// 并按布局方式生成 ImageObject 外观。设置后忽略 Appearance。
	Image *Image
}

Watermark 描述一条水印注解及其外观。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL