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

creator

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: 42 Imported by: 1

README

OFD 创建器库指南

github.com/zc310/ofd/pkg/creator 根据 Go 文档模型生成 OFD 文件包。本指南集中说明 文字定位、资源和页面组织、附加结构及流式创建;命令行 manifest、导出、合并、替换 和水印用法见 ofd-creator。

主题 入口
最小创建示例 项目快速开始、Go 文档示例
写出与配置 创建入口、生成选项
坐标、页面和图层 坐标与页面组织
文字与字形 文字定位与字体
路径、颜色与透明度 绘制样式与颜色
裁剪、模板和复合图元 裁剪、模板与复合图元
资源复用 图片与资源
元数据及附加结构 元数据与阅读偏好、注解与动作、附件与扩展、签名与版本
大文档 流式创建超大 OFD
引用、路径和输出校验 校验与输出约定

除完整程序和 Go 文档示例外,下文代码均为模型片段。将片段组合进包含非空 ID 和页面的 creator.Document 后调用创建入口;fontBytes、imageBytes、pngData、 iccProfileData 等变量需要由调用方提供真实资源。使用 spec. 常量时导入 github.com/zc310/ofd/pkg/spec。

创建入口

输出方式 默认入口 指定 CreateOptions
文件 CreateFile(document, filename) 打开文件,调用 CreateWithOptions 并处理关闭错误。
io.Writer Create(document, writer) CreateWithOptions(document, writer, options)
字节切片 Marshal(document) MarshalWithOptions(document, options)
PageProvider → 文件 — CreateFileWithPages(meta, pages, filename, options)
PageProvider → io.Writer CreateWithPages(meta, pages, writer) CreateWithPagesOptions(meta, pages, writer, options)
PageProvider → 字节切片 — MarshalWithPages(meta, pages, options)

创建器会分配自动资源 ID、生成 XML 和资源清单,并检查字段、资源引用及继承循环。 文件入口返回的错误也包括文件创建和关闭错误。命令行的临时文件与原子替换策略 见 CLI 说明,不要把 CLI 行为等同于库的文件入口。

生成选项

CreateOptions 字段 用途
Compression Auto、Deflate 或 Store 压缩策略。
CompressionLevel DEFLATE 级别 1–9,0 使用默认级别 5;只影响实际使用 Deflate 的条目。
Deterministic 固定 ZIP 元数据,便于可重复生成。输入内容和日期也需要保持稳定。
PreserveEmbeddedFonts 保留完整嵌入字体;流式页面含嵌入字体时需要设置,或由调用方预先子集化。
CompleteTextCodeDeltas 按字体度量补齐缺失的 DeltaX/DeltaY,默认关闭。
DocType 根节点的文档类型,缺省为基础 OFD;声明类型不代表自动满足该类型的全部要求。

坐标与页面组织

页面坐标以左上角为原点,边界和字号均使用毫米。PageSize 未设置时默认 A4。 Document.Area 提供文档默认区域,Page.Area 可以设置本页区域;PhysicalBox 缺省时由页面尺寸补齐。

page := creator.Page{
	Area: &creator.PageArea{
		PhysicalBox:    &creator.Box{Width: 210, Height: 297},
		ApplicationBox: &creator.Box{X: 5, Y: 5, Width: 200, Height: 287},
		ContentBox:     &creator.Box{X: 10, Y: 10, Width: 190, Height: 277},
		BleedBox:       &creator.Box{X: -3, Y: -3, Width: 216, Height: 303},
	},
	Items: []creator.Item{
		creator.Text{X: 20, Y: 20, Width: 100, Height: 10, Value: "正文"},
	},
}

简单页面使用 Page.Items,多图层页面使用 Page.Layers,两者互斥。图层按切片 顺序写出;类型可取 Body、Background、Foreground 或 Custom。Page.LayerType 用于单图层页面。页面和模板的图元还可以通过 PageBlock 递归组织:

page := creator.Page{
	Layers: []creator.Layer{
		{Type: spec.LayerBackground, Items: []creator.Item{
			creator.Path{X: 0, Y: 0, Width: 210, Height: 297,
				Data: "M 0 0 L 210 0 L 210 297 L 0 297 C", Fill: true},
		}},
		{Type: spec.LayerForeground, Items: []creator.Item{
			creator.PageBlock{Items: []creator.Item{
				creator.Text{X: 20, Y: 20, Width: 100, Height: 10, Value: "前景内容"},
			}},
		}},
	},
}

创建器为嵌套块分配 ID,并检查其中的字体、图片、动作、裁剪和复合图元引用。

文字定位与字体

Text.Value 生成一个文字段。TextCodes 可指定多个段,每段提供起点及逐字符 DeltaX/DeltaY;内容可以是纯空白,但不能为空字符串。X/Y 是相对文字对象边界的 毫米坐标,未指定时由创建器补齐默认起点和基线。

x, y := 12.5, 8.0
text := creator.Text{
	X: 10, Y: 10, Width: 100, Height: 12, Size: 4.233,
	CTM: &creator.CTM{1, 0, 0, 1, 3, 4},
	TextCodes: []creator.TextCode{
		{Value: "第一段", X: &x, Y: &y, DeltaX: []float64{4.2, 4.2}},
		{Value: "第二段", DeltaY: []float64{4.2, 4.2}},
	},
}

默认不自动计算 DeltaX/DeltaY。需要根据字体度量补全时启用 CreateOptions{CompleteTextCodeDeltas: true},CLI 对应 --complete-text-code-deltas。

CGTransforms 用于显式字形编号映射。CodePosition 相对于整个文字对象,从 0 开始;CodeCount 是源字符数,GlyphCount 是字形数,Glyphs 保存字体实际字形编号:

text := creator.Text{
	Value: "字形映射",
	CGTransforms: []creator.CGTransform{{
		CodePosition: 1, CodeCount: 1, GlyphCount: 2,
		Glyphs: []int{120, 121},
	}},
}

使用 Document.Fonts 声明字体,Font.Name 与 Text.Font 一致。设置 Data 或 Source 后嵌入字体;仅使用逻辑字体名时,显示依赖阅读器的字体替代。

document := creator.Document{
	ID: "embedded-font",
	Fonts: []creator.Font{{Name: "Noto Sans CJK SC", Data: fontBytes}},
	Pages: []creator.Page{{Items: []creator.Item{
		creator.Text{X: 20, Y: 20, Width: 100, Height: 10,
			Font: "Noto Sans CJK SC", Value: "嵌入字体"},
	}}},
}

字体数据支持 TTF、OTF、TTC;字体子集化或字体面提取可能改变实际写出的数据。 多 FD CFF 保留完整字体面;具体限制见 PDF 支持清单。

绘制样式与颜色

路径支持 Stroke、Fill、Rule、LineWidth、Cap、Join、MiterLimit、DashPattern、 DashOffset 和 Alpha。Stroke: false 会显式关闭描边;StrokeSet 可覆盖 Stroke。 图片可设置名称、可见性及 ImageBorder。

路径语法:M 移动、L 直线、Q 二次曲线、B 三次曲线、A 椭圆弧、C 闭合。 Fill 的 Rule 使用 NonZero 或 Even-Odd;C 不是三次贝塞尔曲线。

alpha := uint8(200)
path := creator.Path{
	X: 10, Y: 10, Width: 80, Height: 60,
	Data: "M 0 0 L 80 0 L 80 60 L 0 60 C",
	Stroke: true, Fill: true, Rule: "Even-Odd",
	LineWidth: 0.5, Cap: "Round", Join: "Bevel", Alpha: &alpha,
	StrokeColor: &creator.Color{R: 0, G: 0, B: 255},
	FillColor:   &creator.Color{R: 255, G: 255, B: 0},
}

Document.DrawParams 抽取公共样式,图层、图元和裁剪通过名称引用,Relative 提供样式继承。名称必须存在,继承不能循环。

document := creator.Document{
	ID: "shared-style",
	DrawParams: []creator.DrawParam{
		{Name: "base-line", LineWidth: 0.5, Cap: "Round", Join: "Miter"},
		{Name: "blue-line", Relative: "base-line",
			StrokeColor: &creator.Color{R: 20, G: 40, B: 200}},
	},
	Pages: []creator.Page{{Items: []creator.Item{
		creator.Path{X: 1, Y: 2, Width: 80, Height: 60,
			Data: "M 0 0 L 80 60", Stroke: true, DrawParam: "blue-line"},
	}}},
}
颜色空间、渐变和图案
配置 规则
Color.R/G/B/Alpha 通道范围 0–255,Alpha 为不透明度。
Color.Components 未指定 ColorSpace 时按默认 RGB,须提供三个分量;指定后按该空间的通道顺序填写。
Document.ColorSpaces 显式 ID;GRAY/RGB/CMYK 分别为 1/3/4 分量。分量和调色板值不得超出 BitsPerComponent 范围,缺省位深为 8。
ColorSpace.ProfileData 嵌入 Profile 到 Doc_0/Res/Profiles/;相同路径不能对应不同数据。
Document.DefaultCS 必须引用已声明的颜色空间 ID。
Color.Axial / Color.Radial 互斥;至少两个 Segments,位置在 0–1 之间,起止坐标必须有限。
Color.Gouraud 至少三个控制点。
Color.LaGouraud 设置 VerticesPerRow,控制点数必须是每行顶点数的整数倍。
Color.Pattern 正尺寸的图案单元,使用 Items 或 Layers;不能同时设置渐变。Thumbnail 可引用图片资源。
pattern := &creator.Pattern{
	Width: 10, Height: 10, XStep: 10, YStep: 10,
	ReflectMethod: "RowAndColumn", RelativeTo: "Object",
	Items: []creator.Item{
		creator.Path{X: 0, Y: 0, Width: 10, Height: 10,
			Data: "M 0 0 L 10 0 L 10 10 C", Fill: true},
	},
}
path := creator.Path{
	X: 10, Y: 10, Width: 80, Height: 40,
	Data: "M 0 0 L 80 0 L 80 40 L 0 40 C", Fill: true,
	FillColor: &creator.Color{Pattern: pattern},
}

图案输出为 OFD CellContent,复用文字、路径、图片及复合图元模型。

裁剪

文字、路径、图片及裁剪区域可使用六参数 CTM。Clips 至少有一个 Clip,每个 Clip 至少有一个 Area;Area 必须且只能提供 Path 或 Text,也可引用 DrawParam。

image := creator.Image{
	X: 10, Y: 10, Width: 80, Height: 60, Data: pngData,
	CTM: &creator.CTM{1, 0, 0, 1, 5, 5},
	Clips: &creator.Clips{Items: []creator.Clip{{Areas: []creator.ClipArea{{
		Path: &creator.ClipPath{
			Boundary: creator.Box{Width: 50, Height: 40},
			Data: "M 0 0 L 50 0 L 50 40 L 0 40 C", Fill: true,
		},
	}}}}},
}

裁剪坐标、CTM、路径数据、文字及绘制参数引用都会校验。

模板与复合图元

模板页、复合图元、颜色空间等资源使用显式 ID;ID 不得与其他资源冲突。 模板内容支持普通页面的图层与图元,页面通过 TemplateRef 引用,ZOrder 只能取 Background 或 Foreground,缺省使用 OFD 默认值。

