@cj-tech-master/excelts 9.5.0 → 9.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/dist/browser/modules/pdf/excel-bridge.js +27 -1
  2. package/dist/browser/modules/pdf/render/layout-engine.js +74 -9
  3. package/dist/browser/modules/pdf/render/style-converter.d.ts +1 -1
  4. package/dist/browser/modules/pdf/render/style-converter.js +98 -1
  5. package/dist/browser/modules/pdf/types.d.ts +1 -0
  6. package/dist/browser/modules/word/color-utils.d.ts +18 -0
  7. package/dist/browser/modules/word/color-utils.js +94 -0
  8. package/dist/browser/modules/word/content-types.d.ts +15 -15
  9. package/dist/browser/modules/word/content-types.js +39 -43
  10. package/dist/browser/modules/word/crypto.d.ts +17 -0
  11. package/dist/browser/modules/word/crypto.js +18 -0
  12. package/dist/browser/modules/word/document-io.d.ts +58 -0
  13. package/dist/browser/modules/word/document-io.js +239 -0
  14. package/dist/browser/modules/word/document.d.ts +64 -135
  15. package/dist/browser/modules/word/document.js +207 -469
  16. package/dist/browser/modules/word/docx-packager.js +90 -90
  17. package/dist/browser/modules/word/html-renderer.js +1 -1
  18. package/dist/browser/modules/word/html.d.ts +13 -0
  19. package/dist/browser/modules/word/html.js +12 -0
  20. package/dist/browser/modules/word/index.base.d.ts +6 -9
  21. package/dist/browser/modules/word/index.base.js +7 -10
  22. package/dist/browser/modules/word/namespaces.d.ts +159 -0
  23. package/dist/browser/modules/word/namespaces.js +189 -0
  24. package/dist/browser/modules/word/relationships.d.ts +15 -16
  25. package/dist/browser/modules/word/relationships.js +37 -45
  26. package/dist/cjs/modules/pdf/excel-bridge.js +27 -1
  27. package/dist/cjs/modules/pdf/render/layout-engine.js +74 -9
  28. package/dist/cjs/modules/pdf/render/style-converter.js +98 -1
  29. package/dist/cjs/modules/word/color-utils.js +97 -0
  30. package/dist/cjs/modules/word/content-types.js +44 -45
  31. package/dist/cjs/modules/word/crypto.js +34 -0
  32. package/dist/cjs/modules/word/document-io.js +244 -0
  33. package/dist/cjs/modules/word/document.js +209 -473
  34. package/dist/cjs/modules/word/docx-packager.js +88 -88
  35. package/dist/cjs/modules/word/html-renderer.js +2 -2
  36. package/dist/cjs/modules/word/html.js +16 -0
  37. package/dist/cjs/modules/word/index.base.js +17 -27
  38. package/dist/cjs/modules/word/namespaces.js +192 -0
  39. package/dist/cjs/modules/word/relationships.js +42 -47
  40. package/dist/esm/modules/pdf/excel-bridge.js +27 -1
  41. package/dist/esm/modules/pdf/render/layout-engine.js +74 -9
  42. package/dist/esm/modules/pdf/render/style-converter.js +98 -1
  43. package/dist/esm/modules/word/color-utils.js +94 -0
  44. package/dist/esm/modules/word/content-types.js +39 -43
  45. package/dist/esm/modules/word/crypto.js +18 -0
  46. package/dist/esm/modules/word/document-io.js +239 -0
  47. package/dist/esm/modules/word/document.js +207 -469
  48. package/dist/esm/modules/word/docx-packager.js +90 -90
  49. package/dist/esm/modules/word/html-renderer.js +1 -1
  50. package/dist/esm/modules/word/html.js +12 -0
  51. package/dist/esm/modules/word/index.base.js +7 -10
  52. package/dist/esm/modules/word/namespaces.js +189 -0
  53. package/dist/esm/modules/word/relationships.js +37 -45
  54. package/dist/iife/excelts.iife.js +153 -11
  55. package/dist/iife/excelts.iife.js.map +1 -1
  56. package/dist/iife/excelts.iife.min.js +4 -4
  57. package/dist/types/modules/pdf/render/style-converter.d.ts +1 -1
  58. package/dist/types/modules/pdf/types.d.ts +1 -0
  59. package/dist/types/modules/word/color-utils.d.ts +18 -0
  60. package/dist/types/modules/word/content-types.d.ts +15 -15
  61. package/dist/types/modules/word/crypto.d.ts +17 -0
  62. package/dist/types/modules/word/document-io.d.ts +58 -0
  63. package/dist/types/modules/word/document.d.ts +64 -135
  64. package/dist/types/modules/word/html.d.ts +13 -0
  65. package/dist/types/modules/word/index.base.d.ts +6 -9
  66. package/dist/types/modules/word/namespaces.d.ts +159 -0
  67. package/dist/types/modules/word/relationships.d.ts +15 -16
  68. package/package.json +1 -1
