docxmlater 0.1.0
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.
- package/LICENSE +21 -0
- package/README.md +624 -0
- package/dist/core/Document.d.ts +215 -0
- package/dist/core/Document.d.ts.map +1 -0
- package/dist/core/Document.js +980 -0
- package/dist/core/Document.js.map +1 -0
- package/dist/core/Relationship.d.ts +42 -0
- package/dist/core/Relationship.d.ts.map +1 -0
- package/dist/core/Relationship.js +115 -0
- package/dist/core/Relationship.js.map +1 -0
- package/dist/core/RelationshipManager.d.ts +28 -0
- package/dist/core/RelationshipManager.d.ts.map +1 -0
- package/dist/core/RelationshipManager.js +126 -0
- package/dist/core/RelationshipManager.js.map +1 -0
- package/dist/elements/Bookmark.d.ts +22 -0
- package/dist/elements/Bookmark.d.ts.map +1 -0
- package/dist/elements/Bookmark.js +75 -0
- package/dist/elements/Bookmark.js.map +1 -0
- package/dist/elements/BookmarkManager.d.ts +22 -0
- package/dist/elements/BookmarkManager.d.ts.map +1 -0
- package/dist/elements/BookmarkManager.js +76 -0
- package/dist/elements/BookmarkManager.js.map +1 -0
- package/dist/elements/Comment.d.ts +42 -0
- package/dist/elements/Comment.d.ts.map +1 -0
- package/dist/elements/Comment.js +156 -0
- package/dist/elements/Comment.js.map +1 -0
- package/dist/elements/CommentManager.d.ts +40 -0
- package/dist/elements/CommentManager.d.ts.map +1 -0
- package/dist/elements/CommentManager.js +177 -0
- package/dist/elements/CommentManager.js.map +1 -0
- package/dist/elements/Field.d.ts +36 -0
- package/dist/elements/Field.d.ts.map +1 -0
- package/dist/elements/Field.js +216 -0
- package/dist/elements/Field.js.map +1 -0
- package/dist/elements/Footer.d.ts +32 -0
- package/dist/elements/Footer.d.ts.map +1 -0
- package/dist/elements/Footer.js +125 -0
- package/dist/elements/Footer.js.map +1 -0
- package/dist/elements/Header.d.ts +32 -0
- package/dist/elements/Header.d.ts.map +1 -0
- package/dist/elements/Header.js +125 -0
- package/dist/elements/Header.js.map +1 -0
- package/dist/elements/HeaderFooterManager.d.ts +37 -0
- package/dist/elements/HeaderFooterManager.d.ts.map +1 -0
- package/dist/elements/HeaderFooterManager.js +87 -0
- package/dist/elements/HeaderFooterManager.js.map +1 -0
- package/dist/elements/Hyperlink.d.ts +39 -0
- package/dist/elements/Hyperlink.d.ts.map +1 -0
- package/dist/elements/Hyperlink.js +107 -0
- package/dist/elements/Hyperlink.js.map +1 -0
- package/dist/elements/Image.d.ts +48 -0
- package/dist/elements/Image.d.ts.map +1 -0
- package/dist/elements/Image.js +431 -0
- package/dist/elements/Image.js.map +1 -0
- package/dist/elements/ImageManager.d.ts +33 -0
- package/dist/elements/ImageManager.d.ts.map +1 -0
- package/dist/elements/ImageManager.js +109 -0
- package/dist/elements/ImageManager.js.map +1 -0
- package/dist/elements/Paragraph.d.ts +82 -0
- package/dist/elements/Paragraph.d.ts.map +1 -0
- package/dist/elements/Paragraph.js +272 -0
- package/dist/elements/Paragraph.js.map +1 -0
- package/dist/elements/Revision.d.ts +34 -0
- package/dist/elements/Revision.d.ts.map +1 -0
- package/dist/elements/Revision.js +115 -0
- package/dist/elements/Revision.js.map +1 -0
- package/dist/elements/RevisionManager.d.ts +31 -0
- package/dist/elements/RevisionManager.d.ts.map +1 -0
- package/dist/elements/RevisionManager.js +96 -0
- package/dist/elements/RevisionManager.js.map +1 -0
- package/dist/elements/Run.d.ts +39 -0
- package/dist/elements/Run.d.ts.map +1 -0
- package/dist/elements/Run.js +140 -0
- package/dist/elements/Run.js.map +1 -0
- package/dist/elements/Section.d.ts +65 -0
- package/dist/elements/Section.d.ts.map +1 -0
- package/dist/elements/Section.js +232 -0
- package/dist/elements/Section.js.map +1 -0
- package/dist/elements/Table.d.ts +49 -0
- package/dist/elements/Table.d.ts.map +1 -0
- package/dist/elements/Table.js +159 -0
- package/dist/elements/Table.js.map +1 -0
- package/dist/elements/TableCell.d.ts +46 -0
- package/dist/elements/TableCell.d.ts.map +1 -0
- package/dist/elements/TableCell.js +128 -0
- package/dist/elements/TableCell.js.map +1 -0
- package/dist/elements/TableOfContents.d.ts +36 -0
- package/dist/elements/TableOfContents.d.ts.map +1 -0
- package/dist/elements/TableOfContents.js +197 -0
- package/dist/elements/TableOfContents.js.map +1 -0
- package/dist/elements/TableOfContentsElement.d.ts +14 -0
- package/dist/elements/TableOfContentsElement.d.ts.map +1 -0
- package/dist/elements/TableOfContentsElement.js +32 -0
- package/dist/elements/TableOfContentsElement.js.map +1 -0
- package/dist/elements/TableRow.d.ts +25 -0
- package/dist/elements/TableRow.d.ts.map +1 -0
- package/dist/elements/TableRow.js +84 -0
- package/dist/elements/TableRow.js.map +1 -0
- package/dist/formatting/AbstractNumbering.d.ts +31 -0
- package/dist/formatting/AbstractNumbering.d.ts.map +1 -0
- package/dist/formatting/AbstractNumbering.js +132 -0
- package/dist/formatting/AbstractNumbering.js.map +1 -0
- package/dist/formatting/NumberingInstance.d.ts +16 -0
- package/dist/formatting/NumberingInstance.d.ts.map +1 -0
- package/dist/formatting/NumberingInstance.js +35 -0
- package/dist/formatting/NumberingInstance.js.map +1 -0
- package/dist/formatting/NumberingLevel.d.ts +37 -0
- package/dist/formatting/NumberingLevel.d.ts.map +1 -0
- package/dist/formatting/NumberingLevel.js +164 -0
- package/dist/formatting/NumberingLevel.js.map +1 -0
- package/dist/formatting/NumberingManager.d.ts +33 -0
- package/dist/formatting/NumberingManager.d.ts.map +1 -0
- package/dist/formatting/NumberingManager.js +162 -0
- package/dist/formatting/NumberingManager.js.map +1 -0
- package/dist/formatting/Style.d.ts +38 -0
- package/dist/formatting/Style.d.ts.map +1 -0
- package/dist/formatting/Style.js +298 -0
- package/dist/formatting/Style.js.map +1 -0
- package/dist/formatting/StylesManager.d.ts +30 -0
- package/dist/formatting/StylesManager.d.ts.map +1 -0
- package/dist/formatting/StylesManager.js +165 -0
- package/dist/formatting/StylesManager.js.map +1 -0
- package/dist/index.d.ts +40 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +135 -0
- package/dist/index.js.map +1 -0
- package/dist/utils/units.d.ts +89 -0
- package/dist/utils/units.d.ts.map +1 -0
- package/dist/utils/units.js +176 -0
- package/dist/utils/units.js.map +1 -0
- package/dist/utils/validation.d.ts +16 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/dist/utils/validation.js +151 -0
- package/dist/utils/validation.js.map +1 -0
- package/dist/xml/XMLBuilder.d.ts +24 -0
- package/dist/xml/XMLBuilder.d.ts.map +1 -0
- package/dist/xml/XMLBuilder.js +126 -0
- package/dist/xml/XMLBuilder.js.map +1 -0
- package/dist/xml/XMLParser.d.ts +10 -0
- package/dist/xml/XMLParser.d.ts.map +1 -0
- package/dist/xml/XMLParser.js +120 -0
- package/dist/xml/XMLParser.js.map +1 -0
- package/dist/zip/ZipHandler.d.ts +50 -0
- package/dist/zip/ZipHandler.d.ts.map +1 -0
- package/dist/zip/ZipHandler.js +307 -0
- package/dist/zip/ZipHandler.js.map +1 -0
- package/dist/zip/ZipReader.d.ts +21 -0
- package/dist/zip/ZipReader.d.ts.map +1 -0
- package/dist/zip/ZipReader.js +152 -0
- package/dist/zip/ZipReader.js.map +1 -0
- package/dist/zip/ZipWriter.d.ts +20 -0
- package/dist/zip/ZipWriter.d.ts.map +1 -0
- package/dist/zip/ZipWriter.js +118 -0
- package/dist/zip/ZipWriter.js.map +1 -0
- package/dist/zip/errors.d.ts +19 -0
- package/dist/zip/errors.d.ts.map +1 -0
- package/dist/zip/errors.js +52 -0
- package/dist/zip/errors.js.map +1 -0
- package/dist/zip/types.d.ts +37 -0
- package/dist/zip/types.d.ts.map +1 -0
- package/dist/zip/types.js +23 -0
- package/dist/zip/types.js.map +1 -0
- package/package.json +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 DocXML Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,624 @@
|
|
|
1
|
+
# DocXML - Professional DOCX Editing Framework
|
|
2
|
+
|
|
3
|
+
[](https://github.com/yourusername/docxml)
|
|
4
|
+
[](https://www.typescriptlang.org/)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
A comprehensive, production-ready TypeScript/JavaScript library for creating, reading, and manipulating Microsoft Word (.docx) documents programmatically.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **High-Level Document API** - Create documents with a simple, intuitive interface
|
|
12
|
+
- **Text Formatting** - Bold, italic, fonts, colors, and 15+ formatting options
|
|
13
|
+
- **Paragraph Formatting** - Alignment, indentation, spacing, keep-with-next
|
|
14
|
+
- **Tables** - Full table support with borders, shading, cell merging
|
|
15
|
+
- **Images** - Embed PNG, JPEG, GIF images with sizing and positioning
|
|
16
|
+
- **Styles System** - 13 built-in styles + custom style creation
|
|
17
|
+
- **ZIP Archive Handling** - Low-level DOCX manipulation
|
|
18
|
+
- **TypeScript First** - Full type safety with comprehensive definitions
|
|
19
|
+
- **Well Tested** - 159 tests, all passing
|
|
20
|
+
- **Production Ready** - Used in real-world applications
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install docxml
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Quick Start
|
|
29
|
+
|
|
30
|
+
### Create Your First Document
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import { Document } from "docxml";
|
|
34
|
+
|
|
35
|
+
const doc = Document.create();
|
|
36
|
+
|
|
37
|
+
// Add content with styles
|
|
38
|
+
doc.createParagraph("My Document").setStyle("Title");
|
|
39
|
+
doc.createParagraph("Introduction").setStyle("Heading1");
|
|
40
|
+
doc.createParagraph("This is body text with the Normal style.");
|
|
41
|
+
|
|
42
|
+
// Save
|
|
43
|
+
await doc.save("my-document.docx");
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Add Formatted Text
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
const doc = Document.create();
|
|
50
|
+
|
|
51
|
+
// Create a paragraph with mixed formatting
|
|
52
|
+
const para = doc.createParagraph();
|
|
53
|
+
para.addText("Bold text", { bold: true });
|
|
54
|
+
para.addText(" and ");
|
|
55
|
+
para.addText("colored text", { color: "FF0000" });
|
|
56
|
+
|
|
57
|
+
await doc.save("formatted.docx");
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Create Tables
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
const doc = Document.create();
|
|
64
|
+
|
|
65
|
+
// Create a 3x3 table
|
|
66
|
+
const table = doc.createTable(3, 3);
|
|
67
|
+
|
|
68
|
+
// Add borders
|
|
69
|
+
table.setAllBorders({ style: "single", size: 8, color: "000000" });
|
|
70
|
+
|
|
71
|
+
// Populate cells
|
|
72
|
+
table.getCell(0, 0)?.createParagraph("Header 1");
|
|
73
|
+
table.getCell(0, 1)?.createParagraph("Header 2");
|
|
74
|
+
|
|
75
|
+
// Add shading to header row
|
|
76
|
+
table.getRow(0)?.getCell(0)?.setShading({ fill: "4472C4" });
|
|
77
|
+
|
|
78
|
+
await doc.save("table.docx");
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Add Images
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import { Document, Image, inchesToEmus } from "docxml";
|
|
85
|
+
|
|
86
|
+
const doc = Document.create();
|
|
87
|
+
|
|
88
|
+
// Add title
|
|
89
|
+
doc.createParagraph("Document with Image").setStyle("Title");
|
|
90
|
+
|
|
91
|
+
// Create image from file
|
|
92
|
+
const image = Image.fromFile("./photo.png");
|
|
93
|
+
image.setWidth(inchesToEmus(4), true); // 4 inches wide, maintain aspect ratio
|
|
94
|
+
|
|
95
|
+
// Add image to document
|
|
96
|
+
doc.addImage(image);
|
|
97
|
+
|
|
98
|
+
// Add caption
|
|
99
|
+
doc
|
|
100
|
+
.createParagraph("Figure 1: Sample image")
|
|
101
|
+
.setAlignment("center")
|
|
102
|
+
.addText("Figure 1: Sample image", { italic: true, size: 10 });
|
|
103
|
+
|
|
104
|
+
await doc.save("with-image.docx");
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Use Custom Styles
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
import { Document, Style } from "docxml";
|
|
111
|
+
|
|
112
|
+
const doc = Document.create();
|
|
113
|
+
|
|
114
|
+
// Create a custom style
|
|
115
|
+
const alertStyle = Style.create({
|
|
116
|
+
styleId: "Alert",
|
|
117
|
+
name: "Alert",
|
|
118
|
+
type: "paragraph",
|
|
119
|
+
basedOn: "Normal",
|
|
120
|
+
runFormatting: {
|
|
121
|
+
bold: true,
|
|
122
|
+
color: "FF0000",
|
|
123
|
+
size: 12,
|
|
124
|
+
},
|
|
125
|
+
paragraphFormatting: {
|
|
126
|
+
alignment: "center",
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
doc.addStyle(alertStyle);
|
|
131
|
+
doc.createParagraph("Important Warning").setStyle("Alert");
|
|
132
|
+
|
|
133
|
+
await doc.save("custom-styles.docx");
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Core Concepts
|
|
137
|
+
|
|
138
|
+
### Document Class
|
|
139
|
+
|
|
140
|
+
The high-level API for creating Word documents:
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
// Create new document
|
|
144
|
+
const doc = Document.create({
|
|
145
|
+
properties: {
|
|
146
|
+
title: "My Document",
|
|
147
|
+
creator: "DocXML",
|
|
148
|
+
subject: "Example",
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
// Add content
|
|
153
|
+
doc.createParagraph("Content here");
|
|
154
|
+
doc.createTable(5, 3);
|
|
155
|
+
|
|
156
|
+
// Save
|
|
157
|
+
await doc.save("output.docx");
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Paragraphs and Runs
|
|
161
|
+
|
|
162
|
+
Paragraphs contain runs of formatted text:
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
const para = doc.createParagraph();
|
|
166
|
+
|
|
167
|
+
// Add text with different formatting
|
|
168
|
+
para.addText("Normal ");
|
|
169
|
+
para.addText("Bold", { bold: true });
|
|
170
|
+
para.addText(" Italic", { italic: true });
|
|
171
|
+
|
|
172
|
+
// Set paragraph formatting
|
|
173
|
+
para.setAlignment("center");
|
|
174
|
+
para.setSpaceAfter(240);
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Tables
|
|
178
|
+
|
|
179
|
+
Create tables with full formatting control:
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
const table = doc.createTable(4, 3);
|
|
183
|
+
|
|
184
|
+
// Format table
|
|
185
|
+
table.setWidth(8640); // Full page width
|
|
186
|
+
table.setAllBorders({ style: "single", size: 6 });
|
|
187
|
+
|
|
188
|
+
// Access cells
|
|
189
|
+
const cell = table.getCell(0, 0);
|
|
190
|
+
cell?.createParagraph("Cell content");
|
|
191
|
+
cell?.setShading({ fill: "D9E1F2" });
|
|
192
|
+
cell?.setVerticalAlignment("center");
|
|
193
|
+
|
|
194
|
+
// Merge cells
|
|
195
|
+
cell?.setColumnSpan(3); // Span 3 columns
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### Styles
|
|
199
|
+
|
|
200
|
+
13 built-in styles ready to use:
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
// Built-in styles
|
|
204
|
+
doc.createParagraph("Title").setStyle("Title");
|
|
205
|
+
doc.createParagraph("Subtitle").setStyle("Subtitle");
|
|
206
|
+
doc.createParagraph("Chapter 1").setStyle("Heading1");
|
|
207
|
+
doc.createParagraph("Section 1.1").setStyle("Heading2");
|
|
208
|
+
doc.createParagraph("Body text").setStyle("Normal");
|
|
209
|
+
|
|
210
|
+
// Check available styles
|
|
211
|
+
console.log(doc.getStylesManager().getStyleCount()); // 13
|
|
212
|
+
|
|
213
|
+
// Create custom styles (see examples)
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
## API Overview
|
|
217
|
+
|
|
218
|
+
### Document
|
|
219
|
+
|
|
220
|
+
```typescript
|
|
221
|
+
// Creating
|
|
222
|
+
Document.create(options?)
|
|
223
|
+
Document.load(filePath)
|
|
224
|
+
Document.loadFromBuffer(buffer)
|
|
225
|
+
|
|
226
|
+
// Content
|
|
227
|
+
doc.createParagraph(text?)
|
|
228
|
+
doc.createTable(rows, columns)
|
|
229
|
+
doc.addParagraph(paragraph)
|
|
230
|
+
doc.addTable(table)
|
|
231
|
+
|
|
232
|
+
// Styles
|
|
233
|
+
doc.addStyle(style)
|
|
234
|
+
doc.getStyle(styleId)
|
|
235
|
+
doc.hasStyle(styleId)
|
|
236
|
+
doc.getStylesManager()
|
|
237
|
+
|
|
238
|
+
// Saving
|
|
239
|
+
doc.save(filePath)
|
|
240
|
+
doc.toBuffer()
|
|
241
|
+
|
|
242
|
+
// Access
|
|
243
|
+
doc.getParagraphs()
|
|
244
|
+
doc.getTables()
|
|
245
|
+
doc.getImageManager()
|
|
246
|
+
doc.getProperties()
|
|
247
|
+
doc.setProperties(props)
|
|
248
|
+
|
|
249
|
+
// Images
|
|
250
|
+
doc.addImage(image)
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
### Paragraph
|
|
254
|
+
|
|
255
|
+
```typescript
|
|
256
|
+
// Content
|
|
257
|
+
para.addText(text, formatting?)
|
|
258
|
+
para.addRun(run)
|
|
259
|
+
para.setText(text, formatting?)
|
|
260
|
+
|
|
261
|
+
// Formatting
|
|
262
|
+
para.setAlignment('left' | 'center' | 'right' | 'justify')
|
|
263
|
+
para.setLeftIndent(twips)
|
|
264
|
+
para.setRightIndent(twips)
|
|
265
|
+
para.setSpaceBefore(twips)
|
|
266
|
+
para.setSpaceAfter(twips)
|
|
267
|
+
para.setLineSpacing(twips, rule?)
|
|
268
|
+
|
|
269
|
+
// Styles
|
|
270
|
+
para.setStyle(styleId)
|
|
271
|
+
|
|
272
|
+
// Special
|
|
273
|
+
para.setKeepNext()
|
|
274
|
+
para.setKeepLines()
|
|
275
|
+
para.setPageBreakBefore()
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
### Run (Text Formatting)
|
|
279
|
+
|
|
280
|
+
```typescript
|
|
281
|
+
const formatting = {
|
|
282
|
+
bold: true,
|
|
283
|
+
italic: true,
|
|
284
|
+
underline: "single",
|
|
285
|
+
font: "Arial",
|
|
286
|
+
size: 12, // points
|
|
287
|
+
color: "FF0000", // hex without #
|
|
288
|
+
highlight: "yellow",
|
|
289
|
+
strike: true,
|
|
290
|
+
subscript: true,
|
|
291
|
+
superscript: true,
|
|
292
|
+
smallCaps: true,
|
|
293
|
+
allCaps: true,
|
|
294
|
+
};
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
### Table
|
|
298
|
+
|
|
299
|
+
```typescript
|
|
300
|
+
// Creation
|
|
301
|
+
const table = doc.createTable(rows, cols);
|
|
302
|
+
|
|
303
|
+
// Access
|
|
304
|
+
table.getRow(index);
|
|
305
|
+
table.getCell(rowIndex, colIndex);
|
|
306
|
+
|
|
307
|
+
// Formatting
|
|
308
|
+
table.setWidth(twips);
|
|
309
|
+
table.setAlignment("left" | "center" | "right");
|
|
310
|
+
table.setAllBorders(border);
|
|
311
|
+
table.setBorders(borders);
|
|
312
|
+
table.setLayout("auto" | "fixed");
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
### Style
|
|
316
|
+
|
|
317
|
+
```typescript
|
|
318
|
+
Style.create(properties);
|
|
319
|
+
Style.createNormalStyle();
|
|
320
|
+
Style.createHeadingStyle(1 - 9);
|
|
321
|
+
Style.createTitleStyle();
|
|
322
|
+
Style.createSubtitleStyle();
|
|
323
|
+
|
|
324
|
+
// Properties
|
|
325
|
+
style.setBasedOn(styleId);
|
|
326
|
+
style.setParagraphFormatting(formatting);
|
|
327
|
+
style.setRunFormatting(formatting);
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
### Image
|
|
331
|
+
|
|
332
|
+
```typescript
|
|
333
|
+
// Creation
|
|
334
|
+
Image.fromFile(filePath, width?, height?)
|
|
335
|
+
Image.fromBuffer(buffer, extension, width?, height?)
|
|
336
|
+
Image.create(properties)
|
|
337
|
+
|
|
338
|
+
// Sizing (all in EMUs - use inchesToEmus() helper)
|
|
339
|
+
image.setWidth(emus, maintainAspectRatio?)
|
|
340
|
+
image.setHeight(emus, maintainAspectRatio?)
|
|
341
|
+
image.setSize(width, height)
|
|
342
|
+
|
|
343
|
+
// Properties
|
|
344
|
+
image.getWidth()
|
|
345
|
+
image.getHeight()
|
|
346
|
+
image.getExtension()
|
|
347
|
+
image.getImageData()
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
## Built-in Styles
|
|
351
|
+
|
|
352
|
+
All documents include 13 ready-to-use styles:
|
|
353
|
+
|
|
354
|
+
| Style | Description | Font | Size | Color |
|
|
355
|
+
| ----------------- | ------------------- | ------------- | ------- | --------------- |
|
|
356
|
+
| **Normal** | Default paragraph | Calibri | 11pt | Black |
|
|
357
|
+
| **Heading1** | Major headings | Calibri Light | 16pt | Blue (#2E74B5) |
|
|
358
|
+
| **Heading2** | Section headings | Calibri Light | 13pt | Blue (#1F4D78) |
|
|
359
|
+
| **Heading3-9** | Subsection headings | Calibri Light | 12-11pt | Blue (#1F4D78) |
|
|
360
|
+
| **Title** | Document title | Calibri Light | 28pt | Blue (#2E74B5) |
|
|
361
|
+
| **Subtitle** | Document subtitle | Calibri Light | 14pt | Gray, Italic |
|
|
362
|
+
| **ListParagraph** | List items | Calibri | 11pt | Black, Indented |
|
|
363
|
+
|
|
364
|
+
See [Using Styles Guide](docs/guides/using-styles.md) for complete documentation.
|
|
365
|
+
|
|
366
|
+
## Examples
|
|
367
|
+
|
|
368
|
+
The `examples/` directory contains comprehensive examples:
|
|
369
|
+
|
|
370
|
+
### Basic Examples (`examples/01-basic/`)
|
|
371
|
+
|
|
372
|
+
- Creating simple documents
|
|
373
|
+
- Reading and modifying DOCX files
|
|
374
|
+
- Working with ZIP archives
|
|
375
|
+
|
|
376
|
+
### Text Formatting (`examples/02-text/`)
|
|
377
|
+
|
|
378
|
+
- Paragraph formatting examples
|
|
379
|
+
- Text formatting (bold, italic, colors)
|
|
380
|
+
- Advanced formatting techniques
|
|
381
|
+
|
|
382
|
+
### Tables (`examples/03-tables/`)
|
|
383
|
+
|
|
384
|
+
- Simple tables
|
|
385
|
+
- Tables with borders and shading
|
|
386
|
+
- Complex tables with merged cells
|
|
387
|
+
|
|
388
|
+
### Styles (`examples/04-styles/`)
|
|
389
|
+
|
|
390
|
+
- Using built-in styles
|
|
391
|
+
- Creating custom styles
|
|
392
|
+
- Style inheritance
|
|
393
|
+
|
|
394
|
+
### Images (`examples/05-images/`)
|
|
395
|
+
|
|
396
|
+
- Adding images from files and buffers
|
|
397
|
+
- Sizing and resizing images
|
|
398
|
+
- Multiple images in documents
|
|
399
|
+
- Images with text content
|
|
400
|
+
|
|
401
|
+
### Complete Examples (`examples/06-complete/`)
|
|
402
|
+
|
|
403
|
+
- Professional reports
|
|
404
|
+
- Invoice templates
|
|
405
|
+
- Styled documents
|
|
406
|
+
|
|
407
|
+
Run any example:
|
|
408
|
+
|
|
409
|
+
```bash
|
|
410
|
+
npx ts-node examples/02-text/paragraph-basics.ts
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
## Documentation
|
|
414
|
+
|
|
415
|
+
- **[Getting Started Guide](docs/guides/getting-started.md)** - Your first document
|
|
416
|
+
- **[Using Styles](docs/guides/using-styles.md)** - Complete styles guide
|
|
417
|
+
- **[Working with Tables](docs/guides/working-with-tables.md)** - Table guide
|
|
418
|
+
- **[API Reference](docs/api/)** - Complete API documentation
|
|
419
|
+
- **[Architecture](docs/architecture/)** - System architecture
|
|
420
|
+
|
|
421
|
+
## Advanced Usage
|
|
422
|
+
|
|
423
|
+
### Load and Modify Existing Documents
|
|
424
|
+
|
|
425
|
+
```typescript
|
|
426
|
+
// Load existing DOCX
|
|
427
|
+
const doc = await Document.load("existing.docx");
|
|
428
|
+
|
|
429
|
+
// Add content
|
|
430
|
+
doc.createParagraph("New paragraph added");
|
|
431
|
+
|
|
432
|
+
// Save
|
|
433
|
+
await doc.save("modified.docx");
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
### Work with Buffers
|
|
437
|
+
|
|
438
|
+
```typescript
|
|
439
|
+
// Create document
|
|
440
|
+
const doc = Document.create();
|
|
441
|
+
doc.createParagraph("Content");
|
|
442
|
+
|
|
443
|
+
// Save to buffer
|
|
444
|
+
const buffer = await doc.toBuffer();
|
|
445
|
+
|
|
446
|
+
// Load from buffer
|
|
447
|
+
const doc2 = await Document.loadFromBuffer(buffer);
|
|
448
|
+
```
|
|
449
|
+
|
|
450
|
+
### Low-Level ZIP Access
|
|
451
|
+
|
|
452
|
+
For advanced users, direct ZIP manipulation is available:
|
|
453
|
+
|
|
454
|
+
```typescript
|
|
455
|
+
import { ZipHandler, DOCX_PATHS } from "docxml";
|
|
456
|
+
|
|
457
|
+
const handler = new ZipHandler();
|
|
458
|
+
await handler.load("document.docx");
|
|
459
|
+
|
|
460
|
+
// Direct XML access
|
|
461
|
+
const xml = handler.getFileAsString(DOCX_PATHS.DOCUMENT);
|
|
462
|
+
handler.updateFile(DOCX_PATHS.DOCUMENT, modifiedXml);
|
|
463
|
+
|
|
464
|
+
await handler.save("output.docx");
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
## Development
|
|
468
|
+
|
|
469
|
+
### Setup
|
|
470
|
+
|
|
471
|
+
```bash
|
|
472
|
+
# Install dependencies
|
|
473
|
+
npm install
|
|
474
|
+
|
|
475
|
+
# Build
|
|
476
|
+
npm run build
|
|
477
|
+
|
|
478
|
+
# Run tests
|
|
479
|
+
npm test
|
|
480
|
+
|
|
481
|
+
# Run tests with coverage
|
|
482
|
+
npm run test:coverage
|
|
483
|
+
```
|
|
484
|
+
|
|
485
|
+
### Project Structure
|
|
486
|
+
|
|
487
|
+
```
|
|
488
|
+
src/
|
|
489
|
+
├── core/ # Document class
|
|
490
|
+
├── elements/ # Paragraph, Run, Table
|
|
491
|
+
├── formatting/ # Style, StylesManager
|
|
492
|
+
├── xml/ # XML generation
|
|
493
|
+
├── zip/ # ZIP archive handling
|
|
494
|
+
└── utils/ # Validation utilities
|
|
495
|
+
|
|
496
|
+
tests/
|
|
497
|
+
├── core/ # Document tests
|
|
498
|
+
├── elements/ # Element tests
|
|
499
|
+
├── formatting/ # Style tests (pending)
|
|
500
|
+
├── zip/ # ZIP tests
|
|
501
|
+
└── utils/ # Utility tests
|
|
502
|
+
|
|
503
|
+
docs/
|
|
504
|
+
├── api/ # API reference
|
|
505
|
+
├── guides/ # User guides
|
|
506
|
+
└── architecture/ # Architecture docs
|
|
507
|
+
|
|
508
|
+
examples/
|
|
509
|
+
├── 01-basic/ # Basic examples
|
|
510
|
+
├── 02-text/ # Text examples
|
|
511
|
+
├── 03-tables/ # Table examples
|
|
512
|
+
├── 04-styles/ # Style examples
|
|
513
|
+
├── 05-images/ # Image examples
|
|
514
|
+
└── 06-complete/ # Complete examples
|
|
515
|
+
```
|
|
516
|
+
|
|
517
|
+
## Phase Implementation Status
|
|
518
|
+
|
|
519
|
+
| Phase | Status | Features |
|
|
520
|
+
| -------------------------------- | ----------- | -------------------------------------------------- |
|
|
521
|
+
| **Phase 1: Foundation** | Complete | ZIP handling, XML generation, validation |
|
|
522
|
+
| **Phase 2: Core Elements** | Complete | Paragraph, Run, text formatting |
|
|
523
|
+
| **Phase 3: Advanced Formatting** | Complete | Document API, Tables, Styles, Lists |
|
|
524
|
+
| **Phase 4: Rich Content** | In Progress | Images (Complete) / Headers, Footers (In Progress) |
|
|
525
|
+
| **Phase 5: Polish** | Planned | Track changes, comments, TOC |
|
|
526
|
+
|
|
527
|
+
**Current: 159 tests passing | 17 source files | ~4,000+ lines of code**
|
|
528
|
+
|
|
529
|
+
## Requirements
|
|
530
|
+
|
|
531
|
+
- Node.js 14+
|
|
532
|
+
- TypeScript 5.0+ (for development)
|
|
533
|
+
|
|
534
|
+
## Dependencies
|
|
535
|
+
|
|
536
|
+
- **jszip** (^3.10.1) - ZIP archive handling
|
|
537
|
+
|
|
538
|
+
## Browser Support
|
|
539
|
+
|
|
540
|
+
DocXML works in Node.js environments. Browser support requires bundling with webpack/rollup and may need buffer polyfills.
|
|
541
|
+
|
|
542
|
+
## Contributing
|
|
543
|
+
|
|
544
|
+
Contributions are welcome! Please read [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
|
|
545
|
+
|
|
546
|
+
1. Fork the repository
|
|
547
|
+
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
|
|
548
|
+
3. Commit your changes (`git commit -m 'Add amazing feature'`)
|
|
549
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
|
550
|
+
5. Open a Pull Request
|
|
551
|
+
|
|
552
|
+
## Testing
|
|
553
|
+
|
|
554
|
+
All features are comprehensively tested:
|
|
555
|
+
|
|
556
|
+
```bash
|
|
557
|
+
# Run all tests
|
|
558
|
+
npm test
|
|
559
|
+
|
|
560
|
+
# Run specific test suite
|
|
561
|
+
npm test -- tests/core/Document.test.ts
|
|
562
|
+
|
|
563
|
+
# Watch mode
|
|
564
|
+
npm run test:watch
|
|
565
|
+
|
|
566
|
+
# Coverage report
|
|
567
|
+
npm run test:coverage
|
|
568
|
+
```
|
|
569
|
+
|
|
570
|
+
**Test Statistics:**
|
|
571
|
+
|
|
572
|
+
- 159 tests passing
|
|
573
|
+
- 4 test suites
|
|
574
|
+
- High code coverage
|
|
575
|
+
- Integration tests included
|
|
576
|
+
|
|
577
|
+
## License
|
|
578
|
+
|
|
579
|
+
MIT © DiaTech
|
|
580
|
+
|
|
581
|
+
## Acknowledgments
|
|
582
|
+
|
|
583
|
+
- Built with [JSZip](https://stuk.github.io/jszip/) for ZIP archive handling
|
|
584
|
+
- Follows [ECMA-376](https://www.ecma-international.org/publications-and-standards/standards/ecma-376/) Office Open XML standard
|
|
585
|
+
- Inspired by [python-docx](https://python-docx.readthedocs.io/) and [docx](https://github.com/dolanmiu/docx)
|
|
586
|
+
|
|
587
|
+
## Support
|
|
588
|
+
|
|
589
|
+
- **Documentation**: [docs/](docs/)
|
|
590
|
+
- **Examples**: [examples/](examples/)
|
|
591
|
+
- **Issues**: [GitHub Issues](https://github.com/yourusername/docxml/issues)
|
|
592
|
+
|
|
593
|
+
## Roadmap
|
|
594
|
+
|
|
595
|
+
**Phase 3 (Complete):**
|
|
596
|
+
|
|
597
|
+
- [x] Document API
|
|
598
|
+
- [x] Tables with formatting
|
|
599
|
+
- [x] Styles system
|
|
600
|
+
- [x] Lists and numbering
|
|
601
|
+
|
|
602
|
+
**Phase 4 (Current):**
|
|
603
|
+
|
|
604
|
+
- [x] Images and media
|
|
605
|
+
- [ ] Headers and footers (in progress)
|
|
606
|
+
- [ ] Page sections (in progress)
|
|
607
|
+
- [ ] Hyperlinks (in progress)
|
|
608
|
+
|
|
609
|
+
**Phase 5 (Future):**
|
|
610
|
+
|
|
611
|
+
- [ ] Track changes
|
|
612
|
+
- [ ] Comments
|
|
613
|
+
- [ ] Table of contents
|
|
614
|
+
- [ ] Fields
|
|
615
|
+
|
|
616
|
+
## Related Projects
|
|
617
|
+
|
|
618
|
+
- **[python-docx](https://python-docx.readthedocs.io/)** - Python DOCX library
|
|
619
|
+
- **[docx](https://github.com/dolanmiu/docx)** - JavaScript DOCX library
|
|
620
|
+
- **[mammoth.js](https://github.com/mwilliamson/mammoth.js)** - Convert DOCX to HTML
|
|
621
|
+
|
|
622
|
+
---
|
|
623
|
+
|
|
624
|
+
**Ready to get started?** Check out the [Quick Start Guide](docs/guides/getting-started.md) or explore the [examples](examples/).
|