document := creator.Document{
	ID: "template-example",
	Templates: []creator.TemplatePage{{
		ID: 10, Name: "页眉模板", ZOrder: "Background",
		Items: []creator.Item{
			creator.Path{X: 0, Y: 0, Width: 210, Height: 5,
				Data: "M 0 0 L 210 0 L 210 5 L 0 5 C", Fill: true},
		},
	}},
	Pages: []creator.Page{{
		Templates: []creator.TemplateRef{{ID: 10, ZOrder: "Background"}},
		Items: []creator.Item{
			creator.Text{X: 10, Y: 20, Width: 80, Height: 10, Value: "正文"},
		},
	}},
}

复合图元用 Document.Composites 定义尺寸和内容,图元 Composite.ResourceID 引用它。复合图元可设置 Thumbnail 和 Substitution 图片资源。

composite := creator.CompositeGraphicUnit{
	ID: 20, Width: 40, Height: 30,
	Items: []creator.Item{
		creator.Path{X: 0, Y: 0, Width: 40, Height: 30,
			Data: "M 0 0 L 40 0 L 40 30 C", Fill: true},
	},
}
item := creator.Composite{X: 10, Y: 20, Width: 40, Height: 30, ResourceID: 20}

图片与资源

资源入口 用途与规则
Image.Data / Source 直接提供图片;未指定 ResourceID 时自动注册文档资源。
Document.Media 显式注册 Image/Audio/Video,供图片、复合图元或声音/影片动作引用。
Page.Resources 页面专属资源;Images 使用显式 ID,页面 Image.ResourceID 引用该 ID。
PublicRes 公共资源 XML,根元素须为 OFD Res;名称相对于文档根目录。
PageResource.Data / PublicResource.Data 原始 Res XML,保留字体、绘制参数、颜色空间、复合图元和多媒体等资源。
PageResource.Files / PublicResource.Files 提供原始 XML 引用的二进制文件,路径相对于资源 XML 所在目录;FontFile、MediaFile、Profile 必须可解析。
Image.Substitution / Image.ImageMask 引用图片替代或蒙版资源,目标必须是页面图片或 Type 为 Image 的文档多媒体资源。
document := creator.Document{
	ID: "page-image",
	Pages: []creator.Page{{
		Resources: []creator.PageResource{{Images: []creator.PageImage{
			{ID: 70, Format: "PNG", Data: imageBytes},
		}}},
		Items: []creator.Item{
			creator.Image{X: 10, Y: 10, Width: 40, Height: 30, ResourceID: 70},
		},
	}},
}

页面资源文件写入对应页面目录,公共资源路径例如 Public/SharedRes.xml。原始资源 中的 ID 参与引用校验,不能覆盖 Document.xml、页面或其他系统文件。媒体和附件格式 去除前导点并规范为小写,格式值不能含路径分隔符或 NUL。

元数据与阅读偏好

文档元数据可设置标题、作者、摘要、用途、关键词、CustomDatas 和 CoverData。 封面写入 Doc_0/Cover/ 并由 DocInfo 引用。

document := creator.Document{
	ID: "metadata-example", Title: "标题", Author: "作者",
	Abstract: "摘要", DocUsage: "Report", Keywords: []string{"ofd", "metadata"},
	CustomDatas: []creator.CustomData{{Name: "Department", Value: "Engineering"}},
	CoverData: coverImageBytes,
	Pages: []creator.Page{{}},
}

Permissions 声明编辑、导出、打印、份数和有效期;Preferences 声明页面模式、布局、 界面显示和缩放。ZoomMode 与具体 Zoom 比例互斥。声明这些字段不代表所有阅读器 都会执行,支持范围见 OFD 支持清单。

注解与动作

同一页面的注解合并到一个 AnnotationPage,Page 是从 0 开始的索引。注解必须指定 唯一 ID、合法 Type、Creator 和外观内容。ReadOnlyValue 可显式输出 false,避免 被 Schema 的默认 true 覆盖。

annotations := []creator.AnnotationPage{{
	Page: 0,
	Items: []creator.Annotation{{
		ID: 50, Type: "Highlight", Creator: "ofd-creator", Remark: "重点内容",
		Boundary: &creator.Box{X: 10, Y: 20, Width: 40, Height: 8},
		Items: []creator.Item{
			creator.Path{X: 10, Y: 20, Width: 40, Height: 8,
				Data: "M 0 0 L 40 0 L 40 8 L 0 8 C", Fill: true},
		},
	}},
}}

文档、页面和文字/路径/图片等图元可以携带 Actions。一个动作只设置 URI、Goto、 GotoA、Sound 或 Movie 之一;未指定 Event 时使用默认点击事件。

动作或目标 规则
URI URI 非空,可指定 Target。
Goto.Page 从 0 开始,必须在文档页数范围内。
Goto.Bookmark 引用已声明的书签,与页面目标互斥;Document.Bookmarks 自己的 Goto 必须使用页面 Dest。
FitH / FitV 分别使用 Top / Left。
FitR 完整且顺序正确的 Left/Top/Right/Bottom 矩形。
Fit 不接受位置或缩放参数。
GotoA.AttachID 引用当前文档的附件;NewWindow 缺省沿用 OFD 默认行为。
Sound / Movie ResourceID 分别引用 Audio / Video 资源。
Action.Region 至少一个区域和路径命令,坐标有限;使用 RegionMove/Line/曲线/Arc/Close 等命令。
top := 40.0
document := creator.Document{
	ID: "page-link",
	Pages: []creator.Page{{Items: []creator.Item{
		creator.Text{X: 10, Y: 10, Width: 80, Height: 10, Value: "打开第二页",
			Actions: []creator.Action{{Goto: &creator.GotoAction{
				Page: 1, Type: "FitH", Top: &top,
			}}},
		},
	}}, {}},
}

Outlines 支持 Children 层级和动作,Bookmarks 提供命名目标。它们的页面目标同样 使用从 0 开始的索引;页数要包含所有被引用的页面。

附件与扩展

配置 规则与输出
Attachments ID、Name 和数据非空;写入附件清单及数据,Document.xml 引用清单。同一文档附件文件名唯一。
CustomTags NameSpace 和标签数据非空;Schema 可选,只有提供 Schema 数据时才设置 SchemaName。写入标签清单及子文件。
Extensions AppName、RefID 和数据按模型校验;Data 与 DataXML 互斥;内联数据与外部 ExtendData 互斥。
Extension.Data 文本内容,包括需要按文本保存的 XML 字符串。
Extension.DataXML 嵌套 XML;保留原有前缀与命名空间。
DataName 用于外部扩展文件,内联扩展不能设置。
attachments := []creator.Attachment{{
	ID: "att-1", Name: "说明文本", Format: "txt", FileName: "readme.txt",
	Usage: "Data", Data: []byte("attachment content"),
}}

附件、多媒体、封面、页面图片、自定义标签、扩展、签名值和版本根文件的文件名 必须满足各自的单层文件名规则,不能含分隔符、路径段或 NUL。

签名与版本

Signatures 生成根入口的签名引用、签名清单、Signature.xml 和签名值文件。 调用方负责提供合法签名数据;创建签名结构和计算引用摘要不等同于完成密码学签署。 外部命令签名见 pkg/sign 和 CLI 签名说明。

signatures := []creator.Signature{{
	ID: "sig-1", Type: "Sign", ProviderName: "creator-example",
	Method: "RSA", CheckMethod: "SHA1",
	References: []creator.SignatureReference{{FileRef: "../Document.xml"}},
	SignedValue: signedValueBytes, SignedValueName: "value.bin",
}}
结构 规则
Signature.ID 合法 XML ID,不自动裁剪首尾空白。
Sign / Seal Sign 必须提供 SignedValue;Seal 可提供 SealFile。
CheckValue 为空时根据 CheckMethod 计算已存在目标条目的摘要;签名 XML 自身和尚未生成的签名条目不能作为自动摘要目标。
CheckMethod MD5、SHA1、SM3 及对应国密 OID;缺省按 Schema 使用 MD5,大小写会规范化。
SignatureReference.FileRef 从签名目录解析,允许规范的 ../Document.xml;目标必须存在。
Versions 生成版本清单、版本描述和可选文档根文件;ID 与文件 ID 为合法 XML ID,Index 唯一,最多一个 Current 版本。
Version.Files 列出该版本所需的全部文件,包括入口、页面和资源。输入路径相对于文档目录 Doc_0;输出到版本描述文件时自动转换为 /Doc_0/... 包内绝对路径。
Version.Version 该版本适用的 OFD 格式版本,如 1.0,不是文档修订版本号。
Version.DocRoot 根元素为 Document,使用 OFD 命名空间。
versions := []creator.DocumentVersion{{
	ID: "version-1", Index: 1, Current: true, Version: "1.0", Name: "初始版本",
	Files: []creator.VersionFile{
		{ID: "file-1", Path: "Versions/Files/version-document.xml"},
		{ID: "file-2", Path: "Pages/Page_0/Content.xml"},
	},
	DocRoot: versionDocumentXML, DocRootName: "version-document.xml",
}}

上述示例假设版本只有一个空页面;若页面使用字体、图片等资源,还需在版本入口引用资源文件并将依赖加入 Files。 生成的版本 BaseLoc、文件清单和 DocRoot 统一使用包内绝对路径。 直接提供的版本入口 XML 保留原内容;建议也使用 /Doc_0/Pages/Page_0/Content.xml 等包内绝对路径。 若使用相对路径,则从 Doc_0/Versions/Files 解析,例如 ../../Pages/Page_0/Content.xml。 这些路径规则与版本文件清单要求依据 GB/T 33190—2016 §7.3、§19.2。 不同签名值/印章文件和不同版本根文件不能使用冲突的文件名。

流式创建超大 OFD

页面数量大或资源大时,使用 PageProvider 按索引提供页面,用 DataSource 按需读取 资源。元数据和资源放在 meta,meta.Pages 必须为空;PageAt 在 ID 预留、校验和 写出阶段会被重复调用,必须为同一索引返回相同页面。

下面片段使用外部字体文件,调用前需要准备该路径,并导入 fmt、log:

meta := creator.Document{
	ID: "big-report", PageSize: creator.A4,
	Fonts: []creator.Font{{
		Name: "SourceHanSans", Format: "otf",
		Source: creator.FileDataSource("/path/to/SourceHanSans.otf"),
	}},
}
options := creator.CreateOptions{
	Compression: creator.CompressionAuto, PreserveEmbeddedFonts: true,
}
if err := creator.CreateFileWithPages(meta, reportPages{total: 10_000}, "report.ofd", options); err != nil {
	log.Fatal(err)
}
type reportPages struct{ total int }

func (p reportPages) PageCount() int { return p.total }

func (p reportPages) PageAt(index int) (creator.Page, error) {
	return creator.Page{Items: []creator.Item{
		creator.Text{X: 20, Y: 20, Width: 170, Height: 12,
			Value: fmt.Sprintf("第 %d / %d 页", index+1, p.total),
			Font: "SourceHanSans", Size: 4.233},
	}}, nil
}

流式页面不能回写字体子集后的字形编号;含嵌入字体时设置 PreserveEmbeddedFonts, 或预先完成子集化。可使用 Source 的资源包括字体、图片、多媒体、附件、封面、 公共/页面资源文件、扩展数据及印章;同名 Source 优先于 Data。