@@ -0,0 +1,239 @@
1
+ /**
2
+ * DOCX Module - Document IO
3
+ *
4
+ * IO operations that depend on docx-packager and docx-reader.
5
+ * Separated from document.ts so that builder helpers can be imported
6
+ * without pulling in archive/xml/writer code.
7
+ */
8
+ import { packageDocx } from "./docx-packager.js";
9
+ import { readDocx } from "./docx-reader.js";
10
+ import { bytesToBase64 } from "./internal-utils.js";
11
+ // =============================================================================
12
+ // Document IO (toBuffer / toBase64)
13
+ // =============================================================================
14
+ /** Package a DocxDocument model to DOCX bytes. */
15
+ export async function toBuffer(doc, compressionLevel) {
16
+ return packageDocx(doc, compressionLevel);
17
+ }
18
+ /** Package a DocxDocument model to base64 string. */
19
+ export async function toBase64(doc, compressionLevel) {
20
+ const bytes = await toBuffer(doc, compressionLevel);
21
+ return bytesToBase64(bytes);
22
+ }
23
+ /**
24
+ * Read an existing DOCX file, replace placeholders with content, and produce a new DOCX.
25
+ *
26
+ * Placeholders are strings like `{{name}}` embedded in the document text.
27
+ * They may span across multiple runs — the patcher handles cross-run matching.
28
+ *
29
+ * Supported patch content types:
30
+ * - `text` — simple text replacement (preserves formatting of the first run)
31
+ * - `paragraph` — replaces the entire paragraph containing the placeholder
32
+ * - `table` — replaces the entire paragraph with a table
33
+ * - `image` — replaces the placeholder with an inline image
34
+ *
35
+ * @param buffer - The source DOCX file as a Uint8Array.
36
+ * @param patches - Array of patch operations to apply.
37
+ * @param options - Optional compression settings.
38
+ * @returns New DOCX file as a Uint8Array.
39
+ */
40
+ export async function patchDocument(buffer, patches, options) {
41
+ const doc = await readDocx(buffer);
42
+ // Build lookup map for quick placeholder matching
43
+ const patchMap = new Map();
44
+ for (const patch of patches) {
45
+ patchMap.set(patch.placeholder, patch);
46
+ }
47
+ // Process body content
48
+ const newBody = [];
49
+ for (const block of doc.body) {
50
+ if (block.type === "paragraph") {
51
+ const result = patchParagraph(block, patchMap);
52
+ if (result) {
53
+ if (Array.isArray(result)) {
54
+ newBody.push(...result);
55
+ }
56
+ else {
57
+ newBody.push(result);
58
+ }
59
+ }
60
+ }
61
+ else if (block.type === "table") {
62
+ patchTable(block, patchMap);
63
+ newBody.push(block);
64
+ }
65
+ else {
66
+ newBody.push(block);
67
+ }
68
+ }
69
+ // Patch headers
70
+ if (doc.headers) {
71
+ for (const [, headerDef] of doc.headers) {
72
+ patchHeaderFooterContent(headerDef.content, patchMap);
73
+ }
74
+ }
75
+ // Patch footers
76
+ if (doc.footers) {
77
+ for (const [, footerDef] of doc.footers) {
78
+ patchHeaderFooterContent(footerDef.content, patchMap);
79
+ }
80
+ }
81
+ // Add any new images from patches
82
+ const images = doc.images ? [...doc.images] : [];
83
+ for (const patch of patches) {
84
+ if (patch.content.type === "image") {
85
+ const imgContent = patch.content;
86
+ const existing = images.find(i => i.fileName === imgContent.image.fileName);
87
+ if (!existing) {
88
+ images.push(imgContent.image);
89
+ }
90
+ }
91
+ }
92
+ const patched = {
93
+ ...doc,
94
+ body: newBody,
95
+ images: images.length > 0 ? images : undefined
96
+ };
97
+ return packageDocx(patched, options?.compressionLevel);
98
+ }
99
+ // =============================================================================
100
+ // Internal helpers
101
+ // =============================================================================
102
+ /** Extract concatenated plain text from a paragraph's runs. */
103
+ function paragraphText(para) {
104
+ let t = "";
105
+ for (const child of para.children) {
106
+ if ("content" in child && Array.isArray(child.content)) {
107
+ for (const c of child.content) {
108
+ if ("type" in c && c.type === "text" && "text" in c) {
109
+ t += c.text;
110
+ }
111
+ }
112
+ }
113
+ }
114
+ return t;
115
+ }
116
+ /** Replace text within a single paragraph. */
117
+ function replaceInParagraph(para, search, replacement) {
118
+ for (const child of para.children) {
119
+ if (!("content" in child) || !Array.isArray(child.content)) {
120
+ continue;
121
+ }
122
+ for (const c of child.content) {
123
+ if (!("type" in c) || c.type !== "text" || !("text" in c)) {
124
+ continue;
125
+ }
126
+ const before = c.text;
127
+ if (before.includes(search)) {
128
+ c.text = before.replaceAll(search, replacement);
129
+ }
130
+ }
131
+ }
132
+ // Cross-run replacement fallback
133
+ const fullText = paragraphText(para);
134
+ if (fullText.includes(search)) {
135
+ const newText = fullText.replaceAll(search, replacement);
136
+ let placed = false;
137
+ for (const child of para.children) {
138
+ if (!("content" in child) || !Array.isArray(child.content)) {
139
+ continue;
140
+ }
141
+ for (const c of child.content) {
142
+ if (!("type" in c) || c.type !== "text" || !("text" in c)) {
143
+ continue;
144
+ }
145
+ if (!placed) {
146
+ c.text = newText;
147
+ placed = true;
148
+ }
149
+ else {
150
+ c.text = "";
151
+ }
152
+ }
153
+ }
154
+ }
155
+ }
156
+ /** Patch a paragraph — returns replacement content or null to remove. */
157
+ function patchParagraph(para, patchMap) {
158
+ const text = paragraphText(para);
159
+ for (const [placeholder, patch] of patchMap) {
160
+ if (!text.includes(placeholder)) {
161
+ continue;
162
+ }
163
+ switch (patch.content.type) {
164
+ case "text": {
165
+ replaceInParagraph(para, placeholder, patch.content.text);
166
+ return para;
167
+ }
168
+ case "paragraph": {
169
+ return patch.content.children;
170
+ }
171
+ case "table": {
172
+ return patch.content.table;
173
+ }
174
+ case "image": {
175
+ const img = patch.content.image;
176
+ const rId = img.rId ?? `rId_img_${img.fileName}`;
177
+ const imgContent = {
178
+ type: "image",
179
+ rId,
180
+ width: patch.content.width,
181
+ height: patch.content.height,
182
+ altText: img.fileName,
183
+ name: img.fileName
184
+ };
185
+ const newPara = {
186
+ type: "paragraph",
187
+ properties: para.properties,
188
+ children: [{ content: [imgContent] }]
189
+ };
190
+ return newPara;
191
+ }
192
+ }
193
+ }
194
+ return para;
195
+ }
196
+ /** Patch text inside table cells recursively. */
197
+ function patchTable(table, patchMap) {
198
+ for (const row of table.rows) {
199
+ for (const cell of row.cells) {
200
+ const newContent = [];
201
+ for (const block of cell.content) {
202
+ if (block.type === "paragraph") {
203
+ const result = patchParagraph(block, patchMap);
204
+ if (result) {
205
+ if (Array.isArray(result)) {
206
+ newContent.push(...result);
207
+ }
208
+ else {
209
+ newContent.push(result);
210
+ }
211
+ }
212
+ }
213
+ else if (block.type === "table") {
214
+ patchTable(block, patchMap);
215
+ newContent.push(block);
216
+ }
217
+ else {
218
+ newContent.push(block);
219
+ }
220
+ }
221
+ cell.content = newContent;
222
+ }
223
+ }
224
+ }
225
+ /** Patch text in header/footer content. */
226
+ function patchHeaderFooterContent(content, patchMap) {
227
+ for (const child of content.children) {
228
+ if (child.type === "paragraph") {
229
+ for (const [placeholder, patch] of patchMap) {
230
+ if (patch.content.type === "text") {
231
+ const text = paragraphText(child);
232
+ if (text.includes(placeholder)) {
233
+ replaceInParagraph(child, placeholder, patch.content.text);
234
+ }
235
+ }
236
+ }
237
+ }
238
+ }
239
+ }
@@ -4,8 +4,11 @@
4
4
  * High-level fluent API for constructing DOCX documents programmatically.
