document

package
v0.0.0-...-4d6acc0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedFormat = errors.New("unsupported document format")
	ErrInvalidFormat     = errors.New("invalid document format")
	ErrEncryptedDocument = errors.New("encrypted document not supported")
	ErrSheetNotFound     = errors.New("sheet not found")
	ErrDocumentNotLoaded = errors.New("document not loaded")
	ErrSaveFailed        = errors.New("save failed")
	ErrOpenFailed        = errors.New("open failed")
)

Common errors for document processing.

View Source
var BufferPool = sync.Pool{
	New: func() any {
		return new(bytes.Buffer)
	},
}

BufferPool is a pool of bytes.Buffer objects to reduce allocations.

View Source
var XMLDecoderPool = sync.Pool{
	New: func() any {
		return xml.NewDecoder(bytes.NewReader(nil))
	},
}

XMLDecoderPool is a pool of xml.Decoder objects.

View Source
var XMLEncoderPool = sync.Pool{
	New: func() any {
		return xml.NewEncoder(io.Discard)
	},
}

XMLEncoderPool is a pool of xml.Encoder objects.

Functions

func GetBuffer

func GetBuffer() *bytes.Buffer

GetBuffer returns a bytes.Buffer from the pool.

func PutBuffer

func PutBuffer(buf *bytes.Buffer)

PutBuffer returns a bytes.Buffer to the pool, resetting it first.

Types

type Cell

type Cell interface {
	Set(value any) Cell
	Formula(formula string) Cell
	Hyperlink(url string) Cell
	Style(style CellStyle) Cell
	Comment(text string) Cell
	Get() (string, error)
	Err() error
}

Cell is a fluent handle bound to a specific cell in a sheet.

type CellStyle

type CellStyle struct {
	Bold          bool
	Italic        bool
	Size          int
	Color         string // Hexadecimal color code, e.g., "FF0000"
	Background    string // Hexadecimal color code
	Border        bool
	BorderTop     bool
	BorderBottom  bool
	BorderLeft    bool
	BorderRight   bool
	BorderWidth   float64
	BorderColor   string
	Horizontal    string // "left", "center", "right"
	Vertical      string // "top", "center", "bottom"
	NumberFormat  string // e.g., "0.00", "mm-dd-yy", "#,##0.00"
	Name          string // Named style, e.g., "Heading1"
	Link          string // Hyperlink URL
	Font          string // Custom font name
	Alt           string // Alternative text for accessibility
	LineSpacing   float64
	SpacingBefore float64
	SpacingAfter  float64
	Indent        float64   // Left indent in points
	Hanging       float64   // Hanging indent in points
	Padding       float64   // Cell padding in points
	KeepWithNext  bool      // Ensure paragraph stays on same page as next item
	KeepTogether  bool      // Ensure paragraph doesn't break across pages
	WrapText      bool      // Wrap text within a cell (Excel/Word)
	Superscript   bool      // Render as superscript
	Subscript     bool      // Render as subscript
	DashPattern   []float64 // PDF dash pattern, e.g., [3, 3] for dashed
	Opacity       float64   // Opacity from 0.0 to 1.0 (PDF)
	Absolute      bool      // If true, the item is positioned relative to the current flow (PDF)
	X             float64   // Absolute X position (PDF)
	Y             float64   // Absolute Y position (PDF)
}

CellStyle represents the visual formatting of a spreadsheet cell.

type CellStyleBuilder

type CellStyleBuilder interface {
	Bold() CellStyleBuilder
	Italic() CellStyleBuilder
	Size(size int) CellStyleBuilder
	Color(hex string) CellStyleBuilder
	Background(hex string) CellStyleBuilder
	Border() CellStyleBuilder
	BorderTop() CellStyleBuilder
	BorderBottom() CellStyleBuilder
	BorderLeft() CellStyleBuilder
	BorderRight() CellStyleBuilder
	BorderWidth(width float64) CellStyleBuilder
	BorderColor(hex string) CellStyleBuilder
	Align(h, v string) CellStyleBuilder
	NumberFormat(format string) CellStyleBuilder
	Name(name string) CellStyleBuilder
	Link(url string) CellStyleBuilder
	Font(name string) CellStyleBuilder
	Alt(text string) CellStyleBuilder
	LineSpacing(spacing float64) CellStyleBuilder
	SpacingBefore(spacing float64) CellStyleBuilder
	SpacingAfter(spacing float64) CellStyleBuilder
	Indent(points float64) CellStyleBuilder
	Hanging(points float64) CellStyleBuilder
	Padding(points float64) CellStyleBuilder
	KeepWithNext() CellStyleBuilder
	KeepTogether() CellStyleBuilder
	Superscript() CellStyleBuilder
	Subscript() CellStyleBuilder
	Absolute() CellStyleBuilder
	Pos(x, y float64) CellStyleBuilder
	Opacity(opacity float64) CellStyleBuilder
	DashPattern(pattern []float64) CellStyleBuilder
	Build() CellStyle
}