PageProvider 减少页面模型的常驻内存,不能保证所有资源、ID 和引用统计都使用 常量内存;MarshalWithPages 仍把最终 OFD 保存在字节缓冲区。CLI 对应 --stream。

页面和模板 XML 在生成过程中分块写入 ZIP,输出缓冲区不随单页 XML 大小增长; 单次超长文本、路径和数值列表也可分块输出。页面模型、构建后的图元及辅助 XML 仍占用内存;被签名引用的条目会保留完整原文用于计算摘要。需要降低最终产物的 内存占用时,使用文件或 io.Writer 创建入口。

校验与输出约定

项目 约定
文档和资源引用 文档 ID 非空;资源引用必须存在且类型正确;继承和递归遵守实现限制。
数值 ID 不要求按输入顺序递增;创建器预留显式 ID,自动分配跳过这些值。ID/引用符合 xs:unsignedInt,即不超过 uint32 范围。
XML 标识符 附件、签名、版本等 XML ID 必须合法,首尾空白不自动裁剪。
数值和日期 坐标、CTM、渐变等必须有限;Schema 日期的年份范围为 1–9999。
包内路径 规范的相对路径,不允许绝对路径、反斜杠、NUL、路径穿越或首尾空白;签名引用按自身目录解析。
文件冲突 页面图片、附件、多媒体、签名和版本文件等不得产生重复或覆盖系统条目。
XML 输出 创建器生成的元素使用 ofd: 前缀,根节点声明 xmlns:ofd="http://www.ofdspec.org/2016";普通属性无命名空间。原始资源 XML 和 DataXML 保留自身前缀。
稳定输出 Profile 条目按颜色空间 ID 排序;注解日期缺省取文档修改/创建日期,再回退到 1970-01-01。
输入模型 Marshal/Create 不把生成的 Profile 路径、签名摘要或图案缓存写回调用方模型,同一输入可重复生成。

独立校验生成结果时,在仓库根目录运行:

go run ./cmd/ofd-validator --format text --mode strict /path/to/output.ofd

格式校验通过不等同于显示正确。含字体、裁剪或渐变的文档还需检查页面渲染和 PDF/文字输出。字段定义及其注释见 model.go,完整 API 可通过 go doc github.com/zc310/ofd/pkg/creator 查询。

Documentation

Overview

Package creator 根据高层文档模型创建 OFD 文件包。

Index

Examples

Constants

View Source
const (
	// DefaultMaxEntries 是默认的最大条目数。
	DefaultMaxEntries = 10000
	// DefaultMaxEntryBytes 是默认的单条解压上限(64MB)。
	DefaultMaxEntryBytes = int64(64 << 20)
	// DefaultMaxTotalBytes 是默认的解压总字节上限(512MB)。
	DefaultMaxTotalBytes = int64(512 << 20)
)
View Source
const DefaultCompressionLevel = 5

DefaultCompressionLevel 是 DEFLATE 的默认压缩级别,与 klauspost zip 的 默认一致。级别越高压缩率越高、写入越慢。

Variables

View Source
var A4 = PageSize{Width: 210, Height: 297}

A4 是毫米单位的标准 A4 页面尺寸。

Functions

func ApplyCompressionLevel added in v0.1.3

func ApplyCompressionLevel(w *zip.Writer, level int)

ApplyCompressionLevel 在 zip.Writer 上按级别注册 DEFLATE 压缩器,必须 在第一次创建 Deflate 条目之前调用。level 为 0 或默认级别时保持 klauspost 的默认实现,不注册。调用方应先通过 NormalizeCompressionLevel 校验级别。

func CheckSize added in v0.1.3

func CheckSize(name string, size, maxEntryBytes, maxTotalBytes int64, remaining *int64) error

CheckSize 校验内存条目 size 是否在单条与总预算内,并在通过后扣减总预算。

func Create

func Create(document Document, w io.Writer) error

Create 将完整的 OFD ZIP 文件包写入 w。

func CreateFile

func CreateFile(document Document, filename string) (err error)

CreateFile 在 filename 指定的位置创建 OFD 文件包。

func CreateFileWithPages added in v0.1.3

func CreateFileWithPages(meta Document, pages PageProvider, filename string, options CreateOptions) (err error)

CreateFileWithPages 使用 PageProvider 在 filename 指定的位置创建 OFD 文件包。

func CreateWithOptions

func CreateWithOptions(document Document, w io.Writer, options CreateOptions) error

CreateWithOptions 按指定选项将完整的 OFD ZIP 文件包写入 w。

func CreateWithPages added in v0.1.3

func CreateWithPages(meta Document, pages PageProvider, w io.Writer) error

CreateWithPages 使用 PageProvider 按需提供的页面创建 OFD,用于页数或页模型 超过内存的场景。meta 提供文档元数据与资源,meta.Pages 必须为空。

因为字体子集化需要改写页内字形编号,流式页面必须设置 CreateOptions.PreserveEmbeddedFonts 为 true,或预先自行完成字体子集化。

Example

ExampleCreateWithPages 演示用 PageProvider 按需生成大量页面。

package main

import (
	"bytes"
	"fmt"
	"log"

	"github.com/zc310/ofd/pkg/creator"
)

// reportPages 按索引即时生成报表页面,不把全部页面保存在内存中。
// 实际使用时可以从磁盘、数据库或业务数据流中构造页面。
type reportPages struct {
	total int
}

func (p reportPages) PageCount() int { return p.total }

func (p reportPages) PageAt(index int) (creator.Page, error) {
	fill := true
	return creator.Page{
		Items: []creator.Item{
			creator.Text{
				X: 35, Y: 99, Width: 170, Height: 10,
				Value: "欢迎使用 OFD!",
				Font:  "楷体", Size: 12, Fill: &fill, FillColor: &creator.Color{R: 90, G: 100, B: 120},
			},
			creator.Text{
				X: 80, Y: 279, Width: 170, Height: 10,
				Value: fmt.Sprintf("第 %d / %d 页", index+1, p.total),
				Font:  "SimSun", Size: 7, Fill: &fill, FillColor: &creator.Color{R: 189, G: 189, B: 189},
			},
		},
	}, nil
}

// ExampleCreateWithPages 演示用 PageProvider 按需生成大量页面。
func main() {
	// 文档元数据与资源放在 meta 中,Pages 为空,页面由 PageProvider 提供。
	meta := creator.Document{
		ID:       "report",
		Title:    "按需生成报表",
		PageSize: creator.A4,
	}

	var output bytes.Buffer
	if err := creator.CreateWithPages(meta, reportPages{total: 1000}, &output); err != nil {
		log.Fatal(err)
	}
	fmt.Println(output.Len() > 0)
}
Output:
true

func CreateWithPagesOptions added in v0.1.3

func CreateWithPagesOptions(meta Document, pages PageProvider, w io.Writer, options CreateOptions) error

CreateWithPagesOptions 按指定选项使用 PageProvider 创建 OFD。

func EntryMethod added in v0.1.3

func EntryMethod(name string, mode CompressionMode) uint16

EntryMethod 返回 ZIP 条目的压缩方式。mode 为 "store" 或 "deflate" 时 强制指定;其他值按扩展名自动选择:本身已压缩的格式使用 Store,其余使用 Deflate。

func InSignatureDir added in v0.1.3

func InSignatureDir(name string, dirs ...string) bool

InSignatureDir 判断条目是否位于给出的任一签名目录内;空目录忽略。

func IsSignatureEntry added in v0.1.3

func IsSignatureEntry(name string) bool

IsSignatureEntry 判断条目是否为签名清单、签名文件或签名目录内的条目, 覆盖标准 Signatures/ 布局与历史 Signs/ 布局。

func Marshal

func Marshal(document Document) ([]byte, error)

Marshal 返回完整 OFD 文件包的字节数据。

func MarshalWithOptions

func MarshalWithOptions(document Document, options CreateOptions) ([]byte, error)

MarshalWithOptions 按指定选项返回完整 OFD 文件包的字节数据。

func MarshalWithPages added in v0.1.3

func MarshalWithPages(meta Document, pages PageProvider, options CreateOptions) ([]byte, error)

MarshalWithPages 按指定选项返回使用 PageProvider 创建的完整 OFD 字节数据。

func NormalizeCompressionLevel added in v0.1.3

func NormalizeCompressionLevel(level int) (int, error)

NormalizeCompressionLevel 校验 DEFLATE 压缩级别:0 表示使用默认级别 DefaultCompressionLevel,显式范围 1(最快)到 9(最紧凑)。返回实际 应使用的压缩级别。

func TableHeight added in v0.1.5

func TableHeight(table Table, pageWidth float64) (float64, error)

TableHeight 返回表格内容区从顶边到最后一行的总高,单位毫米。

用于把多张表依次排在同一页上:摆下一张表要知道上一张有多高,而这个高度由行高 与单元格换行决定,调用方按字号估算必然对不上。

不含表题:表题在内容上方,计入会让调用方重复预留间距。页高不参与计算, 所以这个值与表格最终落在第几页无关。

func ValidateLimits added in v0.1.3

func ValidateLimits(limits Limits) error

ValidateLimits 校验解压规模限制不能为负数。

Types

type Action

type Action struct {
	// Event 是触发动作的事件类型。
	Event ActionEvent
	// Region 是动作触发区域,可选。
	Region *ActionRegion
	// URI 是外部 URI 动作目标,与其他动作目标互斥。
	URI *URIAction
	// Goto 是当前文档内的页面跳转目标,与其他动作目标互斥。
	Goto *GotoAction
	// GotoA 是文档附件跳转目标,与其他动作目标互斥。
	GotoA *GotoAAction
	// Sound 是声音播放目标,与其他动作目标互斥。
	Sound *SoundAction
	// Movie 是影片控制目标,与其他动作目标互斥。
	Movie *MovieAction
}

Action 描述一个 URI、页面、附件或媒体动作,且只能设置一个动作目标。

type ActionArea

type ActionArea struct {
	// Start 是区域路径的起始点,单位为毫米。
	Start Point
	// Commands 是从 Start 开始的路径命令。
	Commands []RegionCommand
}

ActionArea 描述动作区域中的一条路径。

type ActionEvent

type ActionEvent string

ActionEvent 表示动作触发事件。

const (
	// ActionEventDO 表示文档打开或对象执行事件。
	ActionEventDO ActionEvent = spec.ActionEventDO
	// ActionEventPO 表示页面打开事件。
	ActionEventPO ActionEvent = spec.ActionEventPO
	// ActionEventClick 表示点击事件,也是默认事件。
	ActionEventClick ActionEvent = spec.ActionEventClick
)

type ActionRegion

type ActionRegion struct {
	// Areas 是动作触发区域中的路径列表。
	Areas []ActionArea
}

ActionRegion 描述动作触发区域。

type Annotation