5
5
  * Provides convenience methods for common operations including comments,
6
6
  * track changes, TOC, math, text boxes, checkboxes, and custom properties.
7
+ *
8
+ * This file has NO static imports from docx-packager or docx-reader,
9
+ * ensuring that importing builder helpers does not pull in archive/xml code.
7
10
  */
8
- import type { DocxDocument, BodyContent, Paragraph, ParagraphProperties, ParagraphChild, Run, RunProperties, Table, TableRow, TableCell, TableProperties, TableRowProperties, TableCellProperties, SectionProperties, StyleDef, DocDefaults, BookmarkStart, Hyperlink, HeaderFooterContent, ImageDef, ImageMediaType, CoreProperties, AppProperties, DocumentSettings, FontDef, Border, TableWidth, TableBorders, Emu, Twips, CommentDef, DocumentBackground, CustomPropertyValue, TableOfContents, MathBlock, MathContent, CheckBox, RevisionInfo, InsertedRun, DeletedRun, MovedFromRun, MovedToRun, MoveRangeMarker, Chart, ChartSeries, ChartAxis, ChartLegendPosition, ChartContent, CommentRangeStart, CommentRangeEnd, CommentReference, FloatingImage, StructuredDocumentTag, SdtProperties, Watermark, DocumentTheme, ColorSpec, HexColor, DrawingShape, PositionalTabAlignment, PositionalTabRelativeTo, PositionalTabLeader, RubyProperties } from "./types.js";
11
+ import type { DocxDocument, BodyContent, Paragraph, ParagraphProperties, ParagraphChild, Run, RunProperties, Table, TableRow, TableCell, TableProperties, TableRowProperties, TableCellProperties, SectionProperties, StyleDef, DocDefaults, BookmarkStart, Hyperlink, HeaderFooterContent, ImageDef, ImageMediaType, CoreProperties, AppProperties, DocumentSettings, FontDef, Border, TableWidth, TableBorders, Emu, Twips, CommentDef, DocumentBackground, CustomPropertyValue, TableOfContents, MathBlock, MathContent, CheckBox, RevisionInfo, InsertedRun, DeletedRun, MovedFromRun, MovedToRun, MoveRangeMarker, Chart, ChartSeries, ChartAxis, ChartLegendPosition, ChartContent, CommentRangeStart, CommentRangeEnd, CommentReference, FloatingImage, StructuredDocumentTag, SdtProperties, Watermark, HexColor, DrawingShape, PositionalTabAlignment, PositionalTabRelativeTo, PositionalTabLeader, RubyProperties } from "./types.js";
9
12
  /** Create a text run. */