CellStyleBuilder is a fluent builder for CellStyle.

func NewCellStyleBuilder

func NewCellStyleBuilder() CellStyleBuilder

type Document

type Document interface {
	Metadatable

	// Open loads a document from a reader.
	Open(ctx context.Context, reader io.Reader) error

	// ReadContent returns the text content of the document.
	ReadContent() (string, error)

	// Search finds keywords in the document.
	Search(keywords []string) ([]SearchResult, error)

	// Replace replaces keywords with new values throughout the document.
	Replace(replacements map[string]string) error

	// SetPageSettings configures the document's layout.
	SetPageSettings(settings PageSettings) error

	// Save writes the modified document to the provided writer.
	Save(ctx context.Context, writer io.Writer) error

	// Export saves the document using the configured storage system.
	Export(uri string) error

	// SetPassword sets the document password for encryption.
	SetPassword(password string) error

	// Close releases any resources used by the document.
	Close() error
}

Document is the common interface for processing Excel, Word, and PDF files. It provides methods for opening, reading, searching, modifying, and saving documents.

type Margins

type Margins struct {
	Top    float64
	Bottom float64
	Left   float64
	Right  float64
}

Margins represents the page margins.

type Metadata

type Metadata struct {
	Title       string
	Author      string
	Subject     string
	Description string
	Keywords    []string
	Created     time.Time
	Modified    time.Time
	Generator   string
	Company     string
}

Metadata represents document-level information.

type Metadatable

type Metadatable interface {
	GetMetadata() (Metadata, error)
	SetMetadata(metadata Metadata) error
}

Metadatable defines an interface for documents that support metadata operations.

type Orientation

type Orientation string

Orientation represents the page orientation.

const (
	// OrientationPortrait represents the vertical page layout.
	OrientationPortrait Orientation = "Portrait"
	// OrientationLandscape represents the horizontal page layout.
	OrientationLandscape Orientation = "Landscape"
)

type PageSettings

type PageSettings struct {
	Orientation Orientation
	PaperType   PaperType
	Margins     Margins
	Columns     int     // Number of columns (default 1)
	ColumnGap   float64 // Gap between columns in points
}

PageSettings represents document layout settings.

type PageSettingsBuilder

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

PageSettingsBuilder helps in constructing PageSettings using a chainable API.

func NewPageSettingsBuilder

func NewPageSettingsBuilder() *PageSettingsBuilder

NewPageSettingsBuilder creates a new PageSettingsBuilder.

func (*PageSettingsBuilder) Build

func (b *PageSettingsBuilder) Build() PageSettings

Build returns the constructed PageSettings.

func (*PageSettingsBuilder) WithColumns

func (b *PageSettingsBuilder) WithColumns(count int, gap float64) *PageSettingsBuilder

WithColumns sets the number of columns and gap.

func (*PageSettingsBuilder) WithMargins

func (b *PageSettingsBuilder) WithMargins(m Margins) *PageSettingsBuilder

WithMargins sets the margins for the page settings.

func (*PageSettingsBuilder) WithOrientation

func (b *PageSettingsBuilder) WithOrientation(o Orientation) *PageSettingsBuilder

WithOrientation sets the orientation for the page settings.

func (*PageSettingsBuilder) WithPaperType

func (b *PageSettingsBuilder) WithPaperType(p PaperType) *PageSettingsBuilder

WithPaperType sets the paper type for the page settings.

type PaperType

type PaperType string

PaperType represents the paper size.

const (
	// PaperA4 represents A4 size paper.
	PaperA4 PaperType = "A4"
	// PaperLetter represents US Letter size paper.
	PaperLetter PaperType = "Letter"
	// PaperLegal represents US Legal size paper.
	PaperLegal PaperType = "Legal"
)

type RegexSearchStrategy

type RegexSearchStrategy struct{}

RegexSearchStrategy implements a regex-based search.

func (*RegexSearchStrategy) Execute

func (s *RegexSearchStrategy) Execute(ctx context.Context, content string, patterns []string) ([]SearchResult, error)

Execute performs a regex-based search.

type Row

type Row interface {
	Cell(index int) TableCell
	Err() error
}

Row is a fluent handle bound to a specific row in a table.

type SearchResult

type SearchResult struct {
	Keyword  string
	Location string // e.g., "Page 1", "Sheet1!A1", etc.
	Index    int    // Character index or sequence number
}

SearchResult contains the information about a found keyword.

type SearchStrategy

type SearchStrategy interface {
	Execute(ctx context.Context, content string, patterns []string) ([]SearchResult, error)
}