type Annotation struct {
	// ID 是注解标识。
	ID uint64
	// Type 是注解类型。
	Type string
	// Creator 是注解创建者。
	Creator string
	// LastModDate 是注解最后修改时间。
	LastModDate time.Time
	// Visible 指定注解是否可见;为空时使用默认值。
	Visible *bool
	// Subtype 是注解子类型。
	Subtype string
	// Print 指定注解是否随文档打印;为空时使用默认值。
	Print *bool
	// NoZoom 指定注解是否不随页面缩放。
	NoZoom bool
	// NoRotate 指定注解是否不随页面旋转。
	NoRotate bool
	// ReadOnly 指定注解是否只读。
	ReadOnly bool
	// ReadOnlyValue 用于显式写出 ReadOnly=false。为空时沿用 ReadOnly 的兼容行为。
	ReadOnlyValue *bool
	// Remark 是注解备注。
	Remark string
	// Parameters 是注解自定义参数。
	Parameters []AnnotationParameter
	// Boundary 是注解边界,单位为毫米。
	Boundary *Box
	// Items 是注解外观中的页面对象。
	Items []Item
}

Annotation 描述页面注解及其外观。

type AnnotationPage

type AnnotationPage struct {
	// Page 是从 0 开始的注解所属页面索引。
	Page int
	// Items 是该页面的注解列表。
	Items []Annotation
}

AnnotationPage 描述某个页面的注解集合,Page 使用从 0 开始的页面索引。

type AnnotationParameter

type AnnotationParameter struct {
	// Name 是注解参数名称。
	Name string
	// Value 是注解参数值。
	Value string
}

AnnotationParameter 描述注解的自定义参数。

type Attachment

type Attachment struct {
	// ID 是附件标识。
	ID string
	// Name 是附件显示名称。
	Name string
	// Format 是附件格式或媒体类型。
	Format string
	// CreationDate 是附件创建时间。
	CreationDate time.Time
	// ModDate 是附件最后修改时间。
	ModDate time.Time
	// Visible 指定附件是否在阅读器中可见;为空时使用默认值。
	Visible *bool
	// Usage 描述附件用途。
	Usage string
	// Data 是附件的二进制数据。
	Data []byte
	// Source 是附件的惰性数据来源,优先于 Data。
	Source DataSource
	// FileName 是附件在 OFD 包内使用的文件名。
	FileName string
}

Attachment 描述文档附件及其包内数据。

type AxialShading

type AxialShading struct {
	// MapType 是渐变映射类型。
	MapType string
	// MapUnit 是渐变映射单位。
	MapUnit float64
	// Extend 指定渐变端点外的延展方式。
	Extend int
	// StartPoint 是渐变起点坐标。
	StartPoint string
	// EndPoint 是渐变终点坐标。
	EndPoint string
	// Segments 是按位置排列的颜色分段。
	Segments []ColorStop
}

AxialShading 描述轴向渐变。

type Bookmark

type Bookmark struct {
	// Name 是书签名称。
	Name string
	// Goto 是书签指向的页面目标。
	Goto GotoAction
}

Bookmark 描述文档书签及其页面目标。

type Box

type Box struct {
	// X 是区域左上角的横坐标,单位为毫米。
	X float64
	// Y 是区域左上角的纵坐标,单位为毫米。
	Y float64
	// Width 是区域宽度,单位为毫米。
	Width float64
	// Height 是区域高度,单位为毫米。
	Height float64
}

Box 描述页面区域或图元边界,单位为毫米。

type CGTransform

type CGTransform struct {
	// CodePosition 是相对于文字对象的起始字符位置,从 0 开始。
	CodePosition int
	// CodeCount 是参与映射的字符数量。
	CodeCount int
	// GlyphCount 是映射生成的字形数量。
	GlyphCount int
	// Glyphs 是字形索引列表。
	Glyphs []int
}

CGTransform 描述文字分段中的字形映射关系。 CodePosition 是相对于整个文字对象的字符位置,从 0 开始。

type CTM

type CTM [6]float64

CTM 是 OFD 六参数坐标变换矩阵,顺序为 a、b、c、d、e、f。

type CaptionStyle added in v0.1.5

type CaptionStyle struct {
	// Font 是字体资源名称。
	Font string
	// Size 是字号,单位为毫米。
	Size float64
	// Weight 是字重。
	Weight int
	// Italic 指定是否斜体。
	Italic bool
	// Align 是对齐方式:left/center/right。
	Align string
	// Color 是文字颜色。
	Color *Color
	// Gap 是说明文字与它所标注对象之间的间距,单位毫米,取正数。
	// 说明文字处在对象哪一侧由所在图元决定:表格表题在上,图片说明在下。
	Gap float64
}

CaptionStyle 控制表题/续表题的样式。

type Clip

type Clip struct {
	// Areas 是裁剪区域中的路径或文字区域列表。
	Areas []ClipArea
}

Clip 描述一个裁剪区域定义。

type ClipArea

type ClipArea struct {
	// DrawParam 是区域引用的绘制参数名称。
	DrawParam string
	// CTM 是区域的坐标变换矩阵。
	CTM *CTM
	// Path 是路径裁剪区域;与 Text 二选一。
	Path *ClipPath
	// Text 是文字裁剪区域;与 Path 二选一。
	Text *ClipText
}

ClipArea 描述一个路径或文字裁剪区域。

type ClipPath

type ClipPath struct {
	// Boundary 是路径边界,单位为毫米,且为必填项。
	Boundary Box
	// Name 是路径名称。
	Name string
	// Visible 指定路径是否可见;为空时使用默认值。
	Visible *bool
	// CTM 是路径的坐标变换矩阵。
	CTM *CTM
	// Data 是 OFD 缩略路径语法内容。
	Data string
	// Stroke 指定路径是否描边。
	Stroke bool
	// StrokeSet 指定是否显式设置 Stroke。
	StrokeSet *bool
	// Fill 指定路径是否填充。
	Fill bool
	// Rule 是路径填充规则。
	Rule string
	// LineWidth 是描边线宽,单位为毫米。
	LineWidth float64
	// Cap 是描边端点样式。
	Cap string
	// Join 是描边连接样式。
	Join string
	// MiterLimit 是斜接连接的长度限制。
	MiterLimit float64
	// DashOffset 是虚线起始偏移量,单位为毫米。
	DashOffset float64
	// DashPattern 是虚线长度与间隔组成的序列,单位为毫米。
	DashPattern []float64
	// Alpha 是路径整体透明度,取值范围为 0 到 255。
	Alpha *uint8
	// StrokeColor 是路径描边颜色。
	StrokeColor *Color
	// FillColor 是路径填充颜色。
	FillColor *Color
}

ClipPath 是裁剪区域中的路径,Boundary 为必填边界。

type ClipText

type ClipText struct {
	// Boundary 是文字裁剪区域边界,单位为毫米。
	Boundary Box
	// CTM 是文字的坐标变换矩阵。
	CTM *CTM
	// Font 是字体资源名称;为空时使用 SimSun。
	Font string
	// Size 是字号,单位为毫米。
	Size float64
	// Value 是裁剪文字内容。
	Value string
	// TextCodes 是文字分段及其定位信息。
	TextCodes []TextCode
	// Stroke 指定文字是否描边。
	Stroke bool
	// Fill 指定文字是否填充;为空时使用默认值。
	Fill *bool
	// HScale 是文字水平方向的缩放比例。
	HScale float64
	// ReadDirection 是文字阅读方向。
	ReadDirection int
	// CharDirection 是字符排列方向。
	CharDirection int
	// Weight 是文字字重。
	Weight int
	// Italic 指定文字是否使用斜体。
	Italic bool
	// FillColor 是文字填充颜色。
	FillColor *Color
	// StrokeColor 是文字描边颜色。
	StrokeColor *Color
}

ClipText 是裁剪区域中的文字,Font 为空时使用 SimSun。

type Clips

type Clips struct {
	// Items 是裁剪区域定义列表。
	Items []Clip
	// TransFlag 指定裁剪区域是否应用图元自身的 CTM。缺省(nil)时按规范取
	// true,即裁剪路径坐标处于图元 CTM 之前坐标系;裁剪路径已经是图元局部
	// 毫米坐标时必须显式设为 false,否则阅读器会把 CTM 再叠加一次,把裁剪区
	// 放大 CTM 倍。OFD 规范与现有测试文档中的裁剪区都显式写 false。
	TransFlag *bool
}

Clips 描述图元使用的裁剪区域集合。

type Color

type Color struct {
	// R 是 RGB 红色分量,取值范围为 0 到 255。
	R uint8
	// G 是 RGB 绿色分量,取值范围为 0 到 255。
	G uint8
	// B 是 RGB 蓝色分量,取值范围为 0 到 255。
	B uint8
	// Components 是当前颜色空间中的颜色分量。
	Components []int
	// ColorSpace 是引用的颜色空间资源 ID。
	ColorSpace uint64
	// Index 是颜色空间调色板索引;为空时使用 Components。
	Index *int
	// Alpha 是颜色透明度,取值范围为 0 到 255。
	Alpha *uint8
	// Axial 是轴向渐变配置。
	Axial *AxialShading
	// Radial 是径向渐变配置。
	Radial *RadialShading
	// Gouraud 是三角网格渐变配置。
	Gouraud *GouraudShading
	// LaGouraud 是四边形网格渐变配置。
	LaGouraud *LaGouraudShading
	// Pattern 是平铺图案填充配置。
	Pattern *Pattern
}

Color 描述颜色值或渐变填充。未设置 Components、ColorSpace 和渐变时使用 RGB。

type ColorSpace

type ColorSpace struct {
	// ID 是颜色空间资源标识。
	ID uint64
	// Type 是颜色空间类型。
	Type string
	// BitsPerComponent 是每个颜色分量的位数。
	BitsPerComponent int
	// Profile 是颜色配置文件名称或标识。
	Profile string
	// ProfileData 是颜色配置文件的二进制数据。
	ProfileData []byte
	// Palette 是调色板颜色列表。
	Palette []string
}

ColorSpace 描述 DocumentRes.xml 中的颜色空间资源。

type ColorStop

type ColorStop struct {
	// Position 是渐变位置,通常取值范围为 0 到 1。PositionSet 为假时表示省略。
	Position float64
	// PositionSet 表示 Position 是否显式给出。省略时不写出 XML 属性。
	PositionSet bool
	// Color 是该位置对应的颜色。
	Color Color
}

ColorStop 描述渐变中的颜色分段。

type Composite

type Composite struct {
	// X 是复合图元左上角的横坐标,单位为毫米。
	X float64
	// Y 是复合图元左上角的纵坐标,单位为毫米。
	Y float64
	// Width 是复合图元宽度,单位为毫米。
	Width float64
	// Height 是复合图元高度,单位为毫米。
	Height float64
	// ResourceID 是引用的复合图元资源 ID。
	ResourceID uint64
	// Name 是复合图元名称。
	Name string
	// Visible 指定复合图元是否可见;为空时使用默认值。
	Visible *bool
	// DrawParam 是复合图元引用的绘制参数名称。
	DrawParam string
	// Actions 是复合图元对象的动作。
	Actions []Action
	// CTM 是复合图元的坐标变换矩阵。
	CTM *CTM
	// Clips 是复合图元使用的裁剪区域。
	Clips *Clips
}

Composite 是页面中的复合图元对象。

type CompositeGraphicUnit