10
13
  export declare function text(content: string, properties?: RunProperties): Run;
11
14
  /** Create a bold text run. */
@@ -411,71 +414,55 @@ export declare function simpleTable(data: string[][], options?: {
411
414
  width?: TableWidth;
412
415
  columnWidths?: Twips[];
413
416
  }): Table;
417
+ declare const _documentBrand: unique symbol;
418
+ /**
419
+ * Opaque handle representing a document being built.
420
+ * Created via `Document.create()`, passed to `Document.*` functions.
421
+ */
422
+ export type DocumentHandle = {
423
+ readonly [_documentBrand]: true;
424
+ };
414
425
  /**
415
- * Fluent builder for constructing DOCX documents.
426
+ * Namespace of free functions for building DOCX documents.
427
+ *
428
+ * Replaces the former `DocumentBuilder` class with tree-shakeable free functions.
429
+ * Each function operates on an opaque `DocumentHandle`.
416
430
  *
417
431
  * @example
418
432
  * ```ts
419
- * const doc = new DocumentBuilder()
420
- * .addHeading("Hello World", 1)
421
- * .addParagraph("This is a paragraph.")
422
- * .addTable([["Name", "Age"], ["Alice", "30"]])
423
- * .build();
424
- *
425
- * const bytes = await doc.toBuffer();
433
+ * const doc = Document.create();
434
+ * Document.addHeading(doc, "Hello World", 1);
435
+ * Document.addParagraph(doc, "This is a paragraph.");
436
+ * Document.addTable(doc, [["Name", "Age"], ["Alice", "30"]]);
437
+ * const bytes = await Document.toBuffer(doc);
426
438
  * ```
427
439
  */