SearchStrategy defines the interface for different search algorithms.

func NewRegexSearchStrategy

func NewRegexSearchStrategy() SearchStrategy

NewRegexSearchStrategy creates a new RegexSearchStrategy.

func NewSimpleSearchStrategy

func NewSimpleSearchStrategy() SearchStrategy

NewSimpleSearchStrategy creates a new SimpleSearchStrategy.

type Sheet

type Sheet interface {
	Cell(axis string) Cell
	MergeCells(hRange string) Sheet
	SetColumnWidth(col int, width float64) Sheet
	SetRowHeight(row int, height float64) Sheet
	AutoFilter(ref string) Sheet
	FreezePanes(col, row int) Sheet
	InsertImage(path string, x, y float64) Sheet
	SetDataValidation(ref string, options ...string) Sheet
	SetConditionalFormatting(ref string, style CellStyle) Sheet
	SetPageSettings(settings PageSettings) Sheet
	Protect(password string) Sheet
	GroupRows(start, end int, level int) Sheet
	GroupCols(start, end int, level int) Sheet
	SetHeader(text string) Sheet
	SetFooter(text string) Sheet
	AddTable(ref string, name string) Sheet
	SetPrintArea(ref string) Sheet
	SetPrintTitles(rowRef, colRef string) Sheet
	GetCellValue(axis string) (string, error)
	Err() error
}

Sheet is a fluent handle bound to a specific worksheet. It provides sheet-scoped operations without repeatedly passing the sheet name or context.

type SimpleSearchStrategy

type SimpleSearchStrategy struct{}

SimpleSearchStrategy implements a basic text search.

func (*SimpleSearchStrategy) Execute

func (s *SimpleSearchStrategy) Execute(ctx context.Context, content string, patterns []string) ([]SearchResult, error)

Execute performs a simple substring search.

type Spreadsheet

type Spreadsheet interface {
	Document
	// Fluent sheet selector
	Sheet(name string) (Sheet, error)

	// Additional spreadsheet-level ops
	GetSheets() ([]string, error)
	SetNamedRange(name, ref string) error
}

Spreadsheet defines operations specific to spreadsheet documents (Excel).

type Table

type Table interface {
	Row(index int) Row
	MergeCells(row, col, rowSpan, colSpan int) Table
	SetColumnWidths(widths ...float64) Table
	SetHeaderRows(count int) Table
	SetStyle(style string) Table
	Err() error
}

Table is a fluent handle bound to a specific Word/PDF table instance. It provides table-scoped operations without passing the table index each time.

type TableCell

type TableCell interface {
	AddParagraph(text string, style ...CellStyle) TableCell
	AddRichParagraph(spans []TextSpan) TableCell
	AddImage(path string, width, height float64, style ...CellStyle) TableCell
	AddList(items []string, ordered bool, style ...CellStyle) TableCell
	AddTable(rows, cols int) Table
	Style(style CellStyle) TableCell
	Err() error
}

TableCell is a fluent handle bound to a specific cell in a table row.

type TextSpan

type TextSpan struct {
	Text  string
	Style CellStyle
}

TextSpan represents a styled span within a rich text paragraph.

type WordProcessor

type WordProcessor interface {
	Document
	AddParagraph(text string, style ...CellStyle) error
	AddRichParagraph(spans []TextSpan) error
	AddHeading(text string, level int, style ...CellStyle) error
	InsertImage(path string, width, height float64, style ...CellStyle) error
	AddTable(rows, cols int) (Table, error)

	AddPageBreak() error
	AddSection(settings PageSettings) error

	SetHeader(text string, style ...CellStyle) error
	SetFooter(text string, style ...CellStyle) error

	DrawLine(x1, y1, x2, y2 float64, style ...CellStyle) error
	DrawRect(x, y, w, h float64, style ...CellStyle) error
	DrawEllipse(x, y, w, h float64, style ...CellStyle) error
	RegisterFont(name, path string) error

	AddTextField(name string, x, y, w, h float64) error
	AddCheckbox(name string, x, y float64) error
	AddComboBox(name string, x, y, w, h float64, options ...string) error
	AddRadioButton(name string, x, y float64, options ...string) error
	ImportPage(path string, pageNum int) error
	AddFootnote(text string) error
	AddList(items []string, ordered bool, style ...CellStyle) error
	SetWatermark(text string, style ...CellStyle) error
	AddHyperlink(text, url string, style ...CellStyle) error
	AddBookmark(name string) error
	AddTableOfContents() error
	AttachFile(path, name, description string) error

	// Fluent API: get a table-scoped handle
	Table(index int) (Table, error)
}

WordProcessor defines operations specific to word processing documents (Word).

Jump to

Keyboard shortcuts

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