type CompositeGraphicUnit struct {
	// ID 是复合图元资源标识,且必须在文档资源中唯一。
	ID uint64
	// Width 是复合图元宽度,单位为毫米。
	Width float64
	// Height 是复合图元高度,单位为毫米。
	Height float64
	// Thumbnail 是复合图元缩略图资源 ID。
	Thumbnail uint64
	// Substitution 是替代复合图元资源 ID。
	Substitution uint64
	// Items 是复合图元内部对象,顺序即绘制顺序。
	Items []Item
}

CompositeGraphicUnit 描述资源中的复合图元定义。 ID 必须显式指定且在文档资源中唯一,Items 保存复合图元内部内容。

type CompressionMode

type CompressionMode string

CompressionMode 表示 OFD ZIP 条目的压缩策略。

const (
	// CompressionAuto 对已压缩格式使用 Store,其他文件使用 Deflate。
	CompressionAuto CompressionMode = "auto"
	// CompressionDeflate 对所有条目使用 Deflate。
	CompressionDeflate CompressionMode = "deflate"
	// CompressionStore 对所有条目使用 Store。
	CompressionStore CompressionMode = "store"
)

func NormalizeCompression added in v0.1.3

func NormalizeCompression(mode CompressionMode) (CompressionMode, error)

NormalizeCompression 返回合法化的压缩策略:空值使用 CompressionAuto, 不支持的值返回错误。

type CreateOptions

type CreateOptions struct {
	Compression CompressionMode
	// CompressionLevel 是 DEFLATE 压缩级别,0 使用默认级别 5,显式范围
	// 1(最快)到 9(最紧凑)。仅对实际使用 Deflate 的条目生效,
	// CompressionStore 或自动选择 Store 的条目不受影响。
	CompressionLevel      int
	Deterministic         bool
	PreserveEmbeddedFonts bool
	// CompleteTextCodeDeltas 按字体度量自动补全缺失的 DeltaX 和 DeltaY。
	CompleteTextCodeDeltas bool
	// DocType 是 OFD.xml 根节点 DocType 属性的取值,留空时使用基础 profile
	// spec.DocTypeOFD。除基础 profile 外,GB/T 42133—2022(档案长期保存,
	// "OFD-A")与 GB/T 48666-2026(电子病历,"OFD-H")分别收紧了 DocType
	// 取值,生成对应文件时须显式设置。
	DocType string
}

CreateOptions 控制 OFD ZIP 包的生成方式。

type CustomData

type CustomData struct {
	// Name 是自定义元数据名称。
	Name string
	// Value 是自定义元数据值。
	Value string
}

CustomData 描述 DocInfo 中的自定义元数据。

type CustomTag

type CustomTag struct {
	// NameSpace 是自定义标签的命名空间。
	NameSpace string
	// Schema 是标签模式文件的二进制数据。
	Schema []byte
	// SchemaName 是模式文件在包内的文件名。
	SchemaName string
	// Data 是自定义标签数据文件的二进制数据。
	Data []byte
	// DataName 是数据文件在包内的文件名。
	DataName string
}

CustomTag 描述一个自定义标签清单及其数据文件。

type DataSource added in v0.1.3

type DataSource interface {
	// Open 返回从头读取资源的 ReadCloser,调用方负责关闭。
	Open() (io.ReadCloser, error)
	// Size 返回资源字节数;未知时返回负数。
	Size() int64
}

DataSource 表示 OFD 包内资源的惰性数据来源,用于在创建超大 OFD 时避免把 大资源整体读入内存。带 Source 的资源优先使用 Source,忽略同名的 Data。

实现必须支持重复 Open,因为创建过程需要多次读取同一来源(摘要、格式嗅探、 实际写入)。

func BytesDataSource added in v0.1.3

func BytesDataSource(data []byte) DataSource

BytesDataSource 返回从内存字节读取的 DataSource。

func FileDataSource added in v0.1.3

func FileDataSource(path string) DataSource

FileDataSource 返回从磁盘文件按需读取的 DataSource。

type Document

type Document struct {
	// ID 是文档标识。
	ID string
	// Title 是文档标题。
	Title string
	// Author 是文档作者。
	Author string
	// Subject 是文档主题。
	Subject string
	// Abstract 是文档摘要。
	Abstract string
	// DocUsage 描述文档用途。
	DocUsage string
	// Cover 是封面图像在包内的文件名或引用名。
	Cover string
	// CoverData 是封面图像的二进制数据。
	CoverData []byte
	// CoverSource 是封面图像的惰性数据来源,优先于 CoverData。
	CoverSource DataSource
	// CoverName 是封面图像写入包时使用的文件名。
	CoverName string
	// Keywords 是文档关键字列表。
	Keywords []string
	// CustomDatas 是文档自定义元数据。
	CustomDatas []CustomData
	// Creator 是创建文档的软件名称。
	Creator string
	// CreatorVersion 是创建软件的版本。
	CreatorVersion string
	// CreationDate 是文档创建时间。
	CreationDate time.Time
	// ModDate 是文档最后修改时间。
	ModDate time.Time
	// PageSize 是文档默认页面尺寸,单位为毫米。
	PageSize PageSize
	// Area 是文档默认页面区域设置。
	Area *PageArea
	// DrawParams 是文档级绘制参数资源。
	DrawParams []DrawParam
	// Fonts 是文档字体资源。
	Fonts []Font
	// ColorSpaces 是文档颜色空间资源。
	ColorSpaces []ColorSpace
	// DefaultCS 是默认颜色空间的资源 ID。
	DefaultCS uint64
	// PublicRes 是文档公共资源文件。
	PublicRes []PublicResource
	// Media 是文档级多媒体资源。
	Media []Media
	// Composites 是文档级复合图元资源。
	Composites []CompositeGraphicUnit
	// Pages 是文档页面,切片索引即页面索引。
	Pages []Page
	// Templates 是文档模板页资源。
	Templates []TemplatePage
	// Actions 是文档级动作。
	Actions []Action
	// Outlines 是文档大纲。
	Outlines []Outline
	// Permissions 是文档权限设置。
	Permissions *Permissions
	// Preferences 是阅读器显示偏好设置。
	Preferences *ViewPreferences
	// Bookmarks 是文档书签。
	Bookmarks []Bookmark
	// Annotations 是按页面组织的注解集合。
	Annotations []AnnotationPage
	// Attachments 是文档附件。
	Attachments []Attachment
	// CustomTags 是文档自定义标签资源。
	CustomTags []CustomTag
	// Extensions 是文档扩展信息。
	Extensions []Extension
	// Signatures 是文档签名清单。
	Signatures []Signature
	// Versions 是文档版本清单。
	Versions []DocumentVersion
}

Document 是 OFD 文件包的输入模型。 页面、对象坐标、对象尺寸和文字字号的单位均为毫米。

type DocumentVersion

type DocumentVersion struct {
	// ID 是文档版本标识。
	ID string
	// Index 是版本在版本清单中的索引。
	Index int
	// Current 指定该版本是否为当前版本。
	Current bool
	// Version 是该版本适用的 OFD 格式版本,如 "1.0"。
	Version string
	// Name 是版本显示名称。
	Name string
	// CreationDate 是版本创建时间。
	CreationDate time.Time
	// Files 是该版本包含的文件清单。
	Files []VersionFile
	// DocRoot 是版本文档根文件的二进制数据。
	DocRoot []byte
	// DocRootName 是文档根文件在包内的文件名。
	DocRootName string
}

DocumentVersion 描述文档的一个版本及其文件清单。

type DrawParam

type DrawParam struct {
	// Name 是绘制参数资源名称。
	Name string
	// Relative 是继承来源绘制参数的 Name。
	Relative string
	// LineWidth 是线宽,单位为毫米。
	LineWidth float64
	// Join 是路径连接处的连接样式。
	Join string
	// Cap 是路径端点的端点样式。
	Cap string
	// DashOffset 是虚线起始偏移量,单位为毫米。
	DashOffset float64
	// DashPattern 是虚线长度与间隔组成的序列,单位为毫米。
	DashPattern []float64
	// MiterLimit 是斜接连接的长度限制。
	MiterLimit float64
	// FillColor 是填充颜色。
	FillColor *Color
	// StrokeColor 是描边颜色。
	StrokeColor *Color
}

DrawParam 描述可被图层或图元引用的绘制参数资源。 Relative 使用另一个绘制参数的 Name 作为继承来源。

type Extension

type Extension struct {
	// AppName 是扩展应用名称。
	AppName string
	// Company 是扩展提供方名称。
	Company string
	// AppVersion 是扩展应用版本。
	AppVersion string
	// Date 是扩展信息的日期。
	Date time.Time
	// RefID 是扩展引用的资源或对象标识。
	RefID uint64
	// Properties 是扩展的键值属性。
	Properties []ExtensionProperty
	// Data 是扩展数据的文本内容。
	Data string
	// DataXML 是 Data 元素中的原始 XML 内容。设置后会作为子元素写入,
	// 与 Data 互斥;Data 适合只需要文本内容的扩展。
	DataXML []byte
	// DataFile 是扩展外部数据文件的二进制内容。
	DataFile []byte
	// DataFileSource 是扩展外部数据文件的惰性数据来源,优先于 DataFile。
	DataFileSource DataSource
	// DataName 是外部数据文件在包内的文件名。
	DataName string
}

Extension 描述文档扩展及其属性、内联数据或外部数据文件。

type ExtensionProperty

type ExtensionProperty struct {
	// Name 是扩展属性名称。
	Name string
	// Type 是扩展属性类型。
	Type string
	// Value 是扩展属性值。
	Value string
}

ExtensionProperty 描述扩展的键值属性。

type Figure added in v0.1.5

type Figure struct {
	// X、Y 是图块左上角坐标,单位毫米。
	X float64
	Y float64
	// Width 是图块宽度,也是图片与说明文字对齐的参照框,单位毫米。
	// 为 0 时图块宽度等于图片实际宽度,居中与居右都退化为无操作。
	Width float64
	// Align 是图片与说明文字共用的水平对齐:left / center / right。
	Align string

	// Image 承载图片数据与外观(边框、透明度、裁剪等)。其中的 X、Y、Width、
	// Height 由 Figure 统一决定,展开时会被覆盖。
	Image Image
	// ImageWidth 是图片宽度,单位毫米;为 0 表示不指定。
	// ImageFillWidth 为真时 ImageWidth 按图块宽度的比例解释(0.5 即一半)。
	ImageWidth float64
	// ImageFillWidth 为真时 ImageWidth 按图块宽度的比例解释。
	ImageFillWidth bool
	// ImageHeight 是图片高度,单位毫米;为 0 时按图片原始比例推出。
	ImageHeight float64
	// Fit 是图片在给定尺寸内的适配方式:
	// "contain"(默认)保持原始比例,两个尺寸都给时按小的比例缩放;
	// "stretch" 拉伸填满,此时两个尺寸都必须给。
	Fit string

	// Caption 是图片下方的说明文字,如 "图 3 营收走势"。留空不画说明。
	Caption string
	// CaptionStyle 是说明文字样式;Gap 指定说明与图片底边的间距,
	// 为 0 时用 defaultCaptionGap。
	CaptionStyle *CaptionStyle
}

Figure 是带说明文字的图片块,生成时展开为一个 ImageObject 与一个 TextObject。

单独的 Image 图元对应 OFD 的 ImageObject,是个叶子图元,没有"图片带标题"的 概念;而带说明的图在公文、报告、论文里是极常见的排版单元,所以另立一个复合 图元,而不是往 Image 上加说明字段。