428
- export declare class DocumentBuilder {
429
- private _body;
430
- private _sectionProperties?;
431
- private _styles;
432
- private _docDefaults?;
433
- private _abstractNumberings;
434
- private _numberingInstances;
435
- private _headers;
436
- private _footers;
437
- private _footnotes;
438
- private _endnotes;
439
- private _images;
440
- private _fonts;
441
- private _settings?;
442
- private _coreProperties?;
443
- private _appProperties?;
444
- private _comments;
445
- private _background?;
446
- private _customProperties;
447
- private _watermark?;
448
- private _nextImageId;
449
- private _nextFootnoteId;
450
- private _nextEndnoteId;
451
- private _nextBookmarkId;
452
- private _nextAbstractNumId;
453
- private _nextNumId;
454
- private _nextDrawingId;
455
- private _nextCommentId;
440
+ export declare const Document: {
441
+ /** Create a new document handle. */
442
+ create(): DocumentHandle;
456
443
  /** Add raw body content. */
457
- addContent(content: BodyContent): this;
444
+ addContent(doc: DocumentHandle, content: BodyContent): void;
458
445
  /** Add a paragraph with runs. */
459
- addParagraphElement(para: Paragraph): this;
446
+ addParagraphElement(doc: DocumentHandle, para: Paragraph): void;
460
447
  /** Add a simple text paragraph. */
461
- addParagraph(content: string, properties?: ParagraphProperties & {
448
+ addParagraph(doc: DocumentHandle, content: string, properties?: ParagraphProperties & {
462
449
  run?: RunProperties;
463
- }): this;
450
+ }): void;
464
451
  /** Add a heading. */
465
- addHeading(content: string, level?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9): this;
452
+ addHeading(doc: DocumentHandle, content: string, level?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9): void;
466
453
  /** Add a page break. */
467
- addPageBreak(): this;
454
+ addPageBreak(doc: DocumentHandle): void;
468
455
  /** Add a table from a 2D array. */
469
- addTable(data: string[][], options?: {
456
+ addTable(doc: DocumentHandle, data: string[][], options?: {
470
457
  headerRow?: boolean;
471
458
  borders?: boolean;
472
459
  width?: TableWidth;
473
460
  columnWidths?: Twips[];
474
- }): this;
461
+ }): void;
475
462
  /** Add a table element. */
476
- addTableElement(tbl: Table): this;
463
+ addTableElement(doc: DocumentHandle, tbl: Table): void;
477
464
  /** Add an inline image. Returns the image relationship ID and drawing ID. */
478
- addImage(data: Uint8Array, mediaType: ImageMediaType, width: Emu, height: Emu, options?: {
465
+ addImage(doc: DocumentHandle, data: Uint8Array, mediaType: ImageMediaType, width: Emu, height: Emu, options?: {
479
466
  altText?: string;
480
467
  name?: string;
481
468
  }): {
@@ -483,7 +470,7 @@ export declare class DocumentBuilder {
483
470
  drawingId: number;
484
471
  };
485
472
  /** Add a floating image. Returns the image relationship ID. */
486
- addFloatingImage(data: Uint8Array, mediaType: ImageMediaType, width: Emu, height: Emu, options?: {
473
+ addFloatingImage(doc: DocumentHandle, data: Uint8Array, mediaType: ImageMediaType, width: Emu, height: Emu, options?: {
487
474
  altText?: string;
488
475
  name?: string;
489
476
  horizontalPosition?: FloatingImage["horizontalPosition"];
@@ -502,76 +489,63 @@ export declare class DocumentBuilder {
502
489
  flipVertical?: boolean;
503
490
  }): string;
504
491
  /** Add a custom font definition. */
505
- addFont(font: FontDef): this;
492
+ addFont(doc: DocumentHandle, font: FontDef): void;
506
493
  /** Set a text watermark on the document. */
507
- setWatermark(watermark: Watermark): this;
494
+ setWatermark(doc: DocumentHandle, watermark: Watermark): void;
508
495
  /** Add a footnote. Returns the footnote ID. */
509
- addFootnote(content: string | Paragraph[]): number;
496
+ addFootnote(doc: DocumentHandle, content: string | Paragraph[]): number;
510
497
  /** Add an endnote. Returns the endnote ID. */
511
- addEndnote(content: string | Paragraph[]): number;
498
+ addEndnote(doc: DocumentHandle, content: string | Paragraph[]): number;
512
499
  /** Add a comment. Returns the comment ID. */
513
- addComment(author: string, content: string | Paragraph[], options?: {
500
+ addComment(doc: DocumentHandle, author: string, content: string | Paragraph[], options?: {
514
501
  date?: string;
515
502
  initials?: string;
516
503
  }): number;
517
504
  /** Add a Table of Contents. */
518
- addTableOfContents(options?: Partial<Omit<TableOfContents, "type">>): this;
505
+ addTableOfContents(doc: DocumentHandle, options?: Partial<Omit<TableOfContents, "type">>): void;
519
506
  /** Add a math equation block. */
520
- addMath(content: MathContent[]): this;
507
+ addMath(doc: DocumentHandle, content: MathContent[]): void;
521
508
  /** Add a text box. */
522
- addTextBox(content: string | Paragraph[], options?: {
509
+ addTextBox(doc: DocumentHandle, content: string | Paragraph[], options?: {
523
510
  width?: Twips;
524
511
  height?: Twips;
525
512
  stroke?: boolean;
526
513
  fill?: boolean;
527
- }): this;
514
+ }): void;
528
515
  /** Add a bullet list. */
529
- addBulletList(items: string[], level?: number): this;
516
+ addBulletList(doc: DocumentHandle, items: string[], level?: number): void;
530
517
  /** Add a numbered list. */
531
- addNumberedList(items: string[], level?: number): this;
518
+ addNumberedList(doc: DocumentHandle, items: string[], level?: number): void;
532
519
  /** Set section properties (page size, margins, etc.). */
533
- setSectionProperties(props: SectionProperties): this;
520
+ setSectionProperties(doc: DocumentHandle, props: SectionProperties): void;
534
521
  /** Set document defaults. */
535
- setDocDefaults(defaults: DocDefaults): this;
522
+ setDocDefaults(doc: DocumentHandle, defaults: DocDefaults): void;
536
523
  /** Add a style definition. */
537
- addStyle(style: StyleDef): this;
524
+ addStyle(doc: DocumentHandle, style: StyleDef): void;
538
525
  /** Set default styles (Normal, Heading1-6, Hyperlink, etc.). */
539
- useDefaultStyles(): this;
526
+ useDefaultStyles(doc: DocumentHandle): void;
540
527
  /** Set a header for the given type. */
541
- setHeader(type: string, content: HeaderFooterContent): this;
528
+ setHeader(doc: DocumentHandle, type: string, content: HeaderFooterContent): void;
542
529
  /** Set a footer for the given type. */
543
- setFooter(type: string, content: HeaderFooterContent): this;
530
+ setFooter(doc: DocumentHandle, type: string, content: HeaderFooterContent): void;
544
531
  /** Set document settings. */
545
- setSettings(settings: DocumentSettings): this;
532
+ setSettings(doc: DocumentHandle, settings: DocumentSettings): void;
546
533
  /** Set core properties (metadata). */
547
- setCoreProperties(props: CoreProperties): this;
534
+ setCoreProperties(doc: DocumentHandle, props: CoreProperties): void;
548
535
  /** Set application properties. */
549
- setAppProperties(props: AppProperties): this;
536
+ setAppProperties(doc: DocumentHandle, props: AppProperties): void;
550
537
  /** Set document background. */
551
- setBackground(background: DocumentBackground): this;
538
+ setBackground(doc: DocumentHandle, background: DocumentBackground): void;
552
539
  /** Add a custom document property. */
553
- addCustomProperty(name: string, value: CustomPropertyValue): this;
540
+ addCustomProperty(doc: DocumentHandle, name: string, value: CustomPropertyValue): void;
554
541
  /** Add a section break with properties. */
555
- addSectionBreak(props: SectionProperties): this;
542
+ addSectionBreak(doc: DocumentHandle, props: SectionProperties): void;
556
543
  /** Get next available bookmark ID. */
557
- nextBookmarkId(): number;
558
- /** Build the DocxDocument model. */
559
- build(): DocxDocument;
560
- /** Build and package to DOCX bytes. */
561
- toBuffer(compressionLevel?: number): Promise<Uint8Array>;
562
- /** Build and package to base64 string. */
563
- toBase64(compressionLevel?: number): Promise<string>;
564
- }
565
- /**
566
- * Resolve a ColorSpec to an actual hex RGB color using the document theme.
567
- *
568
- * Applies theme color lookup + tint/shade transformations per OOXML spec.
569
- *
570
- * @param color - The color value (HexColor string or ColorSpec).
571
- * @param theme - The document theme (from `doc.theme`).
572
- * @returns Resolved hex color string (6 chars, no #), or undefined if unresolvable.
573
- */
574
- export declare function resolveThemeColor(color: HexColor | ColorSpec | undefined, theme?: DocumentTheme): HexColor | undefined;
544
+ nextBookmarkId(doc: DocumentHandle): number;
545
+ /** Build the DocxDocument model from the handle. */
546
+ build(doc: DocumentHandle): DocxDocument;
547
+ };
548
+ export { resolveThemeColor } from "./color-utils.js";
575
549
  /** Result of a text search in a document. */
576
550
  export interface SearchResult {
577
551
  /** Paragraph index in body. */
@@ -680,49 +654,4 @@ export declare function mailMerge(doc: DocxDocument, data: Record<string, string
680
654
  /** If true, remove fields not found in data. Default: false (leave unchanged). */
681
655
  removeUnmatched?: boolean;
682
656
  }): number;
683
- /** Type of content to patch into a placeholder. */
684
- export type PatchContent = {
685
- readonly type: "text";
686
- readonly text: string;
687
- } | {
688
- readonly type: "paragraph";
689
- readonly children: readonly Paragraph[];
690
- } | {
691
- readonly type: "table";
692
- readonly table: Table;
693
- } | {
694
- readonly type: "image";
695
- readonly image: ImageDef;
696
- readonly width: number;
697
- readonly height: number;
698
- };
699
- /** A single patch operation mapping a placeholder to replacement content. */
700
- export interface PatchOperation {
701
- /** Placeholder string to find (e.g. "{{name}}"). */
702
- readonly placeholder: string;
703
- /** Content to replace the placeholder with. */
704
- readonly content: PatchContent;
705
- }
706
- /** Options for patchDocument. */
707
- export interface PatchOptions {
708
- /** Compression level (0-9). Default: 6. */
709
- readonly compressionLevel?: number;
710
- }
711
- /**
712
- * Read an existing DOCX file, replace placeholders with content, and produce a new DOCX.
713
- *
714
- * Placeholders are strings like `{{name}}` embedded in the document text.
715
- * They may span across multiple runs — the patcher handles cross-run matching.
716
- *
717
- * Supported patch content types:
718
- * - `text` — simple text replacement (preserves formatting of the first run)
719
- * - `paragraph` — replaces the entire paragraph containing the placeholder
720
- * - `table` — replaces the entire paragraph with a table
721
- * - `image` — replaces the placeholder with an inline image
722
- *
723
- * @param buffer - The source DOCX file as a Uint8Array.
724
- * @param patches - Array of patch operations to apply.
725
- * @param options - Optional compression settings.
726
- * @returns New DOCX file as a Uint8Array.
727
- */
728
- export declare function patchDocument(buffer: Uint8Array, patches: readonly PatchOperation[], options?: PatchOptions): Promise<Uint8Array>;
657
+ export type { PatchContent, PatchOperation, PatchOptions } from "./document-io.js";