type Font

type Font struct {
	// Name 是字体资源名称,必须与文字对象的 Font 值一致。
	Name string
	// FamilyName 是字体族名称。
	FamilyName string
	// Charset 是字体字符集标识。
	Charset string
	// Italic 指定字体是否为斜体。
	Italic bool
	// Bold 指定字体是否为粗体。
	Bold bool
	// Serif 指定字体是否为衬线字体。
	Serif bool
	// FixedWidth 指定字体是否为等宽字体。
	FixedWidth bool
	// Format 是字体数据格式。
	Format string
	// Data 是嵌入字体的二进制数据。
	Data []byte
	// Source 是嵌入字体的惰性数据来源,优先于 Data。
	// 默认的子集化需要完整字体字节,会在子集化时逐字体读取并物化来源。
	Source DataSource
}

Font 描述文档字体资源,Name 必须与 Text.Font 的值一致。 Data 非空时,字体数据会嵌入 OFD 文件包。

type GotoAAction

type GotoAAction struct {
	// AttachID 是目标附件标识。
	AttachID string
	// NewWindow 指定是否在新窗口打开附件;为空时使用默认值。
	NewWindow *bool
}

GotoAAction 描述跳转到文档附件的动作。

type GotoAction

type GotoAction struct {
	// Page 是从 0 开始的目标页面索引。
	Page int
	// Bookmark 是目标书签名称。
	Bookmark string
	// Type 是页面适配方式;为空时使用 Fit。
	Type string
	// Left 是目标视图左边界,单位为毫米。
	Left *float64
	// Top 是目标视图上边界,单位为毫米。
	Top *float64
	// Right 是目标视图右边界,单位为毫米。
	Right *float64
	// Bottom 是目标视图下边界,单位为毫米。
	Bottom *float64
	// Zoom 是目标视图缩放比例。
	Zoom *float64
}

GotoAction 描述当前文档内的页面跳转动作。 Page 是从 0 开始的页面索引;Type 为空时使用 Fit。

type GouraudPoint

type GouraudPoint struct {
	// X 是控制点横坐标,单位为毫米。
	X float64
	// Y 是控制点纵坐标,单位为毫米。
	Y float64
	// EdgeFlag 是控制点所在网格的方向标志,决定本点如何与前一个三角形衔接。
	// 设前一个三角形为 (V0,V1,V2),取值含义见 GB/T 33190 图 40:
	//
	//	0:本点与随后两点另起一个三角形,与前一个三角形无共享边。
	//	1:本点复用 V1-V2 边,两个三角形拼成四边形。
	//	2:本点复用 V0-V2 边,两个三角形拼成四边形。
	//
	// 缺省为 0。只有 1、2 会产生共享边从而拼成连续网格。规范未定义 3,
	// 部分阅读器按 PDF 语义复用 V0-V1 边做兼容处理。
	EdgeFlag int
	// Color 是控制点颜色。
	Color Color
}

GouraudPoint 描述三角网格渐变控制点。

type GouraudShading

type GouraudShading struct {
	// Extend 指定网格边界外的延展方式。
	Extend int
	// Points 是三角网格控制点列表。
	Points []GouraudPoint
	// BackColor 是网格背景颜色。
	BackColor *Color
}

GouraudShading 描述三角网格渐变。

type Image

type Image struct {
	// X 是图片对象左上角的横坐标,单位为毫米。
	X float64
	// Y 是图片对象左上角的纵坐标,单位为毫米。
	Y float64
	// Width 是图片对象宽度,单位为毫米。
	Width float64
	// Height 是图片对象高度,单位为毫米。
	Height float64
	// Data 是图片的二进制数据。
	Data []byte
	// Source 是图片的惰性数据来源,优先于 Data。
	Source DataSource
	// ResourceID 是引用的图片资源 ID。
	ResourceID uint64
	// Substitution 是替代图片资源 ID。
	Substitution uint64
	// ImageMask 是图片蒙版资源 ID。
	ImageMask uint64
	// Format 是图片格式,如 PNG 或 JPEG。
	Format string
	// Name 是图片名称。
	Name string
	// Visible 指定图片是否可见;为空时使用默认值。
	Visible *bool
	// LineWidth 是图片边框线宽,单位为毫米。
	LineWidth float64
	// Cap 是图片边框端点样式。
	Cap string
	// Join 是图片边框连接样式。
	Join string
	// MiterLimit 是图片边框斜接连接的长度限制。
	MiterLimit float64
	// DashOffset 是图片边框虚线起始偏移量,单位为毫米。
	DashOffset float64
	// DashPattern 是图片边框虚线长度与间隔序列,单位为毫米。
	DashPattern []float64
	// Alpha 是图片整体透明度,取值范围为 0 到 255。
	Alpha *uint8
	// DrawParam 是图片引用的绘制参数名称。
	DrawParam string
	// Actions 是图片对象的动作。
	Actions []Action
	// CTM 是图片对象的坐标变换矩阵。
	CTM *CTM
	// Clips 是图片对象使用的裁剪区域。
	Clips *Clips
	// Border 是图片边框样式。
	Border *ImageBorder
}

Image 是图片对象,Data 保存编码后的图片数据。 Format 是 PNG 或 JPEG 等 OFD 图片格式;为空时会尽量根据文件头识别。

type ImageBorder

type ImageBorder struct {
	// LineWidth 是边框线宽,单位为毫米。
	LineWidth float64
	// HorizontalRadius 是水平圆角半径,单位为毫米。
	HorizontalRadius float64
	// VerticalRadius 是垂直圆角半径,单位为毫米。
	VerticalRadius float64
	// DashOffset 是虚线起始偏移量,单位为毫米。
	DashOffset float64
	// DashPattern 是虚线长度与间隔组成的序列,单位为毫米。
	DashPattern []float64
	// Color 是边框颜色。
	Color *Color
}

ImageBorder 描述图片边框的线宽、圆角和虚线样式。

type Item

type Item interface {
	// contains filtered or unexported methods
}

Item 是 OFD 页面上的对象,可使用 Text、Path、Image、Composite 或 PageBlock。

type LaGouraudPoint

type LaGouraudPoint struct {
	// X 是控制点横坐标,单位为毫米。
	X float64
	// Y 是控制点纵坐标,单位为毫米。
	Y float64
	// Color 是控制点颜色。
	Color Color
}

LaGouraudPoint 描述四边形网格渐变控制点。

type LaGouraudShading

type LaGouraudShading struct {
	// VerticesPerRow 是每行顶点数量。
	VerticesPerRow int
	// Extend 指定网格边界外的延展方式。
	Extend int
	// Points 是四边形网格控制点列表。
	Points []LaGouraudPoint
	// BackColor 是网格背景颜色。
	BackColor *Color
}

LaGouraudShading 描述四边形网格渐变。

type Layer

type Layer struct {
	// Type 是图层类型。
	Type string
	// DrawParam 是图层引用的绘制参数名称。
	DrawParam string
	// Items 是图层中的页面对象,顺序即绘制顺序。
	Items []Item
}

Layer 表示页面中的一个图层,图层顺序就是绘制顺序。

type LimitWriter added in v0.1.3

type LimitWriter struct {
	// contains filtered or unexported fields
}

LimitWriter 在复制过程中同时限制单条与总解压字节数,防止声明的解压 大小与实际不符(如 zip bomb)。

func NewLimitWriter added in v0.1.3

func NewLimitWriter(writer io.Writer, name string, entryRemaining, maxTotal int64, totalRemaining *int64) *LimitWriter

NewLimitWriter 返回带限制的 writer;totalRemaining 指向调用方的总预算, 每次写入后同步扣减。

func (*LimitWriter) Write added in v0.1.3

func (l *LimitWriter) Write(p []byte) (int, error)

type Limits added in v0.1.3

type Limits struct {
	// MaxEntries 是允许搬运的最大条目数,0 表示默认 10000。
	MaxEntries int
	// MaxEntryBytes 是单个条目解压后的最大字节数,0 表示默认 64MB。
	MaxEntryBytes int64
	// MaxTotalBytes 是所有条目解压后的总字节上限,0 表示默认 512MB。
	MaxTotalBytes int64
}

Limits 限制 OFD 包读写的规模,避免恶意或异常文档造成的解压放大。

type Media

type Media struct {
	// ID 是多媒体资源标识。
	ID uint64
	// Type 是媒体类型,如图片、音频或视频。
	Type string
	// Format 是媒体编码格式。
	Format string
	// Data 是媒体的二进制数据。
	Data []byte
	// Source 是媒体的惰性数据来源,优先于 Data。
	Source DataSource
	// Name 是媒体文件在包内的文件名。
	Name string
}

Media 描述文档级图片、音频或视频资源。

type MovieAction

type MovieAction struct {
	// ResourceID 是影片资源 ID。
	ResourceID uint64
	// Operator 是影片控制操作。
	Operator string
}

MovieAction 描述影片控制动作。

type Outline

type Outline struct {
	// Title 是大纲项标题。
	Title string
	// Count 是子项数量或展开状态相关的计数值。
	Count *int
	// Expanded 指定大纲项是否展开;为空时使用默认值。
	Expanded *bool
	// Actions 是大纲项的跳转动作。
	Actions []Action
	// Children 是嵌套的大纲项。
	Children []Outline
}

Outline 描述文档大纲项。大纲动作使用当前文档页面索引作为目标。

type Page

type Page struct {
	// Area 是页面区域设置。
	Area *PageArea
	// Resources 是页面专属资源文件。
	Resources []PageResource
	// Templates 是页面引用的模板页。
	Templates []TemplateRef
	// LayerType 是页面默认图层类型。
	LayerType string
	// Items 是按绘制顺序排列的页面对象。
	Items []Item
	// Layers 是页面图层,切片顺序即绘制顺序。
	Layers []Layer
	// Actions 是页面动作。
	Actions []Action
}

Page 按绘制顺序保存页面对象。

type PageArea

type PageArea struct {
	// PhysicalBox 是页面物理边界;为空时使用文档页面尺寸。
	PhysicalBox *Box
	// ApplicationBox 是应用程序可用的页面区域。
	ApplicationBox *Box
	// ContentBox 是页面内容的布局区域。
	ContentBox *Box
	// BleedBox 是包含出血范围的页面区域。
	BleedBox *Box
}

PageArea 描述页面的物理区域、应用区域、内容区域和出血区域。 PhysicalBox 为空时使用文档的页面尺寸。

type PageBlock

type PageBlock struct {
	// Items 是容器中的页面对象,顺序即绘制顺序。
	Items []Item
}

PageBlock 是可嵌套的页面对象容器。

type PageImage

type PageImage struct {
	// ID 是图片资源标识,且必须在文档内唯一。
	ID uint64
	// Format 是图片格式,如 PNG 或 JPEG。
	Format string
	// Data 是图片的二进制数据。
	Data []byte
	// Source 是图片的惰性数据来源,优先于 Data。
	Source DataSource
	// Name 是图片在包内的文件名。
	Name string
}

PageImage 描述页面资源中的图片。ID 在文档内必须唯一。

type PageProvider added in v0.1.3

type PageProvider interface {
	// PageCount 返回文档页面总数。
	PageCount() int
	// PageAt 返回从 0 开始的第 index 页。
	PageAt(index int) (Page, error)
}

PageProvider 按索引提供页面,用于在页数或页模型大于内存时流式创建 OFD。

创建过程会在 ID 预留/校验阶段和写出阶段多次访问同一索引,因此实现必须能够 重复返回相同的页面。实现可以从磁盘、数据库或生成器中按需构造页面。

type PageResource

type PageResource struct {
	// Images 是页面图片资源的快捷配置。
	Images []PageImage
	// Data 是完整的 OFD Res XML。用于页面专属的颜色空间、字体、绘制参数、
	// 多媒体或复合图元等资源;设置后不能同时设置 Images。
	Data []byte
	// Files 是 Data 中引用的、相对于 PageRes XML 所在目录的二进制文件。
	Files []PageResourceFile
}

PageResource 描述页面专属的资源文件。Images 适合快速创建页面图片资源; Data 可用于完整的 OFD Res 资源集合。

type PageResourceFile

type PageResourceFile struct {
	// Path 是相对于页面资源 XML 所在目录的文件路径。
	Path string
	// Data 是文件的二进制数据。
	Data []byte
	// Source 是文件的惰性数据来源,优先于 Data。
	Source DataSource
}

PageResourceFile 描述页面资源 XML 引用的包内文件。

type PageSize

type PageSize struct {
	// Width 是页面宽度,单位为毫米。
	Width float64
	// Height 是页面高度,单位为毫米。
	Height float64
}

PageSize 描述页面尺寸,单位为毫米。

type Path

type Path struct {
	// X 是路径对象左上角的横坐标,单位为毫米。
	X float64
	// Y 是路径对象左上角的纵坐标,单位为毫米。
	Y float64
	// Width 是路径对象宽度,单位为毫米。
	Width float64
	// Height 是路径对象高度,单位为毫米。
	Height float64
	// Data 是 OFD 缩略路径语法内容。
	Data string
	// Stroke 指定路径是否描边。
	Stroke bool
	// StrokeSet 指定是否显式设置 Stroke。
	StrokeSet *bool
	// Fill 指定路径是否填充。
	Fill bool
	// Rule 是路径填充规则。
	Rule string
	// Name 是路径名称。
	Name string
	// Visible 指定路径是否可见;为空时使用默认值。
	Visible *bool
	// LineWidth 是描边线宽,单位为毫米。
	LineWidth float64
	// Cap 是描边端点样式。
	Cap string
	// Join 是描边连接样式。
	Join string
	// MiterLimit 是斜接连接的长度限制。
	MiterLimit float64
	// DashOffset 是虚线起始偏移量,单位为毫米。
	DashOffset float64
	// DashPattern 是虚线长度与间隔组成的序列,单位为毫米。
	DashPattern []float64
	// Alpha 是路径整体透明度,取值范围为 0 到 255。
	Alpha *uint8
	// DrawParam 是路径引用的绘制参数名称。
	DrawParam string
	// Actions 是路径对象的动作。
	Actions []Action
	// CTM 是路径对象的坐标变换矩阵。
	CTM *CTM
	// Clips 是路径对象使用的裁剪区域。
	Clips *Clips
	// StrokeColor 是路径描边颜色。
	StrokeColor *Color
	// FillColor 是路径填充颜色。
	FillColor *Color
}

Path 是路径对象。Data 使用 OFD 缩略路径语法,例如 "M 10 10 L 100 10 L 100 100 C"。

type Pattern

type Pattern struct {
	// Width 是图案单元宽度,单位为毫米。
	Width float64
	// Height 是图案单元高度,单位为毫米。
	Height float64
	// XStep 是图案水平方向重复步长,单位为毫米。
	XStep float64
	// YStep 是图案垂直方向重复步长,单位为毫米。
	YStep float64
	// ReflectMethod 是图案重复时的镜像方式。
	ReflectMethod string
	// RelativeTo 是图案坐标相对于页面或对象的参考系。
	RelativeTo string
	// CTM 是图案单元的坐标变换矩阵。
	CTM *CTM
	// Thumbnail 是图案缩略图资源 ID。
	Thumbnail uint64
	// Items 是图案单元中的页面对象。
	Items []Item
	// Layers 是图案单元中的图层。
	Layers []Layer
	// contains filtered or unexported fields
}

Pattern 描述用于填充颜色的平铺图案。 图案单元中的对象使用 Items 或 Layers 配置,坐标单位为毫米。

type Permissions

type Permissions struct {
	// Edit 指定是否允许编辑文档内容。
	Edit *bool
	// Annot 指定是否允许添加或修改注解。
	Annot *bool
	// Export 指定是否允许导出文档内容。
	Export *bool
	// Signature 指定是否允许签名。
	Signature *bool
	// Watermark 指定是否允许添加或修改水印。
	Watermark *bool
	// PrintScreen 指定是否允许屏幕截图。
	PrintScreen *bool
	// Print 是打印权限及打印份数设置。
	Print *PrintSettings
	// ValidPeriod 是权限生效和失效时间范围。
	ValidPeriod *ValidPeriod
}

Permissions 描述文档编辑、导出、打印等权限。

type Point

type Point struct {
	// X 是点的横坐标,单位为毫米。
	X float64
	// Y 是点的纵坐标,单位为毫米。
	Y float64
}

Point 描述区域路径中的二维点。

type PrintSettings

type PrintSettings struct {
	// Printable 指定是否允许打印。
	Printable bool
	// Copies 是允许打印的份数;为空时不限定份数。
	Copies *int
}

PrintSettings 描述打印权限和允许的打印份数。

type PublicResource

type PublicResource struct {
	// Name 是相对于 Doc_0 的公共资源 XML 文件名。
	Name string
	// Data 是 Res 根元素的 XML 内容。
	Data []byte
	// Files 是资源 XML 引用的包内文件。
	Files []PublicResourceFile
}

PublicResource 描述一个嵌入 OFD 包的公共资源 XML 文件。 Name 是相对于 Doc_0 的文件名,Data 应为 Res 根元素的 XML 内容。

type PublicResourceFile

type PublicResourceFile struct {
	// Path 是相对于公共资源 XML 所在目录的文件路径。
	Path string
	// Data 是文件的二进制数据。
	Data []byte
	// Source 是文件的惰性数据来源,优先于 Data。
	Source DataSource
}

PublicResourceFile 描述公共资源 XML 引用的包内文件。 Path 相对于公共资源 XML 所在目录。

type RadialShading

type RadialShading struct {
	// MapType 是渐变映射类型。
	MapType string
	// MapUnit 是渐变映射单位。
	MapUnit float64
	// Eccentricity 是径向渐变椭圆离心率。
	Eccentricity float64
	// Angle 是径向渐变旋转角度。
	Angle float64
	// StartPoint 是渐变起点坐标。
	StartPoint string
	// StartRadius 是渐变起始半径。
	StartRadius float64
	// EndPoint 是渐变终点坐标。
	EndPoint string
	// EndRadius 是渐变结束半径。
	EndRadius float64
	// Extend 指定渐变端点外的延展方式。
	Extend int
	// Segments 是按位置排列的颜色分段。
	Segments []ColorStop
}

RadialShading 描述径向渐变。

type RegionArc

type RegionArc struct {
	// SweepDirection 指定圆弧的扫描方向。
	SweepDirection bool
	// LargeArc 指定是否选择大于半圆的弧段。
	LargeArc bool
	// RotationAngle 是椭圆旋转角度。
	RotationAngle float64
	// EllipseSize 是椭圆的半轴尺寸,单位为毫米。
	EllipseSize Point
	// EndPoint 是圆弧终点,单位为毫米。
	EndPoint Point
}

RegionArc 描述椭圆弧。

type RegionClose

type RegionClose struct{}

RegionClose 关闭当前路径。

type RegionCommand

type RegionCommand interface {
	// contains filtered or unexported methods
}

RegionCommand 是动作区域路径命令。

type RegionCubicBezier

type RegionCubicBezier struct {
	// Control1 是三次贝塞尔曲线的第一个控制点,单位为毫米。
	Control1 Point
	// Control2 是三次贝塞尔曲线的第二个控制点,单位为毫米。
	Control2 Point
	// End 是三次贝塞尔曲线的终点,单位为毫米。
	End Point
}

RegionCubicBezier 描述三次贝塞尔曲线。

type RegionLine

type RegionLine struct {
	// Point 是连接到的目标点,单位为毫米。
	Point Point
}

RegionLine 连接到指定点。

type RegionMove

type RegionMove struct {
	// Point 是移动到的目标点,单位为毫米。
	Point Point
}

RegionMove 移动到指定点。

type RegionQuadraticBezier

type RegionQuadraticBezier struct {
	// Control 是二次贝塞尔曲线的控制点,单位为毫米。
	Control Point
	// End 是二次贝塞尔曲线的终点,单位为毫米。
	End Point
}

RegionQuadraticBezier 描述二次贝塞尔曲线。

type Signature

type Signature struct {
	// ID 是签名标识。
	ID string
	// Type 是签名类型。
	Type string
	// ProviderName 是签名提供方名称。
	ProviderName string
	// ProviderVersion 是签名提供方版本。
	ProviderVersion string
	// Company 是签名方名称。
	Company string
	// Method 是签名方法标识。
	Method string
	// Date 是签名生成时间。
	Date time.Time
	// CheckMethod 是签名引用的摘要算法。
	CheckMethod string
	// References 是签名覆盖的文件摘要引用。
	References []SignatureReference
	// StampAnnots 是与签名关联的印章注解。
	StampAnnots []SignatureStamp
	// SealFile 是印章文件的二进制数据。
	SealFile []byte
	// SealSource 是印章文件的惰性数据来源,优先于 SealFile。
	SealSource DataSource
	// SealName 是印章文件在包内的文件名。
	SealName string
	// SignedValue 是签名值文件的二进制数据。
	SignedValue []byte
	// SignedValueSource 是签名值文件的惰性数据来源,优先于 SignedValue。
	SignedValueSource DataSource
	// SignedValueName 是签名值文件在包内的文件名。
	SignedValueName string
}

Signature 描述一个 OFD 签名清单项及其签名文件。

type SignatureMode added in v0.1.3

type SignatureMode string

SignatureMode 控制处理 OFD 包时签名文件的去向。

const (
	// SignatureDrop 删除签名目录,产出无签名文档。
	SignatureDrop SignatureMode = "drop"
	// SignaturePreserve 原样保留签名文件。
	SignaturePreserve SignatureMode = "preserve"
	// SignatureRewrite 保留签名文件但重写其中的包内绝对路径,签名值通常失效。
	SignatureRewrite SignatureMode = "rewrite"
)

type SignatureReference

type SignatureReference struct {
	// FileRef 是相对于签名 XML 文件的被签名文件路径。
	FileRef string
	// CheckValue 是被引用文件的摘要值。
	CheckValue []byte
}

SignatureReference 描述签名摘要引用。FileRef 相对于签名 XML 文件。

type SignatureStamp

type SignatureStamp struct {
	// ID 是印章标识。
	ID string
	// Page 是从 0 开始的盖章页面索引。
	Page int
	// Boundary 是印章在页面上的边界,单位为毫米。
	Boundary Box
	// Clip 是印章内容的裁剪区域,单位为毫米。
	Clip *Box
}

SignatureStamp 描述签名在页面上的盖章区域。

type SoundAction

type SoundAction struct {
	// ResourceID 是声音资源 ID。
	ResourceID uint64
	// Volume 是播放音量,通常为百分比整数。
	Volume *int
	// Repeat 指定是否循环播放;为空时使用默认值。
	Repeat *bool
	// Synchronous 指定是否同步播放;为空时使用默认值。
	Synchronous *bool
}

SoundAction 描述声音播放动作。

type Table added in v0.1.5

type Table struct {
	// X 是表格左上角的横坐标,单位为毫米。
	X float64
	// Y 是表格左上角的纵坐标,单位为毫米。
	Y float64
	// Width 是表格总宽度,单位为毫米。
	Width float64
	// Rows 是表格的行,每行包含若干单元格。
	Rows []TableRow
	// ColumnWidths 是各列宽度(毫米),为空时均分 Width。
	ColumnWidths []float64
	// Style 是表格整体样式。
	Style TableStyle
	// HeaderRows 是表头行数,跨页时重复显示。
	HeaderRows int
	// Visible 指定表格是否可见。
	Visible *bool
	// DrawParam 是表格引用的绘制参数名称。
	DrawParam string
	// Caption 是表题文字,如 "表 13 文件属性"。
	Caption string
	// CaptionStyle 是表题样式。
	CaptionStyle *CaptionStyle
	// ContinuationCaption 是续表题文字,支持 {caption} 占位符。
	ContinuationCaption string
	// ContinuationCaptionStyle 是续表题样式。
	ContinuationCaptionStyle *CaptionStyle
}

Table 是页面中的表格对象,自动跨页,支持边框控制和单元格样式。

type TableCell added in v0.1.5

type TableCell struct {
	// Value 是单元格文本内容。
	Value string
	// Font 是字体资源名称。
	Font string
	// Size 是字号,单位为毫米。
	Size float64
	// Weight 是字重。
	Weight int
	// Italic 指定是否斜体。
	Italic bool
	// Align 是文本对齐方式:left/center/right。
	Align string
	// ColSpan 是横向合并的列数。
	ColSpan int
	// RowSpan 是纵向合并的行数。
	RowSpan int
	// FirstLineIndent 是首行缩进,单位为毫米。
	FirstLineIndent float64
	// HangingIndent 是续行(第 2 行起)缩进,单位为毫米。
	HangingIndent float64
	// Format 指定单元格文本格式:"text"(默认,纯文本)或 "markdown"。
	// markdown 解析行内语法(**粗体**、*斜体*、`代码`、~~删除线~~)与列表
	// (- 无序、1. 有序,按层级缩进);源码里的换行一律断行,不按 Markdown
	// 正文的软换行约定折成空格。
	Format string
	// FillColor 是单元格背景色。
	FillColor *Color
	// Runs 是单元格内带样式的文字片段。非空时按片段逐段渲染,Value 与 Format
	// 都被忽略;两者与 Runs 同时设置会被校验拒绝,避免「设了却不生效」的静默分歧。
	//
	// 用途是把已经解析好的行内结构原样传进来,不必先拼成 markdown 源码再让创建器
	// 重新解析一遍——往返会出问题:正文里的 `*` `_` `[` 是普通字符,重新拼串再解析
	// 就会变成斜体或坏掉链接。
	Runs []TextRun
	// Actions 是单元格的点击动作,作用范围是整个单元格。
	//
	// 表格文字在落笔时按样式拆成多个文字对象,单元格级的动作会挂到其中每一个
	// 上:热区因此覆盖整格,与 PDF 表格里链接注解盖住单元格的做法一致。要让
	// 单元格里的某几段文字各自可点,得把动作写在那些文字对象上。
	Actions []Action
}

TableCell 是表格中的一个单元格。

type TableRow added in v0.1.5

type TableRow struct {
	// Cells 是该行的单元格列表。
	Cells []TableCell
	// Height 是行高(毫米),为 0 时根据内容自动计算。
	Height float64
}

TableRow 是表格中的一行。

type TableStyle added in v0.1.5

type TableStyle struct {
	// HorizontalLines 控制是否绘制横线。
	HorizontalLines bool
	// VerticalLines 控制是否绘制竖线。
	VerticalLines bool
	// LineWidth 是内部线宽,单位为毫米。
	LineWidth float64
	// StrokeColor 是边框颜色。
	StrokeColor *Color
	// OuterLineWidth 是外框线宽,为 0 时使用 LineWidth。
	OuterLineWidth float64
	// HeaderLineWidth 是表头下方分隔线宽,为 0 时使用 LineWidth。
	HeaderLineWidth float64
	// OuterStrokeColor 是外框颜色,为空时使用 StrokeColor。
	OuterStrokeColor *Color
	// HeaderStrokeColor 是表头分隔线颜色,为空时使用 StrokeColor。
	HeaderStrokeColor *Color
	// HeaderFillColor 是表头背景色。
	HeaderFillColor *Color
	// HeaderFont 是表头字体资源名称。
	HeaderFont string
	// HeaderSize 是表头字号。
	HeaderSize float64
	// HeaderWeight 是表头字重。
	HeaderWeight int
	// StrikeColor 是 Markdown 删除线(~~删除~~)的颜色;为空时与表格边框同色。
	StrikeColor *Color
	// LinkColor 是 Markdown 行内链接([文字](网址))的文字颜色;为空时用默认链接蓝。
	LinkColor *Color
	// RuleColor 是 Markdown 分隔线(---)的颜色;为空时与表格边框同色。
	RuleColor *Color
	// CodeFont 是 Markdown 行内代码(`code`)使用的字体资源名称,
	// 为空时沿用单元格字体。
	CodeFont string
}

TableStyle 控制表格边框样式。

type TemplatePage

type TemplatePage struct {
	// ID 是模板页标识,且必须在文档内唯一。
	ID uint64
	// Name 是模板页名称。
	Name string
	// ZOrder 是模板页相对于页面内容的绘制顺序。
	ZOrder string
	// Area 是模板页区域设置。
	Area *PageArea
	// Items 是模板页中的页面对象。
	Items []Item
	// Layers 是模板页中的图层。
	Layers []Layer
}

TemplatePage 描述文档模板页及其页面内容。 ID 必须在文档内唯一,页面通过该 ID 引用模板页。

type TemplateRef

type TemplateRef struct {
	// ID 是被引用模板页的标识。
	ID uint64
	// ZOrder 是模板页相对于页面内容的绘制顺序。
	ZOrder string
}

TemplateRef 描述页面使用的模板页引用。

type Text

type Text struct {
	// X 是文字对象左上角的横坐标,单位为毫米。
	X float64
	// Y 是文字对象左上角的纵坐标,单位为毫米。
	Y float64
	// Width 是文字对象宽度,单位为毫米。
	Width float64
	// Height 是文字对象高度,单位为毫米。
	Height float64
	// Value 是文字内容。
	Value string
	// Font 是字体资源名称。
	Font string
	// Size 是字号,单位为毫米。
	Size float64
	// Visible 指定文字对象是否可见;为空时使用默认值。
	Visible *bool
	// Stroke 指定文字是否描边。
	Stroke bool
	// Fill 指定文字是否填充;为空时使用默认值。
	Fill *bool
	// HScale 是文字水平方向的缩放比例。
	HScale float64
	// ReadDirection 是文字阅读方向。
	ReadDirection int
	// CharDirection 是字符排列方向。
	CharDirection int
	// Weight 是文字字重。
	Weight int
	// Italic 指定文字是否使用斜体。
	Italic bool
	// DrawParam 是文字对象引用的绘制参数名称。
	DrawParam string
	// CTM 是文字对象的坐标变换矩阵。
	CTM *CTM
	// TextCodes 是文字分段及其定位信息。
	TextCodes []TextCode
	// CGTransforms 是字符到字形的映射关系。
	CGTransforms []CGTransform
	// Actions 是文字对象的动作。
	Actions []Action
	// Clips 是文字对象使用的裁剪区域。
	Clips *Clips
	// FillColor 是文字填充颜色。
	FillColor *Color
	// StrokeColor 是文字描边颜色。
	StrokeColor *Color
}

Text 是文字对象,Size 表示字号,单位为毫米。

type TextCode

type TextCode struct {
	// Value 是该文字分段的内容,可为纯空白字符串,但不能是空字符串。
	Value string
	// X 是分段起始横坐标,单位为毫米;为空时沿用排版位置。
	X *float64
	// Y 是分段起始纵坐标,单位为毫米;为空时沿用排版位置。
	Y *float64
	// DeltaX 是各字符的横向位置增量,单位为毫米。
	DeltaX []float64
	// DeltaY 是各字符的纵向位置增量,单位为毫米。
	DeltaY []float64
}

TextCode 描述文字对象中的一段文字及其定位信息。 X、Y 为空时沿用文字对象的排版位置;DeltaX、DeltaY 表示字符位置增量。

type TextRun added in v0.1.5

type TextRun struct {
	// Text 是这一段的文字。
	Text string
	// Bold 为真时按粗体渲染。
	Bold bool
	// Italic 为真时按斜体渲染。
	Italic bool
	// Code 为真时用表格的 CodeFont 渲染。
	Code bool
	// Strike 为真时在文字中部画删除线。
	Strike bool
	// Href 是这一段的点击目标。非空时只有这一段可点,并按链接蓝着色;为空时沿用
	// 单元格的 Actions。
	Href string
}

TextRun 是表格单元格内的一段同样式文字。

与 TableCell 上的整格样式字段并存而不是取代它们:整格字段是单段单元格的简写, Runs 处理一段里还要分粗体、行内代码、链接的情况。

type URIAction

type URIAction struct {
	// URI 是要访问的统一资源标识符。
	URI string
	// Base 是解析相对 URI 时使用的基础 URI。
	Base string
	// Target 是 URI 打开的目标窗口或框架。
	Target string
}

URIAction 描述外部 URI 动作。

type ValidPeriod

type ValidPeriod struct {
	// Start 是权限生效时间。
	Start time.Time
	// End 是权限失效时间。
	End time.Time
}

ValidPeriod 描述权限的生效和失效时间。

type VersionFile

type VersionFile struct {
	// ID 是版本文件标识。
	ID string
	// Path 是相对于文档目录(Doc_0)的包内路径。
	Path string
}

VersionFile 描述文档版本中的文件。

type ViewPreferences

type ViewPreferences struct {
	// PageMode 是文档打开时的页面导航模式。
	PageMode string
	// PageLayout 是文档打开时的页面布局模式。
	PageLayout string
	// TabDisplay 是阅读器标签页显示的标题来源。
	TabDisplay string
	// HideToolbar 指定是否隐藏工具栏;为空时使用默认值。
	HideToolbar *bool
	// HideMenubar 指定是否隐藏菜单栏;为空时使用默认值。
	HideMenubar *bool
	// HideWindowUI 指定是否隐藏窗口界面元素;为空时使用默认值。
	HideWindowUI *bool
	// ZoomMode 是文档打开时的缩放模式。
	ZoomMode string
	// Zoom 是自定义缩放比例。
	Zoom *float64
}

ViewPreferences 描述阅读器打开文档时的显示设置。

Jump to

Keyboard shortcuts

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