Compare commits

...

182 Commits

Author SHA1 Message Date
9e8d67c4a9 Version bump 2019-06-28 01:59:18 +01:00
cbbc4a80e7 FIx up API for column count 2019-06-28 01:57:43 +01:00
1f51fd7a31 Remove usage of Utility 2019-06-27 01:35:58 +01:00
120d97bc64 Remove Utility 2019-06-27 01:10:39 +01:00
a531713214 Remove usage of Utility class 2019-06-26 22:12:18 +01:00
564a055316 Remove jsonify method 2019-06-26 02:11:43 +01:00
6c29c4fb1f Merge pull request #343 from markcharyk/no-pretty-xml
Make prettified XML a Packer-wide configurable option
2019-06-26 00:44:33 +01:00
552580bc47 Merge pull request #340 from filippomuscolino/feat/row-height
Add table row height
2019-06-25 20:18:17 +01:00
0998d43b8f Merge pull request #341 from filippomuscolino/fix/missing-exports
Fix: add missing exports
2019-06-25 15:38:13 +01:00
b08354494c Improve tests 2019-06-25 14:48:45 +02:00
f54b9a197d Fix: add missing exports 2019-06-25 14:35:35 +02:00
2d4412ce51 Add table row height 2019-06-25 12:02:53 +02:00
72c32378c5 Make prettified XML a Packer-wide configurable option
The xml() function was being passed a flag for pretty printing the XML in word/document.xml. This was causing some parsers (not Word itself) to break when they didn't expect whitepsace. It was also causing some files to be prettified and others to be ugly. This ensures consistent prettification.
2019-06-21 13:53:01 -07:00
0c79c0ac83 Merge pull request #337 from zahrafali/multiple-columns
Multiple columns in a section
2019-06-21 15:36:33 +01:00
1dfc27ba08 Multiple columns in a section 2019-06-21 01:22:27 +05:30
58dc6fe389 Format demo 2019-06-12 01:12:32 +01:00
34c3285426 Version bump 2019-06-12 01:07:53 +01:00
a5afce458d Merge pull request #262 from aravindballa/master
Feature: add line numbers to section
2019-06-05 17:38:49 +01:00
2f687125d1 fix lint issue in section-properties.ts 2019-06-05 13:01:24 +05:30
5c1a731314 Version bump 2019-05-16 22:15:50 +01:00
622331ad24 Merge pull request #324 from Scarface2013/master
Fix merging of 3+ cells vertically
2019-05-16 21:06:20 +01:00
3a593a53d3 Fix tests 2019-05-16 11:15:20 -04:00
31a9111667 Fix merging of 3+ cells vertically 2019-05-15 18:14:39 -04:00
373c890354 Add docsify serve 2019-04-30 00:30:18 +01:00
723d76d06f Merge pull request #314 from efx/fix-doc-links
fix broken documentation links
2019-04-29 12:08:27 +01:00
a24d745d75 fix broken documentation links 2019-04-25 09:49:44 -04:00
90891cfafd Merge pull request #310 from nickgeorgiou/feat/margin-spelling-fix
Fix spelling of "margin"
2019-04-18 17:10:00 +01:00
29f890918c Fix spelling of "margin" 2019-04-18 13:55:18 +10:00
b0f8f8ddbd Merge pull request #308 from filippomuscolino/master
Update documentation
2019-04-12 14:43:32 +01:00
9c89c1ab59 Update docs 2019-04-12 13:46:05 +02:00
7a9cb92955 Merge pull request #307 from brucehappy/features/xml_optimization
Optimize XML output
2019-04-11 22:50:10 +01:00
77edf8862b Added constant EMPTY_OBJECT (an empty sealed object) that is used to indicate to the xml library that an empty XML element should be generated, and use it in the JSON hardcoded into the tests. 2019-04-10 13:47:38 -04:00
bb1604cd8f Turn back on no-null-keyword. Use empty object instead of null to signal to the xml library that an empty element should be produced. Update the related tests.
Related to #306
2019-04-10 01:28:37 -04:00
816cb54b14 Optimize XML output by properly constructing objects to send to the xml library so that it can produce proper empty elements.
Rework the way attributes are stored in ImportedXmlComponent to match elsewhere (required allowing for a null xmlKeys in the XmlAttributeComponent interface).
Rework the way paragraphs get added to the end of table cells if needed.
The goal in both reworks is to not mess around with the objects output from `prepForXml` if we can avoid it.
Made the output of RunProperties, ParagraphProperties, TableCellProperties, TableRowProperties, and TableProperties all optional based on whether they contain any attributes or children.  Changed code in PageBorders, TableCellMargin, and TableCellBorders that implemented this same thing by overriding `prepForXml` so that it uses the new XmlComponent subclass instead.
Removed commented out code that attempted to fix-up XML output and make proper empty elements.
Fixed all affected tests.
Turn off `no-null-keyword` in the linter as we need to use null to signal to the `xml` library to create an empty element with no attributes (`undefined` will not work in its place).

Fixes #306
2019-04-09 05:27:18 -04:00
920bd3c175 Merge pull request #304 from brucehappy/issues/issue_303
Fix table cell margin type XML attribute
2019-04-08 20:30:54 +01:00
a2a01edc24 Change table cell margin type attribute form w:sz to w:type as per http://officeopenxml.com/WPtableCellMargins.php
Fixes #303
2019-04-08 13:50:40 -04:00
193d0c4239 Merge pull request #299 from filippomuscolino/fix-duplicated-section
Fix: duplicated generation of last section properties
2019-04-05 01:07:10 +01:00
d8cc11c5ab Merge pull request #300 from efx/fix-typo
fix minor typos
2019-04-05 01:03:39 +01:00
b874051f32 fix minor typos 2019-04-04 15:35:41 -04:00
272e2496f4 Fix duplicated generation of last section properties 2019-04-04 17:43:54 +02:00
43c199c725 Version bump 2019-03-30 15:45:12 +00:00
0e975b3d66 Merge pull request #292 from dolanmiu/feature/table-background
Add shading (background color) for cell
2019-03-21 01:33:11 +00:00
c6ab47e838 Add shading 2019-03-21 01:06:07 +00:00
48c17d51bb Merge pull request #288 from dolanmiu/feat/table
Add margains, widths and floats to tables
2019-03-19 00:19:55 +00:00
df2315ae4a Disable e2e for demo 32 temporarily 2019-03-19 00:07:11 +00:00
e67fd9cb2b Add tests and margain 2019-03-18 23:50:21 +00:00
639842332f Change contribution guidelines 2019-03-13 23:13:10 +00:00
2cb7d44a77 Work on new table API 2019-03-13 02:29:11 +00:00
6cd62418a5 Update README.md 2019-03-12 20:26:39 +00:00
f3ba11b21c Improving table api 2019-03-08 01:09:21 +00:00
51b1e08a35 Merge pull request #284 from askoufis/numbering-docs-typo
Fixes typo in numbering documentation
2019-03-07 17:08:33 +00:00
b0d0041ff9 Rewording and remove # 2019-03-07 15:36:06 +11:00
55c5dd818a More typos 2019-03-07 15:33:32 +11:00
3bebe0ad59 Found another typo 2019-03-07 15:32:16 +11:00
c5c67fd92a Fixes typo in numbering documentation 2019-03-07 15:28:30 +11:00
1486d3de56 Improve documentation 2019-03-07 01:01:01 +00:00
6d8eea40be Merge branch 'master' of github.com:dolanmiu/docx 2019-03-06 22:53:23 +00:00
0922d49cd3 Merge pull request #282 from filippomuscolino/fix/table-width
Fix: table width in percentage should include '%'
2019-03-06 13:52:49 +00:00
f76471f553 Fix test 2019-03-06 13:33:50 +01:00
973177676e fix: table width in percentage should include '%' 2019-03-06 12:21:15 +01:00
5aa878b901 Fix invalid link 2019-03-06 02:12:41 +00:00
b99d1fc129 correctly use include 2019-03-06 00:18:00 +00:00
1590f9ac70 Merge pull request #274 from dolanmiu/feat/vertical-table-merge
Add table column and vertical merging
2019-03-05 23:53:12 +00:00
6c772b956c Add examples to table documentation 2019-03-05 23:15:50 +00:00
9d09ff3518 Update documentation 2019-03-05 21:57:31 +00:00
d9ad69b7cd Merge pull request #278 from filippomuscolino/feat/style-improvements
Add bold and italics to character style + customize hyperlink
2019-03-05 20:59:21 +00:00
e23d230acb Merge pull request #280 from filippomuscolino/feat/justify-line-break
Add setting for not justify lines ending in soft line break
2019-03-05 20:55:21 +00:00
5ca872cb07 Merge pull request #279 from filippomuscolino/settings-fix
Fix: settings are ignored by Word without relationship and content type
2019-03-05 17:44:21 +00:00
b179facca2 Add setting for not justify lines ending in soft line break 2019-03-05 17:17:06 +01:00
db60270e1f fix: relationship and content type for settings 2019-03-05 15:40:00 +01:00
3e2130bc80 Customize text run of an hyperlink 2019-03-05 15:15:58 +01:00
1b336785b4 Add bold and italics to character style 2019-03-05 15:15:45 +01:00
6eed0fe0f5 Update contribution-guidelines.md 2019-03-05 13:32:09 +00:00
2f8f69b0c4 Update README.md 2019-03-05 13:29:36 +00:00
4580a65a84 Merge pull request #277 from filippomuscolino/feat/style-outline
Select styles that will go into TOC
2019-03-05 13:22:22 +00:00
661c3e21e4 Merge pull request #275 from filippomuscolino/feat/table-pagination
Table pagination: add cantSplit and tblHeader row properties
2019-03-05 11:24:08 +00:00
32646f8806 Add outline level to paragraph style 2019-03-05 12:19:36 +01:00
231428852e Merge pull request #276 from filippomuscolino/feat/cell-properties
Cell borders on Google Docs + set cell width
2019-03-05 11:19:29 +00:00
46ddf77342 Get cell properties 2019-03-05 11:38:21 +01:00
441afe8c97 Add tests 2019-03-05 11:34:43 +01:00
ca9ce01f56 Customize left and right cell borders (for Google Docs) 2019-03-05 11:09:47 +01:00
2b9ce0febd Add tests 2019-03-05 11:04:54 +01:00
18760387db Table pagination: add cantSplit and tblHeader row properties 2019-03-05 10:13:53 +01:00
28dabb06cb Write getColumn test 2019-03-05 01:58:56 +00:00
50fc9b6274 Test index out of bounds 2019-03-05 01:45:04 +00:00
367518d504 Add tests for table column 2019-03-05 01:39:51 +00:00
efd89f24e6 Add table column and vertical merging 2019-03-04 22:50:04 +00:00
4fd2b6f1d3 Merge branch 'feat/table-merge-fix' 2019-02-26 22:32:21 +00:00
5ae541a40d Fix descriptions of demos 2019-02-26 22:20:20 +00:00
c9fb9a827d Version bump 2019-02-26 21:54:33 +00:00
918faf59c3 Merge pull request #273 from dolanmiu/feat/table-merge-fix
Add higher width for GridCol to fix merging cells
2019-02-26 21:53:14 +00:00
3a9fa49fbb Fix test 2019-02-26 21:46:19 +00:00
218a08d793 Fix tests 2019-02-26 21:45:55 +00:00
416a239708 Add higher width for grid col to fix merging 2019-02-26 21:38:54 +00:00
d1bdbd397a Merge pull request #265 from joefitter/bugfix/conditional-coverage
dont run coverage if publishing
2019-02-09 17:43:43 +00:00
a6077b8f16 dont run coverage if publishing 2019-02-08 12:06:50 +00:00
8f133ff93a add example for line numbers 2019-02-06 13:26:18 +05:30
1f12e159ef add line numbers to section 2019-02-04 18:49:12 +05:30
83a7f4664d Update tslint.json 2019-01-28 13:04:51 +00:00
728aefc4a7 Merge pull request #256 from dolanmiu/feat/webpack-ts
Use a typescript webpack config
2019-01-24 01:16:08 +01:00
40730548bb Add webpack type definitions 2019-01-23 20:09:32 +00:00
d6c2c96757 Merge pull request #248 from filippomuscolino/master
Add OutlineLevel Paragraph property
2019-01-17 01:21:55 +01:00
b22f565dd0 Use a typescript webpack config 2019-01-16 13:34:32 +00:00
41eeac1b05 Update README.md 2019-01-16 13:26:26 +00:00
18a5f22f4c Version bump 2019-01-16 12:05:21 +01:00
2a1161d857 Add documentation 2019-01-16 12:05:21 +01:00
4b6d3c3e3c Write tests 2019-01-16 12:05:21 +01:00
14a1d62148 Add Number of pages element 2019-01-16 12:05:21 +01:00
1f98d9461b Merge pull request #250 from dolanmiu/feat/number-of-pages
Feat/number of pages
2019-01-16 01:18:11 +01:00
8dc590746b Version bump 2019-01-16 00:15:27 +00:00
de5f5c9a77 Add documentation 2019-01-16 00:13:18 +00:00
788205b14d Merge pull request #247 from dolanmiu/feat/number-of-pages
Add Total Number of pages element
2019-01-15 22:47:51 +01:00
ca5f6a56a5 Write tests 2019-01-15 21:40:19 +00:00
bf8dfe6604 Fix for prettier 2019-01-15 16:13:20 +01:00
b0ee0305fb Add OutlineLevel Paragraph property 2019-01-15 15:39:48 +01:00
8f6984580a Add Number of pages element 2019-01-15 02:09:38 +00:00
383c6d769f Merge pull request #246 from dolanmiu/feat/e2e-2
Add new e2e command
2019-01-15 02:17:21 +01:00
9f38a4f48a Test 32 instead of 30 2019-01-15 01:04:53 +00:00
475a132ce0 Fix e2e test 2019-01-15 00:50:38 +00:00
4111413074 Add relative pathing 2019-01-15 00:43:48 +00:00
50209bb435 Add Error for e2e test 2019-01-15 00:34:40 +00:00
b37d2c141d Add new e2e command 2019-01-15 00:23:01 +00:00
d19cdcae0c Rename methods 2019-01-11 01:45:57 +00:00
3f652764e9 Merge pull request #234 from dolanmiu/feat/improve-docs
Make ImportDotx easier to read
2019-01-11 01:15:42 +00:00
7b5cf690e2 Merge pull request #242 from dolanmiu/feat/more-tests2
Add more tests to paragraph
2019-01-11 00:29:45 +00:00
d674523139 Fix tests - No longer uses dummy variable 2019-01-11 00:26:16 +00:00
db7f27a88c Add more tests to paragraph 2019-01-11 00:16:25 +00:00
677300e34f Merge pull request #237 from dolanmiu/feat/image-wrap
Merge 4.6.0 into master
2019-01-10 23:53:00 +00:00
da0fa86345 Version bump 2019-01-10 02:37:40 +00:00
78b310e1dd Add Add createFirstPageFooter and createEvenPageFooter 2019-01-10 02:36:42 +00:00
4541d7c977 Text wrapping and borders for images 2019-01-10 02:10:20 +00:00
9271b2d11a Merge pull request #235 from dolanmiu/feat/floating-images
Feat/floating images
2019-01-09 21:33:52 +00:00
942c6d028c Remove unessesary null check
Trust the compiler and code
2019-01-09 21:19:57 +00:00
f717126145 Remove image-size dependency 2019-01-09 02:08:47 +00:00
abd5ace85c Merge branch 'master' of https://github.com/dolanmiu/docx into feat/floating-images
# Conflicts:
#	src/file/drawing/drawing.ts
2019-01-09 02:04:06 +00:00
612c024b1f Use string style factory 2019-01-07 21:43:04 +00:00
12ad545fe8 Write ImportDotx more functionally 2019-01-07 21:41:39 +00:00
6d0f6a61d7 Merge pull request #231 from Vivekananda-Athukuri/master
Export Num class
2019-01-04 12:06:14 +00:00
e3bcad6d3c Fix missing media in Document Template 2019-01-04 00:11:50 +00:00
f7c372a85c Merge branch 'master' into feat/improve-docs 2019-01-03 20:56:05 +00:00
bb277a4a76 Format demo 2019-01-03 20:47:00 +00:00
52b78a583e Merge pull request #202 from dolanmiu/feat/correct-mine-type
Remove async await for Compiler.compile
2019-01-03 10:45:56 +00:00
20ab36191b Merge pull request #228 from dolanmiu/feat/improve-docs
Change table methods and document it
2019-01-03 02:43:16 +00:00
dbfb80e660 Add more tests 2019-01-03 02:36:56 +00:00
421f4471de Merge branch 'master' into feat/improve-docs
# Conflicts:
#	src/file/footer-wrapper.spec.ts
2019-01-03 02:12:05 +00:00
04ea2b2dc9 Add footer tests and improve table documentation 2019-01-03 02:11:04 +00:00
93a2404343 Merge pull request #229 from microbit-matt-hillsdon/get-started-packer-fix
Fix getting started docs for v4 packer.
2019-01-02 16:15:41 +00:00
149cda9a3b Fix getting started docs for v4 packer. 2019-01-02 15:56:13 +00:00
b9465b2a20 Add more changes to table API and documentation 2018-12-31 01:55:15 +00:00
ab07f2ecbe Change table methods and document it 2018-12-29 01:57:20 +00:00
c70c147afe Merge pull request #217 from dolanmiu/feat/fix-header-image-bug
Feat/fix header image bug
2018-12-28 15:19:44 +00:00
8db52212ab Remove id from media 2018-12-24 16:50:53 +00:00
d021a3995e Sending media data down image 2018-12-21 01:39:20 +00:00
1ea347ed21 Clean up 2018-12-18 23:37:21 +00:00
0725e54271 Add tests 2018-12-16 01:56:42 +00:00
3b6aca2451 Merge pull request #220 from efx/topic-docs
add documentation on positioning units
2018-12-12 00:17:55 +00:00
249e44532f add documentation on positioning units
See further discussions in #50, #191.
2018-12-11 16:49:18 -05:00
d3af3663ec Merge branch 'release/4.4.1' 2018-12-10 19:33:00 +00:00
bf18154fcf Fix style 2018-12-07 15:34:27 +00:00
5efc9e383b Update index.html 2018-12-06 14:30:24 +00:00
6505119d3b Merge pull request #205 from dolanmiu/refactor/style-refactor
Refactor in Style
2018-12-05 21:09:30 +00:00
600f97de29 Update README.md 2018-12-05 20:11:46 +00:00
a1c21d2143 Add space 2018-12-05 19:47:56 +00:00
39066fb5f2 Add lifecycles to add image 2018-12-05 00:05:11 +00:00
8fd99052fb Merge pull request #207 from dolanmiu/feat/rename-italic
Breaking change. Make all italic to italics.
2018-11-18 15:47:35 +00:00
4e671eb512 Breaking change. Make all italic to italics. 2018-11-18 15:22:23 +00:00
2677c4a4ec Merge pull request #206 from dolanmiu/feat/refactor-setwidth
Breaking Change. Swap arguments around and make default WidthType
2018-11-15 03:08:00 +00:00
e19890e27a Breaking Change. Swap arguments around and make default WidthType 2018-11-15 03:00:26 +00:00
28556f277b Update contribution-guidelines.md 2018-11-13 14:38:26 +00:00
50e08f198c Linter ajusts 2018-11-13 11:04:29 -02:00
7639b60b15 Refactor to separate classes in their specific files an tests improuvements for styles 2018-11-13 11:04:03 -02:00
765a9686d8 Remove async await for Compiler.compile 2018-11-12 13:17:53 +00:00
ff443aa7c4 Merge pull request #201 from dolanmiu/feat/correct-mine-type
Add correct docx mime type
2018-11-12 13:12:54 +00:00
28fc328cb3 Add more packer tests 2018-11-12 13:03:17 +00:00
083f121683 Add correct docx mime type 2018-11-12 12:32:07 +00:00
2f57d3be84 Merge pull request #197 from dolanmiu/bugfix/correct-section-page-num-type
Bugfix - Correct default section options
2018-11-11 23:05:20 +00:00
7b47dadede Fix Prettier styling 2018-11-11 22:54:35 +00:00
8ad5485347 Improuvments in headers and footers docs 2018-11-09 10:37:36 -02:00
8df78e45d9 Update in examples documentation. 2018-11-09 09:26:27 -02:00
39cef5e61d Demo 16 was updated to show how page number types format and start work. 2018-11-09 09:14:38 -02:00
5552f9d834 The page number type attribute of the sections was always been created, leading Word to always reset page numbers to zero in new sections.
The page number type DECIMAL is already the default in Word, there is no need to force this to be the default option in the default section, like it was.
2018-11-09 09:13:27 -02:00
d13795696d Update index.ts 2018-11-09 12:26:23 +05:30
196 changed files with 6895 additions and 2814 deletions

View File

@ -10,6 +10,7 @@ script:
- npm run style - npm run style
- npm run build - npm run build
- npm run ts-node -- ./demo/demo1.ts - npm run ts-node -- ./demo/demo1.ts
- npm run e2e "My Document.docx"
- npm run ts-node -- ./demo/demo2.ts - npm run ts-node -- ./demo/demo2.ts
- npm run ts-node -- ./demo/demo3.ts - npm run ts-node -- ./demo/demo3.ts
- npm run ts-node -- ./demo/demo4.ts - npm run ts-node -- ./demo/demo4.ts
@ -19,6 +20,7 @@ script:
- npm run ts-node -- ./demo/demo8.ts - npm run ts-node -- ./demo/demo8.ts
- npm run ts-node -- ./demo/demo9.ts - npm run ts-node -- ./demo/demo9.ts
- npm run ts-node -- ./demo/demo10.ts - npm run ts-node -- ./demo/demo10.ts
- npm run e2e "My Document.docx"
- npm run ts-node -- ./demo/demo11.ts - npm run ts-node -- ./demo/demo11.ts
- npm run ts-node -- ./demo/demo12.ts - npm run ts-node -- ./demo/demo12.ts
- npm run ts-node -- ./demo/demo13.ts - npm run ts-node -- ./demo/demo13.ts
@ -41,6 +43,7 @@ script:
- npm run ts-node -- ./demo/demo30.ts - npm run ts-node -- ./demo/demo30.ts
- npm run ts-node -- ./demo/demo31.ts - npm run ts-node -- ./demo/demo31.ts
- npm run ts-node -- ./demo/demo32.ts - npm run ts-node -- ./demo/demo32.ts
# - npm run e2e "My Document.docx" // Need to fix
- npm run ts-node -- ./demo/demo33.ts - npm run ts-node -- ./demo/demo33.ts
- npm run ts-node -- ./demo/demo34.ts - npm run ts-node -- ./demo/demo34.ts
after_failure: after_failure:

View File

@ -25,7 +25,7 @@
## Browser ## Browser
Here are examples of `docx` being used with basic `HTML/JS` in a browser environment. Here are examples of `docx` being used with basic `HTML/JS` in a browser environment:
* https://codepen.io/anon/pen/dqoVgQ * https://codepen.io/anon/pen/dqoVgQ
* https://jsfiddle.net/3xhezb5w/2 * https://jsfiddle.net/3xhezb5w/2
@ -64,8 +64,17 @@ Check the `examples` section in the [documentation](https://docx.js.org/#/exampl
Read the contribution guidelines [here](https://docx.js.org/#/contribution-guidelines). Read the contribution guidelines [here](https://docx.js.org/#/contribution-guidelines).
# Used by
[<img src="https://i.imgur.com/zy5qWmI.png" alt="drawing" height="200"/>](https://hfour.com/)
[<img src="https://i.imgur.com/OYP5tgS.png" alt="drawing" height="200"/>](https://fuzzproductions.com/)
[<img src="https://i.imgur.com/zUDMfZ3.png" alt="drawing" height="200"/>](https://www.mettzer.com/)
--- ---
[![patreon][patreon-image]][patreon-url]
[![browserstack][browserstack-image]][browserstack-url]
Made with 💖 Made with 💖
[npm-image]: https://badge.fury.io/js/docx.svg [npm-image]: https://badge.fury.io/js/docx.svg
@ -84,3 +93,7 @@ Made with 💖
[pr-url]: http://makeapullrequest.com [pr-url]: http://makeapullrequest.com
[codecov-image]: https://codecov.io/gh/dolanmiu/docx/branch/master/graph/badge.svg [codecov-image]: https://codecov.io/gh/dolanmiu/docx/branch/master/graph/badge.svg
[codecov-url]: https://codecov.io/gh/dolanmiu/docx [codecov-url]: https://codecov.io/gh/dolanmiu/docx
[patreon-image]: https://user-images.githubusercontent.com/2917613/51251459-4e880480-1991-11e9-92bf-38b96675a9e2.png
[patreon-url]: https://www.patreon.com/dolanmiu
[browserstack-image]: https://user-images.githubusercontent.com/2917613/54233552-128e9d00-4505-11e9-88fb-025a4e04007c.png
[browserstack-url]: https://www.browserstack.com

View File

@ -1,4 +1,4 @@
// Add images to header and footer // Generate a CV
// Import from 'docx' rather than '../build' if you install from npm // Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs"; import * as fs from "fs";
import { Document, Packer, Paragraph, TextRun } from "../build"; import { Document, Packer, Paragraph, TextRun } from "../build";
@ -233,7 +233,7 @@ class DocumentCreator {
public createRoleText(roleText: string): Paragraph { public createRoleText(roleText: string): Paragraph {
const paragraph = new Paragraph(); const paragraph = new Paragraph();
const role = new TextRun(roleText).italic(); const role = new TextRun(roleText).italics();
paragraph.addRun(role); paragraph.addRun(role);

View File

@ -84,8 +84,7 @@ doc.Styles.createParagraphStyle("ListParagraph", "List Paragraph")
.basedOn("Normal"); .basedOn("Normal");
doc.createImage(fs.readFileSync("./demo/images/pizza.gif")); doc.createImage(fs.readFileSync("./demo/images/pizza.gif"));
doc doc.createParagraph("HEADING")
.createParagraph("HEADING")
.heading1() .heading1()
.center(); .center();
@ -107,12 +106,15 @@ doc.createParagraph("Sir,").style("normalPara");
doc.createParagraph("BRIEF DESCRIPTION").style("normalPara"); doc.createParagraph("BRIEF DESCRIPTION").style("normalPara");
const table = new Table(4, 4); const table = new Table({
rows: 4,
columns: 4,
});
table table
.getRow(0) .getRow(0)
.getCell(0) .getCell(0)
.addContent(new Paragraph("Pole No.")); .addParagraph(new Paragraph("Pole No."));
// table.Properties.width = 10000;
doc.addTable(table); doc.addTable(table);
const arrboth = [ const arrboth = [
@ -127,10 +129,7 @@ const arrboth = [
]; ];
arrboth.forEach((item) => { arrboth.forEach((item) => {
const para = doc.createParagraph(); doc.createImage(fs.readFileSync(item.image));
para.addImage(doc.createImage(fs.readFileSync(item.image)));
// para.Properties.width = 60;
// para.Properties.height = 90;
doc.createParagraph(item.comment).style("normalPara2"); doc.createParagraph(item.comment).style("normalPara2");
}); });

View File

@ -1,7 +1,7 @@
// Multiple sections and headers // Multiple sections and headers
// Import from 'docx' rather than '../build' if you install from npm // Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs"; import * as fs from "fs";
import { Document, Packer, PageNumberFormat, PageOrientation, Paragraph } from "../build"; import { Document, Packer, PageNumberFormat, PageOrientation, Paragraph, TextRun } from "../build";
const doc = new Document(); const doc = new Document();
@ -41,6 +41,42 @@ doc.addSection({
doc.createParagraph("hello in landscape"); doc.createParagraph("hello in landscape");
const header2 = doc.createHeader();
const pageNumber = new TextRun("Page number: ").pageNumber();
header2.createParagraph().addRun(pageNumber);
doc.addSection({
headers: {
default: header2,
},
orientation: PageOrientation.PORTRAIT,
});
doc.createParagraph("Page number in the header must be 2, because it continues from the previous section.");
doc.addSection({
headers: {
default: header2,
},
pageNumberFormatType: PageNumberFormat.UPPER_ROMAN,
orientation: PageOrientation.PORTRAIT,
});
doc.createParagraph(
"Page number in the header must be III, because it continues from the previous section, but is defined as upper roman.",
);
doc.addSection({
headers: {
default: header2,
},
pageNumberFormatType: PageNumberFormat.DECIMAL,
pageNumberStart: 25,
orientation: PageOrientation.PORTRAIT,
});
doc.createParagraph("Page number in the header must be 25, because it is defined to start at 25 and to be decimal in this section.");
const packer = new Packer(); const packer = new Packer();
packer.toBuffer(doc).then((buffer) => { packer.toBuffer(doc).then((buffer) => {

View File

@ -5,11 +5,14 @@ import { BorderStyle, Document, Packer, Paragraph } from "../build";
const doc = new Document(); const doc = new Document();
const table = doc.createTable(4, 4); const table = doc.createTable({
rows: 4,
columns: 4,
});
table table
.getCell(2, 2) .getCell(2, 2)
.addContent(new Paragraph("Hello")) .addParagraph(new Paragraph("Hello"))
.CellProperties.Borders.addTopBorder(BorderStyle.DASH_DOT_STROKED, 3, "red") .Borders.addTopBorder(BorderStyle.DASH_DOT_STROKED, 3, "red")
.addBottomBorder(BorderStyle.DOUBLE, 3, "blue") .addBottomBorder(BorderStyle.DOUBLE, 3, "blue")
.addStartBorder(BorderStyle.DOT_DOT_DASH, 3, "green") .addStartBorder(BorderStyle.DOT_DOT_DASH, 3, "green")
.addEndBorder(BorderStyle.DOT_DOT_DASH, 3, "#ff8000"); .addEndBorder(BorderStyle.DOT_DOT_DASH, 3, "#ff8000");

View File

@ -16,7 +16,7 @@ paragraph2.addRun(textRun2);
doc.addParagraph(paragraph2); doc.addParagraph(paragraph2);
const paragraph3 = new Paragraph().bidirectional(); const paragraph3 = new Paragraph().bidirectional();
const textRun3 = new TextRun("שלום עולם").italic().rightToLeft(); const textRun3 = new TextRun("שלום עולם").italics().rightToLeft();
paragraph3.addRun(textRun3); paragraph3.addRun(textRun3);
doc.addParagraph(paragraph3); doc.addParagraph(paragraph3);

View File

@ -5,11 +5,14 @@ import { Document, Media, Packer, Paragraph } from "../build";
const doc = new Document(); const doc = new Document();
const table = doc.createTable(4, 4); const table = doc.createTable({
table.getCell(2, 2).addContent(new Paragraph("Hello")); rows: 4,
columns: 4,
});
table.getCell(2, 2).addParagraph(new Paragraph("Hello"));
const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg")); const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg"));
table.getCell(1, 1).addContent(image.Paragraph); table.getCell(1, 1).addParagraph(image.Paragraph);
const packer = new Packer(); const packer = new Packer();

View File

@ -1,3 +1,5 @@
// Custom styles using JavaScript configuration
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs"; import * as fs from "fs";
import { Document, Packer } from "../build"; import { Document, Packer } from "../build";

View File

@ -1,4 +1,4 @@
// Creates two paragraphs, one with a border and one without // Table of contents
// Import from 'docx' rather than '../build' if you install from npm // Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs"; import * as fs from "fs";
import { File, Packer, Paragraph, StyleLevel, TableOfContents } from "../build"; import { File, Packer, Paragraph, StyleLevel, TableOfContents } from "../build";

View File

@ -1,3 +1,5 @@
// Numbered lists
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs"; import * as fs from "fs";
import { Document, Indent, Numbering, Packer, Paragraph } from "../build"; import { Document, Indent, Numbering, Packer, Paragraph } from "../build";
@ -15,10 +17,22 @@ const item2 = new Paragraph("line with contextual spacing");
const item3 = new Paragraph("line without contextual spacing"); const item3 = new Paragraph("line without contextual spacing");
const item4 = new Paragraph("line without contextual spacing"); const item4 = new Paragraph("line without contextual spacing");
item1.setNumbering(concrete, 0).spacing({before: 200}).contextualSpacing(true); item1
item2.setNumbering(concrete, 0).spacing({before: 200}).contextualSpacing(true); .setNumbering(concrete, 0)
item3.setNumbering(concrete, 0).spacing({before: 200}).contextualSpacing(false); .spacing({ before: 200 })
item4.setNumbering(concrete, 0).spacing({before: 200}).contextualSpacing(false); .contextualSpacing(true);
item2
.setNumbering(concrete, 0)
.spacing({ before: 200 })
.contextualSpacing(true);
item3
.setNumbering(concrete, 0)
.spacing({ before: 200 })
.contextualSpacing(false);
item4
.setNumbering(concrete, 0)
.spacing({ before: 200 })
.contextualSpacing(false);
doc.addParagraph(item1); doc.addParagraph(item1);
doc.addParagraph(item2); doc.addParagraph(item2);

View File

@ -1,3 +1,5 @@
// Example on how to use a template document
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs"; import * as fs from "fs";
import { Document, ImportDotx, Packer, Paragraph } from "../build"; import { Document, ImportDotx, Packer, Paragraph } from "../build";

View File

@ -5,15 +5,18 @@ import { Document, Packer, Paragraph, VerticalAlign } from "../build";
const doc = new Document(); const doc = new Document();
const table = doc.createTable(2, 2); const table = doc.createTable({
rows: 2,
columns: 2,
});
table table
.getCell(1, 1) .getCell(1, 1)
.addContent(new Paragraph("This text should be in the middle of the cell")) .addParagraph(new Paragraph("This text should be in the middle of the cell"))
.CellProperties.setVerticalAlign(VerticalAlign.CENTER); .setVerticalAlign(VerticalAlign.CENTER);
table table
.getCell(1, 0) .getCell(1, 0)
.addContent( .addParagraph(
new Paragraph( new Paragraph(
"Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah", "Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah",
).heading1(), ).heading1(),

View File

@ -1,33 +1,99 @@
// Example of how you would create a table and add data to it // Example of how you would merge cells together
// Import from 'docx' rather than '../build' if you install from npm // Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs"; import * as fs from "fs";
import { Document, Packer, Paragraph } from "../build"; import { Document, Packer, Paragraph, ShadingType, WidthType } from "../build";
const doc = new Document(); const doc = new Document();
let table = doc.createTable(2, 2); let table = doc.createTable({
rows: 2,
columns: 2,
});
table.getCell(0, 0).addContent(new Paragraph("Hello")); table.getCell(0, 0).addParagraph(new Paragraph("Hello"));
table.getRow(0).mergeCells(0, 1); table.getRow(0).mergeCells(0, 1);
doc.createParagraph("Another table").heading2(); doc.createParagraph("Another table").heading2();
table = doc.createTable(2, 3); table = doc.createTable({
table.getCell(0, 0).addContent(new Paragraph("World")); rows: 2,
columns: 3,
width: 100,
widthUnitType: WidthType.AUTO,
columnWidths: [1000, 1000, 1000],
});
table
.getCell(0, 0)
.addParagraph(new Paragraph("World"))
.setMargins({
top: 1000,
bottom: 1000,
left: 1000,
right: 1000,
});
table.getRow(0).mergeCells(0, 2); table.getRow(0).mergeCells(0, 2);
doc.createParagraph("Another table").heading2(); doc.createParagraph("Another table").heading2();
table = doc.createTable(2, 4); table = doc.createTable({
table.getCell(0, 0).addContent(new Paragraph("Foo")); rows: 2,
columns: 4,
width: 7000,
widthUnitType: WidthType.DXA,
margins: {
top: 400,
bottom: 400,
right: 400,
left: 400,
},
});
table.getCell(0, 0).addParagraph(new Paragraph("Foo"));
table.getCell(0, 1).addParagraph(new Paragraph("v"));
table.getCell(1, 0).addContent(new Paragraph("Bar1")); table
table.getCell(1, 1).addContent(new Paragraph("Bar2")); .getCell(1, 0)
table.getCell(1, 2).addContent(new Paragraph("Bar3")); .addParagraph(new Paragraph("Bar1"))
table.getCell(1, 3).addContent(new Paragraph("Bar4")); .setShading({
fill: "b79c2f",
val: ShadingType.REVERSE_DIAGONAL_STRIPE,
color: "auto",
});
table
.getCell(1, 1)
.addParagraph(new Paragraph("Bar2"))
.setShading({
fill: "42c5f4",
val: ShadingType.PERCENT_95,
color: "auto",
});
table
.getCell(1, 2)
.addParagraph(new Paragraph("Bar3"))
.setShading({
fill: "880aa8",
val: ShadingType.PERCENT_10,
color: "e2df0b",
});
table
.getCell(1, 3)
.addParagraph(new Paragraph("Bar4"))
.setShading({
fill: "FF0000",
val: ShadingType.CLEAR,
color: "auto",
});
table.getRow(0).mergeCells(0, 3); table.getRow(0).mergeCells(0, 3);
doc.createParagraph("hi");
doc.createTable({
rows: 2,
columns: 2,
width: 100,
widthUnitType: WidthType.PERCENTAGE,
});
const packer = new Packer(); const packer = new Packer();
packer.toBuffer(doc).then((buffer) => { packer.toBuffer(doc).then((buffer) => {

View File

@ -1,28 +1,25 @@
// Example of how you would create a table with float positions // Example of how you would create a table with float positions
// Import from 'docx' rather than '../build' if you install from npm // Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs"; import * as fs from "fs";
import { import { Document, Packer, Paragraph, RelativeHorizontalPosition, RelativeVerticalPosition, TableAnchorType, WidthType } from "../build";
Document,
Packer,
Paragraph,
RelativeHorizontalPosition,
RelativeVerticalPosition,
TableAnchorType,
WidthType,
} from "../build";
const doc = new Document(); const doc = new Document();
const table = doc.createTable(2, 2).float({ const table = doc.createTable({
rows: 2,
columns: 2,
float: {
horizontalAnchor: TableAnchorType.MARGIN, horizontalAnchor: TableAnchorType.MARGIN,
verticalAnchor: TableAnchorType.MARGIN, verticalAnchor: TableAnchorType.MARGIN,
relativeHorizontalPosition: RelativeHorizontalPosition.RIGHT, relativeHorizontalPosition: RelativeHorizontalPosition.RIGHT,
relativeVerticalPosition: RelativeVerticalPosition.BOTTOM, relativeVerticalPosition: RelativeVerticalPosition.BOTTOM,
},
width: 4535,
widthUnitType: WidthType.DXA,
}); });
table.setFixedWidthLayout(); table.setFixedWidthLayout();
table.setWidth(WidthType.DXA, 4535);
table.getCell(0, 0).addContent(new Paragraph("Hello")); table.getCell(0, 0).addParagraph(new Paragraph("Hello"));
table.getRow(0).mergeCells(0, 1); table.getRow(0).mergeCells(0, 1);
const packer = new Packer(); const packer = new Packer();

18
demo/demo35.ts Normal file
View File

@ -0,0 +1,18 @@
// Example on how to add hyperlinks to websites
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
import { Document, Packer, Paragraph } from "../build";
const doc = new Document();
const paragraph = new Paragraph();
const link = doc.createHyperlink("http://www.example.com", "Hyperlink");
link.bold();
paragraph.addHyperLink(link);
doc.addParagraph(paragraph);
const packer = new Packer();
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});

26
demo/demo36.ts Normal file
View File

@ -0,0 +1,26 @@
// Add image to table cell
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
import { Document, Media, Packer, Table } from "../build";
const doc = new Document();
const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg"));
const table = new Table({
rows: 2,
columns: 2,
});
table.getCell(1, 1).addParagraph(image.Paragraph);
// doc.createParagraph("Hello World");
doc.addTable(table);
// doc.Header.createImage(fs.readFileSync("./demo/images/pizza.gif"));
doc.Header.addTable(table);
// doc.Footer.createImage(fs.readFileSync("./demo/images/pizza.gif"));
const packer = new Packer();
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});

18
demo/demo37.ts Normal file
View File

@ -0,0 +1,18 @@
// Add images to header and footer
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
import { Document, Media, Packer } from "../build";
const doc = new Document();
const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg"));
doc.createParagraph("Hello World");
doc.Header.addImage(image);
doc.Header.createImage(fs.readFileSync("./demo/images/pizza.gif"));
doc.Header.createImage(fs.readFileSync("./demo/images/image1.jpeg"));
const packer = new Packer();
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});

44
demo/demo38.ts Normal file
View File

@ -0,0 +1,44 @@
// Example of how to "wrap" text around an image
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
// import { Document, Packer, Paragraph } from "../build";
import { Document, Packer, TextWrappingSide, TextWrappingType } from "../build";
const doc = new Document();
doc.createParagraph(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vehicula nec nulla vitae efficitur. Ut interdum mauris eu ipsum rhoncus, nec pharetra velit placerat. Sed vehicula libero ac urna molestie, id pharetra est pellentesque. Praesent iaculis vehicula fringilla. Duis pretium gravida orci eu vestibulum. Mauris tincidunt ipsum dolor, ut ornare dolor pellentesque id. Integer in nulla gravida, lacinia ante non, commodo ex. Vivamus vulputate nisl id lectus finibus vulputate. Ut et nisl mi. Cras fermentum augue arcu, ac accumsan elit euismod id. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed ac posuere nisi. Pellentesque tincidunt vehicula bibendum. Phasellus eleifend viverra nisl.",
);
doc.createParagraph(
"Proin ac purus faucibus, porttitor magna ut, cursus nisl. Vivamus ante purus, porta accumsan nibh eget, eleifend dignissim odio. Integer sed dictum est, aliquam lacinia justo. Donec ultrices auctor venenatis. Etiam interdum et elit nec elementum. Pellentesque nec viverra mauris. Etiam suscipit leo nec velit fringilla mattis. Pellentesque justo lacus, sodales eu condimentum in, dapibus finibus lacus. Morbi vitae nibh sit amet sem molestie feugiat. In non porttitor enim.",
);
doc.createParagraph(
"Ut eget diam cursus quam accumsan interdum at id ante. Ut mollis mollis arcu, eu scelerisque dui tempus in. Quisque aliquam, augue quis ornare aliquam, ex purus ultrices mauris, ut porta dolor dolor nec justo. Nunc a tempus odio, eu viverra arcu. Suspendisse vitae nibh nec mi pharetra tempus. Mauris ut ullamcorper sapien, et sagittis sapien. Vestibulum in urna metus. In scelerisque, massa id bibendum tempus, quam orci rutrum turpis, a feugiat nisi ligula id metus. Praesent id dictum purus. Proin interdum ipsum nulla.",
);
doc.createImage(fs.readFileSync("./demo/images/pizza.gif"), 200, 200, {
floating: {
horizontalPosition: {
offset: 2014400,
},
verticalPosition: {
offset: 2014400,
},
wrap: {
type: TextWrappingType.SQUARE,
side: TextWrappingSide.BOTH_SIDES,
},
margins: {
top: 201440,
bottom: 201440,
},
},
});
const packer = new Packer();
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});

33
demo/demo39.ts Normal file
View File

@ -0,0 +1,33 @@
// Example how to display page numbers
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
import { Document, Packer, PageNumberFormat, TextRun } from "../build";
const doc = new Document(
{},
{
pageNumberStart: 1,
pageNumberFormatType: PageNumberFormat.DECIMAL,
},
);
doc.Header.createParagraph("Foo Bar corp. ")
.addRun(new TextRun("Page Number ").pageNumber())
.addRun(new TextRun(" to ").numberOfTotalPages());
doc.Footer.createParagraph("Foo Bar corp. ")
.center()
.addRun(new TextRun("Page Number: ").pageNumber())
.addRun(new TextRun(" to ").numberOfTotalPages());
doc.createParagraph("Hello World 1").pageBreak();
doc.createParagraph("Hello World 2").pageBreak();
doc.createParagraph("Hello World 3").pageBreak();
doc.createParagraph("Hello World 4").pageBreak();
doc.createParagraph("Hello World 5").pageBreak();
const packer = new Packer();
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});

View File

@ -5,8 +5,11 @@ import { Document, Packer, Paragraph } from "../build";
const doc = new Document(); const doc = new Document();
const table = doc.createTable(4, 4); const table = doc.createTable({
table.getCell(2, 2).addContent(new Paragraph("Hello")); rows: 4,
columns: 4,
});
table.getCell(2, 2).addParagraph(new Paragraph("Hello"));
const packer = new Packer(); const packer = new Packer();

26
demo/demo40.ts Normal file
View File

@ -0,0 +1,26 @@
// Example demonstrating line numbers.
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
import { Document, LineNumberRestartFormat, Packer, Paragraph } from "../build";
const doc = new Document(
{},
{
lineNumberCountBy: 1,
lineNumberRestart: LineNumberRestartFormat.CONTINUOUS,
},
);
doc.addParagraph(new Paragraph("Hello").heading1());
doc.createParagraph(
"Himenaeos duis luctus nullam fermentum lobortis potenti vivamus non dis, sed facilisis ultricies scelerisque aenean risus hac senectus. Adipiscing id venenatis justo ante gravida placerat, ac curabitur dis pellentesque proin bibendum risus, aliquam porta taciti vulputate primis. Tortor ipsum fermentum quam vel convallis primis nisl praesent tincidunt, lobortis quisque felis vitae condimentum class ut sem nam, aenean potenti pretium ac amet lacinia himenaeos mi. Aliquam nisl turpis hendrerit est morbi malesuada, augue interdum mus inceptos curabitur tristique, parturient feugiat sodales nulla facilisi. Aliquam non pulvinar purus nulla ex integer, velit faucibus vitae at bibendum quam, risus elit aenean adipiscing posuere.",
);
doc.createParagraph(
"Sed laoreet id mattis egestas nam mollis elit lacinia convallis dui tincidunt ultricies habitant, pharetra per maximus interdum neque tempor risus efficitur morbi imperdiet senectus. Lectus laoreet senectus finibus inceptos donec potenti fermentum, ultrices eleifend odio suscipit magnis tellus maximus nibh, ac sit nullam eget felis himenaeos. Diam class sem magnis aenean commodo faucibus id proin mi, nullam sodales nec mus parturient ornare ad inceptos velit hendrerit, bibendum placerat eleifend integer facilisis urna dictumst suspendisse.",
);
const packer = new Packer();
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});

51
demo/demo41.ts Normal file
View File

@ -0,0 +1,51 @@
// Multiple cells merging in the same table
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
import { Document, Packer, Paragraph } from "../build";
const doc = new Document();
const table = doc.createTable({
rows: 13,
columns: 6,
});
let row = 0;
table.getCell(row, 0).addContent(new Paragraph("0,0"));
table.getCell(row, 1).addContent(new Paragraph("0,1"));
table.getCell(row, 3).addContent(new Paragraph("0,3"));
table.getCell(row, 4).addContent(new Paragraph("0,4"));
table.getRow(row).mergeCells(4, 5);
table.getRow(row).mergeCells(1, 2);
row = 1;
table.getCell(row, 0).addContent(new Paragraph("1,0"));
table.getCell(row, 2).addContent(new Paragraph("1,2"));
table.getCell(row, 4).addContent(new Paragraph("1,4"));
table.getRow(row).mergeCells(4, 5);
table.getRow(row).mergeCells(2, 3);
table.getRow(row).mergeCells(0, 1);
row = 2;
table.getCell(row, 0).addContent(new Paragraph("2,0"));
table.getCell(row, 1).addContent(new Paragraph("2,1"));
table.getCell(row, 2).addContent(new Paragraph("2,2"));
table.getCell(row, 3).addContent(new Paragraph("2,3"));
table.getCell(row, 4).addContent(new Paragraph("2,4"));
table.getRow(row).mergeCells(4, 5);
table.getRow(row).mergeCells(1, 2);
row = 3;
table.getCell(row, 0).addContent(new Paragraph("3,0"));
table.getCell(row, 1).addContent(new Paragraph("3,1"));
table.getCell(row, 2).addContent(new Paragraph("3,2"));
table.getCell(row, 3).addContent(new Paragraph("3,3"));
table.getCell(row, 4).addContent(new Paragraph("3,4"));
table.getCell(row, 5).addContent(new Paragraph("3,5"));
row = 4;
table.getCell(row, 0).addContent(new Paragraph("4,0"));
table.getCell(row, 5).addContent(new Paragraph("4,5"));
table.getRow(row).mergeCells(0, 4);
const packer = new Packer();
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});

20
demo/demo43.ts Normal file
View File

@ -0,0 +1,20 @@
// Add image to table cell
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
import { Document, Packer, Paragraph } from "../build";
const doc = new Document();
const table = doc.createTable({
rows: 4,
columns: 4,
});
table.getCell(2, 2).addParagraph(new Paragraph("Hello"));
table.getColumn(3).mergeCells(1, 2);
// table.getCell(3, 2).addParagraph(new Paragraph("Hello"));
const packer = new Packer();
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});

36
demo/demo44.ts Normal file
View File

@ -0,0 +1,36 @@
// Sections with multiple columns
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
import { Document, Packer } from "../build";
const doc = new Document();
doc.addSection({
column: {
width: 708,
count: 2,
},
});
doc.createParagraph("This text will be split into 2 columns on a page.");
doc.createParagraph(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
);
doc.addSection({
column: {
width: 708,
count: 2,
},
});
doc.createParagraph("This text will be split into 3 columns on a page.");
doc.createParagraph(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
);
const packer = new Packer();
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});

View File

@ -29,6 +29,7 @@ import * as docx from "docx";
## Basic Usage ## Basic Usage
```js ```js
var fs = require("fs");
var docx = require("docx"); var docx = require("docx");
// Create document // Create document
@ -41,11 +42,12 @@ paragraph.addRun(new docx.TextRun("Lorem Ipsum Foo Bar"));
doc.addParagraph(paragraph); doc.addParagraph(paragraph);
// Used to export the file into a .docx file // Used to export the file into a .docx file
var exporter = new docx.LocalPacker(doc); var packer = new docx.Packer();
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My First Document.docx", buffer);
});
exporter.pack("My First Document"); // Done! A file called 'My First Document.docx' will be in your file system.
// Done! A file called 'My First Document.docx' will be in your file system if you used LocalPacker
``` ```
## Honoured Mentions ## Honoured Mentions

View File

@ -15,8 +15,10 @@
* [Headers & Footers](usage/headers-and-footers.md) * [Headers & Footers](usage/headers-and-footers.md)
* [Bullet Points](usage/bullet-points.md) * [Bullet Points](usage/bullet-points.md)
* [Numbering](usage/numbering.md) * [Numbering](usage/numbering.md)
* [Tables](usage/tables.md)
* [Tab Stops](usage/tab-stops.md) * [Tab Stops](usage/tab-stops.md)
* [Table of Contents](usage/table-of-contents.md) * [Table of Contents](usage/table-of-contents.md)
* [Page Numbers](usage/page-numbers.md)
* Styling * Styling
* [Styling with JS](usage/styling-with-js.md) * [Styling with JS](usage/styling-with-js.md)
* [Styling with XML](usage/styling-with-xml.md) * [Styling with XML](usage/styling-with-xml.md)

View File

@ -12,13 +12,13 @@
## Always think about the user ## Always think about the user
The number one pillar for contribution is to **ALWAYS** think about how the user will use the library. The number one pillar for contribution to `docx` is to **ALWAYS** think about how the user will use `docx`.
Put yourself in their position, and imagine how they would feel about your feature you wrote. Put yourself in their position, and imagine how they would feel about your feature you wrote.
1. Is it easy to use? 1. Is it easy to use?
2. Has it been documented well? 2. Has it been documented well?
3. Is it intuative? 3. Is it intuitive?
4. Is it consistent with the rest of the API? 4. Is it consistent with the rest of the API?
5. Is it fun to use? 5. Is it fun to use?
@ -37,13 +37,13 @@ Unesesary coment removed // Make sure to use correct spelling
## No leaky components in API interface ## No leaky components in API interface
This mainly applies to the API the end user will consume. > This mainly applies to the API the end user will consume.
Try to make method parameters accept primatives, or `json` objects, so that child components are created **inside** the component, rather than being **injected** in. Try to make method parameters of the outside API accept primitives, or `json` objects, so that child components are created **inside** the component, rather than being **injected** in.
This is so that: This is so that:
1. Imports are much cleaner, no need for: 1. Imports are much cleaner for the end user, no need for:
```js ```js
import { ChildComponent } from "./my-feature/sub-component/deeper/.../my-deep.component"; import { ChildComponent } from "./my-feature/sub-component/deeper/.../my-deep.component";
``` ```
@ -52,17 +52,29 @@ This is so that:
3. It means the end user does not need to import and create the child component to be injected. 3. It means the end user does not need to import and create the child component to be injected.
**Do not** **Do not**
`TableFloatProperties` is a class. The outside world would have to construct the object, and inject it in
`TableFloatProperties` is a class. The outside world would have to `new` up the object, and inject it in like so:
```js ```js
public float(tableFloatProperties: TableFloatProperties): Table public float(tableFloatProperties: TableFloatProperties): Table
``` ```
```ts
table.float(new TableFloatProperties(...));
```
**Do** **Do**
`ITableFloatOptions` is an interface for a JSON of primatives.
`ITableFloatOptions` is an interface for a JSON of primitives. The end user would need to pass in a json object and not need to worry about the internals:
```js ```js
public float(tableFloatOptions: ITableFloatOptions): Table public float(tableFloatOptions: ITableFloatOptions): Table
``` ```
```ts
table.float({...});
```
## Add vs Create ## Add vs Create
This is just a guideline, and the rules can sometimes be broken. This is just a guideline, and the rules can sometimes be broken.
@ -76,7 +88,8 @@ This is just a guideline, and the rules can sometimes be broken.
} }
``` ```
* Use `add` if you add the element into the method as a parameter: * Use `add` if you add the element into the method as a parameter.
*Note:* This may look like its breaking the previous guideline, but it has semantically different meanings. The previous one is using data to construct an object, whereas this one is simply adding elements into the document:
```js ```js
public addParagraph(paragraph: Paragraph) { public addParagraph(paragraph: Paragraph) {
@ -108,6 +121,30 @@ private get _level: string;
private get level: string; private get level: string;
``` ```
## Temporal Methods
Some methods are `non-temporal`, which means regardless of when you call the method, it will have the same affect on the document. For example, setting the width of a table at the end of the document will have the same effect as setting the width at the start:
```ts
table.setWidth(1000); // now removed as of version 5.0.0
```
Whereas some methods are `temporal`, which means depending on the time-frame they are called, it would produce a difference result. For example, moving `createParagraph()` around your code will physically alter the document.
```ts
doc.createParagraph("hello");
```
If a method is `non-temporal`, put it in the objects `constructor`. For example:
```ts
const table = new Table(width: number);
```
`Non-temporal` methods are usually methods which can only be used one time and one time only. For example, `.float()`. It does not make sense to call `.float()` again if its already floating.
I am not sure what the real term is, but this will do.
## Interfaces over type alias ## Interfaces over type alias
Do not use `type`, but rather use `Interfaces`. `type` cannot be extended, and a class cannot implement it. Do not use `type`, but rather use `Interfaces`. `type` cannot be extended, and a class cannot implement it.
@ -154,7 +191,7 @@ enum WeaponType = {
} }
``` ```
## Spell correctly, full and in American English ## Spell correctly, in full and in American English
I am not sure where these habits in software development come from, but I do not believe it is beneficial: I am not sure where these habits in software development come from, but I do not believe it is beneficial:

View File

@ -14,7 +14,7 @@ This command will run the `demo selector app` in the `/demo` folder. It will pro
A simple hello world of the `docx` library: A simple hello world of the `docx` library:
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo1.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo1.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo1.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo1.ts_
@ -24,7 +24,7 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo1.ts_
This example shows how to customise the look and feel of a document using JS configuration This example shows how to customise the look and feel of a document using JS configuration
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo2.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo2.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo2.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo2.ts_
@ -32,7 +32,7 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo2.ts_
This example shows how to customise the look and feel of a document using XML configuration This example shows how to customise the look and feel of a document using XML configuration
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo13.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo13.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo13.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo13.ts_
@ -40,7 +40,7 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo13.ts_
This example shows many levels of numbering This example shows many levels of numbering
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo3.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo3.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo3.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo3.ts_
@ -48,7 +48,7 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo3.ts_
Example of simple table Example of simple table
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo4.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo4.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo4.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo4.ts_
@ -56,7 +56,7 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo4.ts_
Styling the borders of a table Styling the borders of a table
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo20.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo20.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo20.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo20.ts_
@ -66,7 +66,7 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo20.ts_
Importing Images from file system path Importing Images from file system path
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo5.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo5.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo5.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo5.ts_
@ -74,7 +74,7 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo5.ts_
Example showing how to add image to headers and footers Example showing how to add image to headers and footers
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo9.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo9.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo9.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo9.ts_
@ -82,7 +82,7 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo9.ts_
Example showing how to scale images Example showing how to scale images
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo12.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo12.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo12.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo12.ts_
@ -90,7 +90,7 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo12.ts_
This is the best way to add an image to a document because you can add the same image in two locations without increasing document size by re-using the same image This is the best way to add an image to a document because you can add the same image in two locations without increasing document size by re-using the same image
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo23.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo23.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo23.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo23.ts_
@ -98,7 +98,7 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo23.ts_
As before, to add an image to a table, you would need to add it to the `Media` object first As before, to add an image to a table, you would need to add it to the `Media` object first
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo24.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo24.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo24.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo24.ts_
@ -106,15 +106,15 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo24.ts_
If you want to use a Base64 image instead If you want to use a Base64 image instead
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo18.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo18.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo18.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo18.ts_
## Margins ## Margins
Example showing how to set custom margains Example showing how to set custom margins
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo6.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo6.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo6.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo6.ts_
@ -122,7 +122,7 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo6.ts_
Example showing how to set the document to `landscape` or `portrait` Example showing how to set the document to `landscape` or `portrait`
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo7.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo7.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo7.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo7.ts_
@ -130,7 +130,7 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo7.ts_
Example showing how to add headers and footers Example showing how to add headers and footers
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo8.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo8.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo8.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo8.ts_
@ -144,7 +144,7 @@ Check out `Sections` for this feature
Example showing how to page break Example showing how to page break
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo14.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo14.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo14.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo14.ts_
@ -152,15 +152,16 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo14.ts_
Example showing how to page break before like in Word Example showing how to page break before like in Word
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo15.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo15.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo15.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo15.ts_
## Sections ## Sections
Example of how sections work. Sections allow multiple headers and footers, and `landscape`/`portrait` inside the same document Example of how sections work. Sections allow multiple headers and footers, and `landscape`/`portrait` inside the same document.
Also you can have different page number formats and starts for different sections.
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo16.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo16.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo16.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo16.ts_
@ -168,7 +169,7 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo16.ts_
Example of how to add footnotes. Good for references Example of how to add footnotes. Good for references
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo17.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo17.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo17.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo17.ts_
@ -178,7 +179,7 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo17.ts_
Example showing how to use the Buffer packer and then write that buffer to the file system Example showing how to use the Buffer packer and then write that buffer to the file system
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo19.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo19.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo19.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo19.ts_
@ -187,7 +188,7 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo19.ts_
Example showing how to make bookmarks to make internal hyperlinks within the document Example showing how to make bookmarks to make internal hyperlinks within the document
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo21.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo21.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo21.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo21.ts_
@ -195,7 +196,7 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo21.ts_
Example showing how to use bidirectional text for certain languages such as Hebrew Example showing how to use bidirectional text for certain languages such as Hebrew
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo22.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo22.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo22.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo22.ts_
@ -205,7 +206,7 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo22.ts_
Example showing how to add headers and footers Example showing how to add headers and footers
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo10.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo10.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo10.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo10.ts_
@ -213,6 +214,6 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo10.ts_
This example shows how to customise the look and feel of a document and add images This example shows how to customise the look and feel of a document and add images
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo11.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo11.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo11.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo11.ts_

View File

@ -25,6 +25,7 @@
<script src="//unpkg.com/docsify/lib/plugins/emoji.min.js"></script> <script src="//unpkg.com/docsify/lib/plugins/emoji.min.js"></script>
<script src="https://unpkg.com/docsify-copy-code@2"></script> <script src="https://unpkg.com/docsify-copy-code@2"></script>
<script src="//unpkg.com/docsify/lib/plugins/search.min.js"></script> <script src="//unpkg.com/docsify/lib/plugins/search.min.js"></script>
<script src="//unpkg.com/prismjs/components/prism-typescript.min.js"></script>
</body> </body>
</html> </html>

View File

@ -5,7 +5,7 @@
To create a new document, it is very easy: To create a new document, it is very easy:
```js ```js
var doc = new docx.Document(); const doc = new docx.Document();
``` ```
## Document properties ## Document properties
@ -13,7 +13,7 @@ var doc = new docx.Document();
You can add properties to the Word document by specifying options, for example: You can add properties to the Word document by specifying options, for example:
```js ```js
var doc = new docx.Document({ const doc = new docx.Document({
creator: "Dolan Miu", creator: "Dolan Miu",
description: "My extremely interesting document", description: "My extremely interesting document",
title: "My Document", title: "My Document",
@ -22,14 +22,18 @@ var doc = new docx.Document({
### Full list of options: ### Full list of options:
```
creator * creator
description * description
title * title
subject * subject
keywords * keywords
lastModifiedBy * lastModifiedBy
revision * revision
```
You can mix and match whatever properties you want, or provide no properties. You can mix and match whatever properties you want, or provide no properties.
### units for positioning
Various parts of the API require positioning arguments. The units are "20ths of a point" from the [OOXML](http://officeopenxml.com/index.php) specification.
See [Lars Corneliussen's blog post](https://startbigthinksmall.wordpress.com/2010/01/04/points-inches-and-emus-measuring-units-in-office-open-xml/) for more information and how to convert units.

View File

@ -23,7 +23,7 @@ doc.Header.createImage([BUFFER_OF_YOUR_IMAGE]);
doc.Footer.createImage([BUFFER_OF_YOUR_IMAGE]); doc.Footer.createImage([BUFFER_OF_YOUR_IMAGE]);
``` ```
Refer to `demo8.js` for more information Refer to [`demo8.ts`](https://github.com/dolanmiu/docx/blob/master/demo/demo8.ts) for more information.
## Multiple Headers and Footers ## Multiple Headers and Footers
@ -37,8 +37,12 @@ Also all the supported section properties are implemented according to: http://o
// Add new section with another header and footer // Add new section with another header and footer
doc.addSection({ doc.addSection({
headerId: header.Header.ReferenceId, headers: {
footerId: footer.Footer.ReferenceId, default: header
},
footers: {
default: footer
},
pageNumberStart: 1, pageNumberStart: 1,
pageNumberFormatType: docx.PageNumberFormat.DECIMAL, pageNumberFormatType: docx.PageNumberFormat.DECIMAL,
}); });

47
docs/usage/hyperlinks.md Normal file
View File

@ -0,0 +1,47 @@
# Hyperlinks
There are two types of hyperlinks: internal (pointing to a bookmark inside the document) and external (pointing to an external url).
## Internal
To create an internal hyperlink you need first to create a bookmark (the paragraph that will be the destination of the hyperlink) with `doc.createBookmark(anchor, text)`.
A bookmark is composed of an anchor (an identifier) and the text displayed. After creating a bookmark just add it to a paragraph with `paragraph.addBookmark(bookmark)`
For example:
```ts
const paragraph = this.doc.createParagraph();
const bookmark = this.doc.createBookmark('anchorForChapter1', 'This is chapter1');
paragraph.addBookmark(bookmark);
```
Then you can create an hyperlink pointing to that bookmark with `doc.createInternalHyperLink(anchor,text)`:
```ts
const paragraph = this.doc.createParagraph();
const link = this.doc.createInternalHyperLink('anchorForChapter1', 'This is a link to chapter1');
paragraph.addHyperLink(link);
```
## External
To create an external hyperlink you just need to specify the url and the text of the link, then add it to a paragraph with `doc.createHyperlink(url, text)`:
```ts
const paragraph = this.doc.createParagraph();
const link = this.doc.createHyperlink('https://docx.js.org', 'This is an external link');
paragraph.addHyperLink(link);
```
## Styling an hyperlink
It is possible to set the style of the text of an hyperlink. This can be done applying run formatting on `TextRun` property of the hyperlink.
Example:
```ts
const link = this.doc.createHyperlink('https://docx.js.org', 'This is an external link');
link.TextRun.bold().italics()
```

View File

@ -56,7 +56,6 @@ Adding images can be done in two ways:
Three types of image positioning is supported: Three types of image positioning is supported:
- Floating - Floating
- Wrapped around the text
- Inline - Inline
By default, picture are exported as `Inline` elements. By default, picture are exported as `Inline` elements.
@ -65,7 +64,7 @@ By default, picture are exported as `Inline` elements.
Pass `options` into the `[POSITION_OPTIONS]` metioned in the [Intro above](#Intro). Pass `options` into the `[POSITION_OPTIONS]` metioned in the [Intro above](#Intro).
### Floating ## Floating
To change the position the image to be on top of the text, simply add the `floating` property to the last argument. By default, the offsets are relative to the top left corner of the `page`. Offset units are in [emus](https://startbigthinksmall.wordpress.com/2010/01/04/points-inches-and-emus-measuring-units-in-office-open-xml/): To change the position the image to be on top of the text, simply add the `floating` property to the last argument. By default, the offsets are relative to the top left corner of the `page`. Offset units are in [emus](https://startbigthinksmall.wordpress.com/2010/01/04/points-inches-and-emus-measuring-units-in-office-open-xml/):
@ -97,7 +96,7 @@ const imageData = document.createImage(buffer, 903, 1149, {
}); });
``` ```
#### Options ### Options
Full options you can pass into `floating` are: Full options you can pass into `floating` are:
@ -126,32 +125,83 @@ Full options you can pass into `floating` are:
| align | `VerticalPositionAlign` | You can either have `align` or `offset`, not both | `BOTTOM`, `CENTER`, `INSIDE`, `OUTSIDE`, `TOP` | | align | `VerticalPositionAlign` | You can either have `align` or `offset`, not both | `BOTTOM`, `CENTER`, `INSIDE`, `OUTSIDE`, `TOP` |
| offset | `number` | You can either have `align` or `offset`, not both | `0` to `Infinity` | | offset | `number` | You can either have `align` or `offset`, not both | `0` to `Infinity` |
### Wrap text ## Wrap text
!> **In progress** Documentation may potentially be changing Wrapping only works for floating elements. Text will "wrap" around the floating `image`.
for `drawingOptions.textWrapping` we can define various options. `textWrapping` has the following properties: Add `wrap` options inside the `floating` options:
```js ```ts
interface TextWrapping { wrap: {
textWrapStyle: TextWrapStyle; type: [TextWrappingType],
wrapTextOption?: WrapTextOption; side: [TextWrappingSide],
distanceFromText?: Distance; },
} ```
enum TextWrapStyle { For example:
NONE,
SQUARE,
TIGHT,
TOP_AND_BOTTOM,
}
enum WrapTextOption { ```ts
BOTH_SIDES = "bothSides", doc.createImage(fs.readFileSync("./demo/images/pizza.gif"), 200, 200, {
LEFT = "left", floating: {
RIGHT = "right", horizontalPosition: {
LARGEST = "largest", offset: 2014400,
} },
verticalPosition: {
offset: 2014400,
},
wrap: {
type: TextWrappingType.SQUARE,
side: TextWrappingSide.BOTH_SIDES,
},
},
});
```
Wrap options have the following properties are:
| Property | Type | Notes | Possible Values |
| -------- | ------------------ | -------- | ------------------------------------------- |
| type | `TextWrappingType` | Optional | `NONE`, `SQUARE`, `TIGHT`, `TOP_AND_BOTTOM` |
| side | `TextWrappingSide` | Optional | `BOTH_SIDES`, `LEFT`, `RIGHT`, `LARGEST` |
## Margins
Margins give some space between the text and the image. Margins [only work for floating elements](http://officeopenxml.com/drwPicInline.php). Additionally, the image must also be in wrap mode (see above).
?> Be sure to also set `wrap` in your options!
To use, add the `margins` options inside the `floating` options:
```ts
margins: {
top: number,
bottom: number,
left: number,
right: number
},
```
For example:
```ts
doc.createImage(fs.readFileSync("./demo/images/pizza.gif"), 200, 200, {
floating: {
horizontalPosition: {
offset: 2014400,
},
verticalPosition: {
offset: 2014400,
},
wrap: {
type: TextWrappingType.SQUARE,
side: TextWrappingSide.BOTH_SIDES,
},
margins: {
top: 201440,
bottom: 201440,
},
},
});
``` ```
## Examples ## Examples
@ -160,7 +210,7 @@ enum WrapTextOption {
Importing Images from file system path Importing Images from file system path
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo5.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo5.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo5.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo5.ts_
@ -168,6 +218,14 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo5.ts_
Example showing how to add image to headers and footers Example showing how to add image to headers and footers
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo9.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo9.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo9.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo9.ts_
### Floating images
Example showing how to float images on top of text and optimally give a `margin`
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo38.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo38.ts_

View File

@ -26,7 +26,7 @@ _levels_ which form a sequence starting at 0 indicating the top-level
list look and increasing from there to descibe the sublists, then list look and increasing from there to descibe the sublists, then
sub-sublists, etc. Each level includes the following properties: sub-sublists, etc. Each level includes the following properties:
* **level**: This its 0-based index in the defintion stack * **level**: This is its 0-based index in the definition stack
* **numberFormat**: This indicates how the bullet or number should be * **numberFormat**: This indicates how the bullet or number should be
generated. Options include `bullet` (meaning don't count), `decimal` generated. Options include `bullet` (meaning don't count), `decimal`
(arabic numerals), `upperRoman`, `lowerRoman`, `hex`, and many (arabic numerals), `upperRoman`, `lowerRoman`, `hex`, and many
@ -64,8 +64,8 @@ styles or numbering of any kind, you'll have to use the
`docx.Numbering` class. `docx.Numbering` class.
First you need to create a new numbering container class and use it to First you need to create a new numbering container class and use it to
create your abstract numbering style, define your levels, and creat create your abstract numbering style, define your levels, and create
your concreate numbering style: your concrete numbering style:
```js ```js
const numbering = new docx.Numbering(); const numbering = new docx.Numbering();
@ -78,8 +78,8 @@ abstractNum.createLevel(2, "lowerLetter", "%3)", "start").addParagraphProperty(n
const concrete = numbering.createConcreteNumbering(abstractNum); const concrete = numbering.createConcreteNumbering(abstractNum);
``` ```
You can then apply your concrete style to paragraphs using their You can then apply your concrete style to paragraphs using the
`#setNumbering` method: `setNumbering` method:
```js ```js
topLevelP.setNumbering(concrete, 0); topLevelP.setNumbering(concrete, 0);

View File

@ -0,0 +1,66 @@
# Page Numbers
> This feature allows you to set page numbers on each page
?> **Note:** This feature only works on Headers and Footers
```ts
doc.Header.createParagraph().addRun(new TextRun("Page Number: ").pageNumber()).addRun(new TextRun("to ").numberOfTotalPages());
```
## Current page number
To get the current page number, call the `.pageNumber()` method on a `TextRun`. Then add the newly created `TextRun` into a paragraph
```ts
pageNumber();
```
For example:
```ts
const currentPageRun = new TextRun("Current Page Number: ").pageNumber();
paragraph.addRun(currentPageRun);
```
## Total number of pages
```ts
numberOfTotalPages();
```
For example:
```ts
const lastPage = new TextRun("Total Page Number: ").numberOfTotalPages();
paragraph.addRun(lastPage);
```
## Both
You can combine the two to get "Page 2 of 10" effect:
```ts
const currentPageRun = new TextRun("Page ").pageNumber();
const lastPage = new TextRun("of ").numberOfTotalPages();
paragraph.addRun(currentPageRun);
paragraph.addRun(lastPage);
```
Or:
```ts
doc.Header.createParagraph().addRun(new TextRun("Page ").pageNumber()).addRun(new TextRun("of ").numberOfTotalPages());
```
## Examples
### Simple Example
Adding page numbers to Header and Footer
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo39.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo39.ts_

View File

@ -76,6 +76,22 @@ paragraph.heading1().center();
The above will create a `heading 1` which is `centered`. The above will create a `heading 1` which is `centered`.
### Justified text with breaks
When a paragraph is justified, you may want to not justify the contents of incomplete lines, which end in a soft line break.
![Justified line before](https://user-images.githubusercontent.com/7989576/53820338-e060c680-3f6b-11e9-817c-ecb43271951e.png)
This is possible to achieve using:
```ts
this.doc.Settings.addCompatibility().doNotExpandShiftReturn();
```
The result is:
![Justified line after](https://user-images.githubusercontent.com/7989576/53820344-e2c32080-3f6b-11e9-9afe-24a2ed6e0d78.png)
## Thematic Break ## Thematic Break
To add a break in the page, simply add `.thematicBreak()` on a paragraph: To add a break in the page, simply add `.thematicBreak()` on a paragraph:
@ -106,7 +122,7 @@ var paragraph = new docx.Paragraph("Hello World on another page").pageBreakBefor
![Page Break Before in Word](https://user-images.githubusercontent.com/34742290/40176503-df3a8398-59db-11e8-8b9c-d719f13aa8b4.png) ![Page Break Before in Word](https://user-images.githubusercontent.com/34742290/40176503-df3a8398-59db-11e8-8b9c-d719f13aa8b4.png)
Example: https://github.com/dolanmiu/docx/blob/master/demo/demo15.js Example: https://github.com/dolanmiu/docx/blob/master/demo/demo15.ts
## Page break control ## Page break control

View File

@ -14,7 +14,7 @@ const name = new TextRun("Name:")
## Available methods ## Available methods
* For run formatting: * For run formatting:
* `.bold()`, `.italic()`, `.smallCaps()`, `.allCaps()`, `.strike()`, `.doubleStrike()`, `.subScript()`, `.superScript()`: Set the formatting property to true * `.bold()`, `.italics()`, `.smallCaps()`, `.allCaps()`, `.strike()`, `.doubleStrike()`, `.subScript()`, `.superScript()`: Set the formatting property to true
* `.underline(style="single", color=null)`: Set the underline style and color * `.underline(style="single", color=null)`: Set the underline style and color
* `.color(color)`: Set the text color, using 6 hex characters for RRGGBB (no leading `#`) * `.color(color)`: Set the text color, using 6 hex characters for RRGGBB (no leading `#`)
* `.size(halfPts)`: Set the font size, measured in half-points * `.size(halfPts)`: Set the font size, measured in half-points

View File

@ -44,4 +44,4 @@ doc.addParagraph(paragraph);
doc.createParagraph("Some normal text"); doc.createParagraph("Some normal text");
``` ```
Example: https://github.com/dolanmiu/docx/blob/master/demo/demo13.js Example: https://github.com/dolanmiu/docx/blob/master/demo/demo13.ts

View File

@ -2,7 +2,7 @@
> Tab stops are useful, if you are unclear of what they are, [here is a link explaining](https://en.wikipedia.org/wiki/Tab_stop). It enables side by side text which is nicely laid out without the need for tables, or constantly pressing space bar. > Tab stops are useful, if you are unclear of what they are, [here is a link explaining](https://en.wikipedia.org/wiki/Tab_stop). It enables side by side text which is nicely laid out without the need for tables, or constantly pressing space bar.
**Note**: At the moment, the unit of measurement for a tab stop is counter intuitive for a human. It is using OpenXMLs own measuring system. For example, 2268 roughly translates to 3cm. Therefore in the future, I may consider changing it to percentages or even cm. !> **Note**: At the moment, the unit of measurement for a tab stop is counter intuitive for a human. It is using OpenXMLs own measuring system. For example, 2268 roughly translates to 3cm. Therefore in the future, I may consider changing it to percentages or even cm.
![Word 2013 Tabs](http://www.teachucomp.com/wp-content/uploads/blog-4-22-2015-UsingTabStopsInWord-1024x577.png "Word 2013 Tab Stops") ![Word 2013 Tabs](http://www.teachucomp.com/wp-content/uploads/blog-4-22-2015-UsingTabStopsInWord-1024x577.png "Word 2013 Tab Stops")

View File

@ -71,6 +71,6 @@ doc.addParagraph(new Paragraph("My Spectacular Style #1").style("MySpectacularSt
### Complete example ### Complete example
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo28.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo28.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo28.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo28.ts_

305
docs/usage/tables.md Normal file
View File

@ -0,0 +1,305 @@
# Tables
You can create tables with `docx`. More information can be found [here](http://officeopenxml.com/WPtable.php).
## Create Table
To create a table, simply use the `createTable()` method on a `document`.
```ts
const table = doc.createTable([NUMBER OF ROWS], [NUMBER OF COLUMNS]);
```
Alternatively, you can create a table object directly, and then add it in the `document`
```ts
const table = new Table(4, 4);
doc.addTable(table);
```
The snippet below creates a table of 2 rows and 4 columns.
```ts
const table = doc.createTable(2, 4);
// Or
const table = new Table(2, 4);
doc.addTable(table);
```
## Rows and Columns
You can get a row or a column from a table like so, where `index` is a number:
### Get Row
```ts
const row = doc.getRow(index);
```
With this, you can merge a row by using the `mergeCells()` method, where `startIndex` is the row number you want to merge from, and `endIndex` is where you want it to merge to:
```ts
row.mergeCells(startIndex, endIndex);
```
You can get a cell from a `row` by using the `getCell()` method, where `index` is the row index:
```ts
row.getCell(index);
```
### Get Column
```ts
const column = doc.getColumn(index);
```
Again, you can merge a row by using the `mergeCells()` method, where `startIndex` is the row number you want to merge from, and `endIndex` is where you want it to merge to:
```ts
column.mergeCells(startIndex, endIndex);
```
You can get a cell from a `column` by using the `getCell()` method, where `index` is the column index:
```ts
column.getCell(index);
```
## Cells
The `createTable()` method created a table with cells. To access the cell, use the `getCell()` method.
```ts
const cell = table.getCell([ROW INDEX], [COLUMN INDEX]);
```
You can also get a cell from a `column` or a `row` with `getCell()`, mentioned previously.
For example:
```ts
const cell = table.getCell(0, 2);
const cell = row.getCell(0);
const cell = column.getCell(2);
```
### Add paragraph to a cell
Once you have got the cell, you can add data to it with the `addParagraph()` method.
```ts
cell.addParagraph(new Paragraph("Hello"));
```
### Set width of a cell
You can specify the width of a cell using:
`cell.Properties.setWidth(width, format)`
format can be:
* WidthType.AUTO
* WidthType.DXA: value is in twentieths of a point
* WidthType.NIL: is considered as zero
* WidthType.PCT: percent of table width
### Example
```ts
cell.Properties.setWidth(100, WidthType.DXA);
```
```ts
cell.Properties.setWidth('50%', WidthType.PCT);
```
## Borders
BorderStyle can be imported from `docx`. Size determines the thickness. HTML color can be a hex code or alias such as `red`.
```ts
cell.Borders.addTopBorder([BorderStyle], [SIZE], [HTML COLOR]);
```
```ts
cell.Borders.addBottomBorder([BorderStyle], [SIZE], [HTML COLOR]);
```
```ts
cell.Borders.addStartBorder([[BorderStyle]], [SIZE], [HTML COLOR]);
```
```ts
cell.Borders.addEndBorder([BorderStyle], [SIZE], [HTML COLOR]);
```
### Example
```ts
import { BorderStyle } from "docx";
cell.Borders.addStartBorder(BorderStyle.DOT_DOT_DASH, 3, "green");
cell.Borders.addEndBorder(BorderStyle.DOT_DOT_DASH, 3, "#ff8000");
```
### Google DOCS
Google DOCS does not support start and end borders, instead they use left and right borders. So to set left and right borders for Google DOCS you should use:
```ts
import { BorderStyle } from "docx";
cell.Borders.addLeftBorder(BorderStyle.DOT_DOT_DASH, 3, "green");
cell.Borders.addRightBorder(BorderStyle.DOT_DOT_DASH, 3, "#ff8000");
```
## Set Width
```ts
import { WidthType } from "docx";
table.setWidth([WIDTH], [OPTIONAL WidthType. Defaults to DXA]);
```
For example:
```ts
table.setWidth(4535, WidthType.DXA);
```
## Vertical Align
Sets the vertical alignment of the contents of the cell
```ts
import { VerticalAlign } from "docx";
cell.setVerticalAlign([VerticalAlign TYPE]);
```
For example, to center align a cell:
```ts
cell.setVerticalAlign(VerticalAlign.CENTER);
```
## Rows
To get a row, use the `getRow` method on a `table`. There are a handful of methods which you can apply to a row which will be explained below.
```ts
table.getRow([ROW INDEX]);
```
## Merge cells together
### Merging on a row
First obtain the row, and call `mergeCells()`. The first argument is where the merge should start. The second argument is where the merge should end.
```ts
table.getRow(0).mergeCells([FROM INDEX], [TO INDEX]);
```
#### Example
This will merge 3 cells together starting from index `0`:
```ts
table.getRow(0).mergeCells(0, 2);
```
### Merging on a column
It has not been implemented yet, but it will follow a similar structure as merging a row.
## Nested Tables
To have a table within a table
```ts
cell.addTable(new Table(1, 1));
```
## Pagination
###Prevent row pagination
To prevent breaking contents of a row across multiple pages, call `cantSplit()`:
```ts
table.getRow(0).setCantSplit();
```
###Repeat row
If a table is paginated on multiple pages, it is possible to repeat a row at the top of each new page calling `setTableHeader()`:
```ts
table.getRow(0).setTableHeader();
```
## Examples
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo4.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo4.ts_
### Custom borders
Example showing how to add colourful borders to tables
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo20.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo20.ts_
### Adding images
Example showing how to add images to tables
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo24.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo24.ts_
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo36.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo36.ts_
### Alignment of text in a cell
Example showing how align text in a table cell
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo31.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo31.ts_
### Merging rows
Example showing merging of `rows`
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo32.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo32.ts_
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo41.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo41.ts_
### Merging columns
Example showing merging of `columns`
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo43.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo43.ts_
### Floating tables
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo34.ts ':include')
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo34.ts_

View File

@ -22,7 +22,7 @@ text.bold();
### Italics ### Italics
```js ```js
text.italic(); text.italics();
``` ```
### Underline ### Underline
@ -80,5 +80,5 @@ text.break();
What if you want to create a paragraph which is **_bold_** and **_italic_**? What if you want to create a paragraph which is **_bold_** and **_italic_**?
```js ```js
paragraph.bold().italic(); paragraph.bold().italics();
``` ```

View File

@ -1,6 +1,6 @@
{ {
"name": "docx", "name": "docx",
"version": "4.5.0", "version": "5.0.0-rc4",
"description": "Generate .docx documents with JavaScript (formerly Office-Clippy)", "description": "Generate .docx documents with JavaScript (formerly Office-Clippy)",
"main": "build/index.js", "main": "build/index.js",
"scripts": { "scripts": {
@ -8,7 +8,7 @@
"test": "mocha-webpack \"src/**/*.ts\"", "test": "mocha-webpack \"src/**/*.ts\"",
"test.coverage": "nyc npm test", "test.coverage": "nyc npm test",
"test.watch": "npm test -- --watch", "test.watch": "npm test -- --watch",
"prepublishOnly": "npm run build", "prepublishOnly": "npm run build --production",
"lint": "tslint --project .", "lint": "tslint --project .",
"build": "npm run webpack && npm run fix-types", "build": "npm run webpack && npm run fix-types",
"tsc": "rimraf ./build && tsc -p .", "tsc": "rimraf ./build && tsc -p .",
@ -17,7 +17,9 @@
"typedoc": "typedoc src/index.ts", "typedoc": "typedoc src/index.ts",
"style": "prettier -l \"src/**/*.ts\"", "style": "prettier -l \"src/**/*.ts\"",
"style.fix": "npm run style -- --write", "style.fix": "npm run style -- --write",
"fix-types": "node types-absolute-fixer.js", "fix-types": "ts-node scripts/types-absolute-fixer.ts",
"e2e": "ts-node scripts/e2e.ts",
"serve.docs": "cd docs && docsify serve",
"ts-node": "ts-node" "ts-node": "ts-node"
}, },
"pre-commit": [ "pre-commit": [
@ -47,9 +49,7 @@
], ],
"types": "./build/index.d.ts", "types": "./build/index.d.ts",
"dependencies": { "dependencies": {
"@types/image-size": "0.0.29",
"@types/jszip": "^3.1.4", "@types/jszip": "^3.1.4",
"image-size": "^0.6.2",
"jszip": "^3.1.5", "jszip": "^3.1.5",
"xml": "^1.0.1", "xml": "^1.0.1",
"xml-js": "^1.6.8" "xml-js": "^1.6.8"
@ -63,9 +63,12 @@
"devDependencies": { "devDependencies": {
"@types/chai": "^3.4.35", "@types/chai": "^3.4.35",
"@types/mocha": "^2.2.39", "@types/mocha": "^2.2.39",
"@types/request-promise": "^4.1.42",
"@types/sinon": "^4.3.1", "@types/sinon": "^4.3.1",
"@types/webpack": "^4.4.24",
"awesome-typescript-loader": "^3.4.1", "awesome-typescript-loader": "^3.4.1",
"chai": "^3.5.0", "chai": "^3.5.0",
"docsify": "^4.9.1",
"glob": "^7.1.2", "glob": "^7.1.2",
"istanbul-instrumenter-loader": "^3.0.1", "istanbul-instrumenter-loader": "^3.0.1",
"jszip": "^3.1.5", "jszip": "^3.1.5",
@ -73,9 +76,11 @@
"mocha-webpack": "^1.0.1", "mocha-webpack": "^1.0.1",
"nyc": "^13.1.0", "nyc": "^13.1.0",
"pre-commit": "^1.2.2", "pre-commit": "^1.2.2",
"prettier": "^1.12.1", "prettier": "^1.15.2",
"prompt": "^1.0.0", "prompt": "^1.0.0",
"replace-in-file": "^3.1.0", "replace-in-file": "^3.1.0",
"request": "^2.88.0",
"request-promise": "^4.2.2",
"rimraf": "^2.5.2", "rimraf": "^2.5.2",
"shelljs": "^0.7.7", "shelljs": "^0.7.7",
"sinon": "^5.0.7", "sinon": "^5.0.7",

30
scripts/e2e.ts Normal file
View File

@ -0,0 +1,30 @@
// tslint:disable:no-console
import * as fs from "fs";
import * as request from "request-promise";
async function e2e(filePath: string): Promise<void> {
console.log(`Running e2e for: ${filePath}`);
if (!fs.existsSync(filePath)) {
console.error("File not found");
throw Error("File not found");
}
const result = await request.post({
url: "https://wt-9017166451e5dc00461b648d19f5e8da-0.sandbox.auth0-extend.com/docx-validator",
formData: {
document: fs.createReadStream(filePath),
},
});
return result;
}
e2e(process.argv[2])
.then(() => {
console.log("Success! Document is valid");
})
.catch(() => {
console.log("Error! Validation failed");
process.exit(1);
});

View File

@ -1,5 +1,5 @@
const glob = require("glob"); import * as glob from "glob";
const replace = require("replace-in-file"); import * as replace from "replace-in-file";
const files = glob.sync("build/**/*.d.ts"); const files = glob.sync("build/**/*.d.ts");

View File

@ -1,10 +1,9 @@
import { assert } from "chai"; import { assert, expect } from "chai";
import { Formatter } from "../export/formatter"; import { Formatter } from "export/formatter";
import * as file from "../file"; import * as file from "file";
import { CoreProperties } from "../file/core-properties"; import { CoreProperties } from "file/core-properties";
import { Attributes } from "../file/xml-components"; import { Attributes } from "file/xml-components";
import { Utility } from "../tests/utility";
describe("Formatter", () => { describe("Formatter", () => {
let formatter: Formatter; let formatter: Formatter;
@ -31,33 +30,31 @@ describe("Formatter", () => {
const paragraph = new file.Paragraph(); const paragraph = new file.Paragraph();
paragraph.addRun(new file.TextRun("test").bold()); paragraph.addRun(new file.TextRun("test").bold());
const newJson = formatter.format(paragraph); const newJson = formatter.format(paragraph);
assert.isDefined(newJson["w:p"][1]["w:r"][0]["w:rPr"][0]["w:b"][0]._attr["w:val"]); assert.isDefined(newJson["w:p"][0]["w:r"][0]["w:rPr"][0]["w:b"]._attr["w:val"]);
}); });
it("should format attributes (rsidSect)", () => { it("should format attributes (rsidSect)", () => {
const attributes = new Attributes({ const attributes = new Attributes({
rsidSect: "test2", rsidSect: "test2",
}); });
let newJson = formatter.format(attributes); const tree = formatter.format(attributes);
newJson = Utility.jsonify(newJson); expect(tree).to.deep.equal({
if (newJson._attr === undefined) { _attr: {
assert.fail(); "w:rsidSect": "test2",
return; },
} });
assert.isDefined(newJson._attr["w:rsidSect"]);
}); });
it("should format attributes (val)", () => { it("should format attributes (val)", () => {
const attributes = new Attributes({ const attributes = new Attributes({
val: "test", val: "test",
}); });
let newJson = formatter.format(attributes); const tree = formatter.format(attributes);
newJson = Utility.jsonify(newJson); expect(tree).to.deep.equal({
if (newJson._attr === undefined) { _attr: {
assert.fail(); "w:val": "test",
return; },
} });
assert.isDefined(newJson._attr["w:val"]);
}); });
it("should should change 'p' tag into 'w:p' tag", () => { it("should should change 'p' tag into 'w:p' tag", () => {

View File

@ -0,0 +1,17 @@
import { IMediaData, Media } from "file/media";
export class ImageReplacer {
public replace(xmlData: string, mediaData: IMediaData[], offset: number): string {
let currentXmlData = xmlData;
mediaData.forEach((image, i) => {
currentXmlData = currentXmlData.replace(`{${image.fileName}}`, (offset + i).toString());
});
return currentXmlData;
}
public getMediaData(xmlData: string, media: Media): IMediaData[] {
return media.Array.filter((image) => xmlData.search(`{${image.fileName}}`) > 0);
}
}

View File

@ -17,7 +17,7 @@ describe("Compiler", () => {
describe("#compile()", () => { describe("#compile()", () => {
it("should pack all the content", async function() { it("should pack all the content", async function() {
this.timeout(99999999); this.timeout(99999999);
const zipFile = await compiler.compile(file); const zipFile = compiler.compile(file);
const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name); const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name);
expect(fileNames).is.an.instanceof(Array); expect(fileNames).is.an.instanceof(Array);
@ -46,7 +46,7 @@ describe("Compiler", () => {
this.timeout(99999999); this.timeout(99999999);
const zipFile = await compiler.compile(file); const zipFile = compiler.compile(file);
const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name); const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name);
expect(fileNames).is.an.instanceof(Array); expect(fileNames).is.an.instanceof(Array);

View File

@ -3,6 +3,7 @@ import * as xml from "xml";
import { File } from "file"; import { File } from "file";
import { Formatter } from "../formatter"; import { Formatter } from "../formatter";
import { ImageReplacer } from "./image-replacer";
interface IXmlifyedFile { interface IXmlifyedFile {
readonly data: string; readonly data: string;
@ -28,14 +29,17 @@ interface IXmlifyedFileMapping {
export class Compiler { export class Compiler {
private readonly formatter: Formatter; private readonly formatter: Formatter;
private readonly imageReplacer: ImageReplacer;
private readonly prettifyXml?: boolean;
constructor() { constructor(prettifyXml?: boolean) {
this.formatter = new Formatter(); this.formatter = new Formatter();
this.imageReplacer = new ImageReplacer();
this.prettifyXml = prettifyXml;
} }
public async compile(file: File): Promise<JSZip> { public compile(file: File): JSZip {
const zip = new JSZip(); const zip = new JSZip();
const xmlifiedFileMapping = this.xmlifyFile(file); const xmlifiedFileMapping = this.xmlifyFile(file);
for (const key in xmlifiedFileMapping) { for (const key in xmlifiedFileMapping) {
@ -59,30 +63,43 @@ export class Compiler {
zip.file(`word/media/${data.fileName}`, mediaData); zip.file(`word/media/${data.fileName}`, mediaData);
} }
for (const header of file.Headers) {
for (const data of header.Media.Array) {
zip.file(`word/media/${data.fileName}`, data.stream);
}
}
for (const footer of file.Footers) {
for (const data of footer.Media.Array) {
zip.file(`word/media/${data.fileName}`, data.stream);
}
}
return zip; return zip;
} }
private xmlifyFile(file: File): IXmlifyedFileMapping { private xmlifyFile(file: File): IXmlifyedFileMapping {
file.verifyUpdateFields(); file.verifyUpdateFields();
const documentRelationshipCount = file.DocumentRelationships.RelationshipCount + 1;
return { return {
Relationships: {
data: (() => {
const xmlData = xml(this.formatter.format(file.Document), this.prettifyXml);
const mediaDatas = this.imageReplacer.getMediaData(xmlData, file.Media);
mediaDatas.forEach((mediaData, i) => {
file.DocumentRelationships.createRelationship(
documentRelationshipCount + i,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
`media/${mediaData.fileName}`,
);
});
return xml(this.formatter.format(file.DocumentRelationships), this.prettifyXml);
})(),
path: "word/_rels/document.xml.rels",
},
Document: { Document: {
data: xml(this.formatter.format(file.Document), true), data: (() => {
const tempXmlData = xml(this.formatter.format(file.Document), this.prettifyXml);
const mediaDatas = this.imageReplacer.getMediaData(tempXmlData, file.Media);
const xmlData = this.imageReplacer.replace(tempXmlData, mediaDatas, documentRelationshipCount);
return xmlData;
})(),
path: "word/document.xml", path: "word/document.xml",
}, },
Styles: { Styles: {
data: xml(this.formatter.format(file.Styles)), data: xml(this.formatter.format(file.Styles), this.prettifyXml),
path: "word/styles.xml", path: "word/styles.xml",
}, },
Properties: { Properties: {
@ -95,58 +112,85 @@ export class Compiler {
path: "docProps/core.xml", path: "docProps/core.xml",
}, },
Numbering: { Numbering: {
data: xml(this.formatter.format(file.Numbering)), data: xml(this.formatter.format(file.Numbering), this.prettifyXml),
path: "word/numbering.xml", path: "word/numbering.xml",
}, },
Relationships: {
data: xml(this.formatter.format(file.DocumentRelationships)),
path: "word/_rels/document.xml.rels",
},
FileRelationships: { FileRelationships: {
data: xml(this.formatter.format(file.FileRelationships)), data: xml(this.formatter.format(file.FileRelationships), this.prettifyXml),
path: "_rels/.rels", path: "_rels/.rels",
}, },
Headers: file.Headers.map((headerWrapper, index) => ({ HeaderRelationships: file.Headers.map((headerWrapper, index) => {
data: xml(this.formatter.format(headerWrapper.Header)), const xmlData = xml(this.formatter.format(headerWrapper.Header), this.prettifyXml);
path: `word/header${index + 1}.xml`, const mediaDatas = this.imageReplacer.getMediaData(xmlData, file.Media);
})),
Footers: file.Footers.map((footerWrapper, index) => ({ mediaDatas.forEach((mediaData, i) => {
data: xml(this.formatter.format(footerWrapper.Footer)), headerWrapper.Relationships.createRelationship(
path: `word/footer${index + 1}.xml`, i,
})), "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
HeaderRelationships: file.Headers.map((headerWrapper, index) => ({ `media/${mediaData.fileName}`,
data: xml(this.formatter.format(headerWrapper.Relationships)), );
});
return {
data: xml(this.formatter.format(headerWrapper.Relationships), this.prettifyXml),
path: `word/_rels/header${index + 1}.xml.rels`, path: `word/_rels/header${index + 1}.xml.rels`,
})), };
FooterRelationships: file.Footers.map((footerWrapper, index) => ({ }),
data: xml(this.formatter.format(footerWrapper.Relationships)), FooterRelationships: file.Footers.map((footerWrapper, index) => {
const xmlData = xml(this.formatter.format(footerWrapper.Footer), this.prettifyXml);
const mediaDatas = this.imageReplacer.getMediaData(xmlData, file.Media);
mediaDatas.forEach((mediaData, i) => {
footerWrapper.Relationships.createRelationship(
i,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
`media/${mediaData.fileName}`,
);
});
return {
data: xml(this.formatter.format(footerWrapper.Relationships), this.prettifyXml),
path: `word/_rels/footer${index + 1}.xml.rels`, path: `word/_rels/footer${index + 1}.xml.rels`,
})), };
}),
Headers: file.Headers.map((headerWrapper, index) => {
const tempXmlData = xml(this.formatter.format(headerWrapper.Header), this.prettifyXml);
const mediaDatas = this.imageReplacer.getMediaData(tempXmlData, file.Media);
// TODO: 0 needs to be changed when headers get relationships of their own
const xmlData = this.imageReplacer.replace(tempXmlData, mediaDatas, 0);
return {
data: xmlData,
path: `word/header${index + 1}.xml`,
};
}),
Footers: file.Footers.map((footerWrapper, index) => {
const tempXmlData = xml(this.formatter.format(footerWrapper.Footer), this.prettifyXml);
const mediaDatas = this.imageReplacer.getMediaData(tempXmlData, file.Media);
// TODO: 0 needs to be changed when headers get relationships of their own
const xmlData = this.imageReplacer.replace(tempXmlData, mediaDatas, 0);
return {
data: xmlData,
path: `word/footer${index + 1}.xml`,
};
}),
ContentTypes: { ContentTypes: {
data: xml(this.formatter.format(file.ContentTypes)), data: xml(this.formatter.format(file.ContentTypes), this.prettifyXml),
path: "[Content_Types].xml", path: "[Content_Types].xml",
}, },
AppProperties: { AppProperties: {
data: xml(this.formatter.format(file.AppProperties)), data: xml(this.formatter.format(file.AppProperties), this.prettifyXml),
path: "docProps/app.xml", path: "docProps/app.xml",
}, },
FootNotes: { FootNotes: {
data: xml(this.formatter.format(file.FootNotes)), data: xml(this.formatter.format(file.FootNotes), this.prettifyXml),
path: "word/footnotes.xml", path: "word/footnotes.xml",
}, },
Settings: { Settings: {
data: xml(this.formatter.format(file.Settings)), data: xml(this.formatter.format(file.Settings), this.prettifyXml),
path: "word/settings.xml", path: "word/settings.xml",
}, },
}; };
} }
/* By default docx collapse empty tags. <a></a> -> <a/>. this function mimic it
so comparing (diff) original docx file and the library output is easier
Currently not used, so commenting out */
// private collapseEmptyTags(xmlData: string): string {
// const regEx = /<(([^ <>]+)[^<>]*)><\/\2>/g;
// const collapsed = xmlData.replace(regEx, "<$1/>");
// return collapsed;
// }
} }

View File

@ -46,4 +46,24 @@ describe("Packer", () => {
}); });
}); });
}); });
describe("#toBase64String()", () => {
it("should create a standard docx file", async function() {
this.timeout(99999999);
const str = await packer.toBase64String(file);
assert.isDefined(str);
assert.isTrue(str.length > 0);
});
it("should handle exception if it throws any", () => {
// tslint:disable-next-line:no-any
const compiler = stub((packer as any).compiler, "compile");
compiler.throwsException();
return packer.toBase64String(file).catch((error) => {
assert.isDefined(error);
});
});
});
}); });

View File

@ -4,12 +4,12 @@ import { Compiler } from "./next-compiler";
export class Packer { export class Packer {
private readonly compiler: Compiler; private readonly compiler: Compiler;
constructor() { constructor(prettifyXml?: boolean) {
this.compiler = new Compiler(); this.compiler = new Compiler(prettifyXml);
} }
public async toBuffer(file: File): Promise<Buffer> { public async toBuffer(file: File): Promise<Buffer> {
const zip = await this.compiler.compile(file); const zip = this.compiler.compile(file);
const zipData = (await zip.generateAsync({ const zipData = (await zip.generateAsync({
type: "nodebuffer", type: "nodebuffer",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
@ -19,7 +19,7 @@ export class Packer {
} }
public async toBase64String(file: File): Promise<string> { public async toBase64String(file: File): Promise<string> {
const zip = await this.compiler.compile(file); const zip = this.compiler.compile(file);
const zipData = (await zip.generateAsync({ const zipData = (await zip.generateAsync({
type: "base64", type: "base64",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
@ -29,7 +29,7 @@ export class Packer {
} }
public async toBlob(file: File): Promise<Blob> { public async toBlob(file: File): Promise<Blob> {
const zip = await this.compiler.compile(file); const zip = this.compiler.compile(file);
const zipData = (await zip.generateAsync({ const zipData = (await zip.generateAsync({
type: "blob", type: "blob",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",

View File

@ -20,64 +20,70 @@ describe("ContentTypes", () => {
expect(tree["Types"]).to.be.an.instanceof(Array); expect(tree["Types"]).to.be.an.instanceof(Array);
expect(tree["Types"][0]).to.deep.equal({ _attr: { xmlns: "http://schemas.openxmlformats.org/package/2006/content-types" } }); expect(tree["Types"][0]).to.deep.equal({ _attr: { xmlns: "http://schemas.openxmlformats.org/package/2006/content-types" } });
expect(tree["Types"][1]).to.deep.equal({ Default: [{ _attr: { ContentType: "image/png", Extension: "png" } }] }); expect(tree["Types"][1]).to.deep.equal({ Default: { _attr: { ContentType: "image/png", Extension: "png" } } });
expect(tree["Types"][2]).to.deep.equal({ Default: [{ _attr: { ContentType: "image/jpeg", Extension: "jpeg" } }] }); expect(tree["Types"][2]).to.deep.equal({ Default: { _attr: { ContentType: "image/jpeg", Extension: "jpeg" } } });
expect(tree["Types"][3]).to.deep.equal({ Default: [{ _attr: { ContentType: "image/jpeg", Extension: "jpg" } }] }); expect(tree["Types"][3]).to.deep.equal({ Default: { _attr: { ContentType: "image/jpeg", Extension: "jpg" } } });
expect(tree["Types"][4]).to.deep.equal({ Default: [{ _attr: { ContentType: "image/bmp", Extension: "bmp" } }] }); expect(tree["Types"][4]).to.deep.equal({ Default: { _attr: { ContentType: "image/bmp", Extension: "bmp" } } });
expect(tree["Types"][5]).to.deep.equal({ Default: [{ _attr: { ContentType: "image/gif", Extension: "gif" } }] }); expect(tree["Types"][5]).to.deep.equal({ Default: { _attr: { ContentType: "image/gif", Extension: "gif" } } });
expect(tree["Types"][6]).to.deep.equal({ expect(tree["Types"][6]).to.deep.equal({
Default: [{ _attr: { ContentType: "application/vnd.openxmlformats-package.relationships+xml", Extension: "rels" } }], Default: { _attr: { ContentType: "application/vnd.openxmlformats-package.relationships+xml", Extension: "rels" } },
}); });
expect(tree["Types"][7]).to.deep.equal({ Default: [{ _attr: { ContentType: "application/xml", Extension: "xml" } }] }); expect(tree["Types"][7]).to.deep.equal({ Default: { _attr: { ContentType: "application/xml", Extension: "xml" } } });
expect(tree["Types"][8]).to.deep.equal({ expect(tree["Types"][8]).to.deep.equal({
Override: [ Override: {
{
_attr: { _attr: {
ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml", ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml",
PartName: "/word/document.xml", PartName: "/word/document.xml",
}, },
}, },
],
}); });
expect(tree["Types"][9]).to.deep.equal({ expect(tree["Types"][9]).to.deep.equal({
Override: [ Override: {
{
_attr: { _attr: {
ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml", ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml",
PartName: "/word/styles.xml", PartName: "/word/styles.xml",
}, },
}, },
],
}); });
expect(tree["Types"][10]).to.deep.equal({ expect(tree["Types"][10]).to.deep.equal({
Override: [ Override: {
{
_attr: { _attr: {
ContentType: "application/vnd.openxmlformats-package.core-properties+xml", ContentType: "application/vnd.openxmlformats-package.core-properties+xml",
PartName: "/docProps/core.xml", PartName: "/docProps/core.xml",
}, },
}, },
],
}); });
expect(tree["Types"][11]).to.deep.equal({ expect(tree["Types"][11]).to.deep.equal({
Override: [ Override: {
{
_attr: { _attr: {
ContentType: "application/vnd.openxmlformats-officedocument.extended-properties+xml", ContentType: "application/vnd.openxmlformats-officedocument.extended-properties+xml",
PartName: "/docProps/app.xml", PartName: "/docProps/app.xml",
}, },
}, },
],
}); });
expect(tree["Types"][12]).to.deep.equal({ expect(tree["Types"][12]).to.deep.equal({
Override: [ Override: {
{
_attr: { _attr: {
ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml", ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml",
PartName: "/word/numbering.xml", PartName: "/word/numbering.xml",
}, },
}, },
], });
expect(tree["Types"][13]).to.deep.equal({
Override: {
_attr: {
ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml",
PartName: "/word/footnotes.xml",
},
},
});
expect(tree["Types"][14]).to.deep.equal({
Override: {
_attr: {
ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml",
PartName: "/word/settings.xml",
},
},
}); });
}); });
}); });
@ -88,26 +94,22 @@ describe("ContentTypes", () => {
contentTypes.addFooter(102); contentTypes.addFooter(102);
const tree = new Formatter().format(contentTypes); const tree = new Formatter().format(contentTypes);
expect(tree["Types"][14]).to.deep.equal({ expect(tree["Types"][15]).to.deep.equal({
Override: [ Override: {
{
_attr: { _attr: {
ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml", ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml",
PartName: "/word/footer101.xml", PartName: "/word/footer101.xml",
}, },
}, },
],
}); });
expect(tree["Types"][15]).to.deep.equal({ expect(tree["Types"][16]).to.deep.equal({
Override: [ Override: {
{
_attr: { _attr: {
ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml", ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml",
PartName: "/word/footer102.xml", PartName: "/word/footer102.xml",
}, },
}, },
],
}); });
}); });
}); });
@ -118,26 +120,22 @@ describe("ContentTypes", () => {
contentTypes.addHeader(202); contentTypes.addHeader(202);
const tree = new Formatter().format(contentTypes); const tree = new Formatter().format(contentTypes);
expect(tree["Types"][14]).to.deep.equal({ expect(tree["Types"][15]).to.deep.equal({
Override: [ Override: {
{
_attr: { _attr: {
ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml", ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml",
PartName: "/word/header201.xml", PartName: "/word/header201.xml",
}, },
}, },
],
}); });
expect(tree["Types"][15]).to.deep.equal({ expect(tree["Types"][16]).to.deep.equal({
Override: [ Override: {
{
_attr: { _attr: {
ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml", ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml",
PartName: "/word/header202.xml", PartName: "/word/header202.xml",
}, },
}, },
],
}); });
}); });
}); });

View File

@ -30,6 +30,7 @@ export class ContentTypes extends XmlComponent {
this.root.push(new Override("application/vnd.openxmlformats-officedocument.extended-properties+xml", "/docProps/app.xml")); this.root.push(new Override("application/vnd.openxmlformats-officedocument.extended-properties+xml", "/docProps/app.xml"));
this.root.push(new Override("application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml", "/word/numbering.xml")); this.root.push(new Override("application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml", "/word/numbering.xml"));
this.root.push(new Override("application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml", "/word/footnotes.xml")); this.root.push(new Override("application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml", "/word/footnotes.xml"));
this.root.push(new Override("application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml", "/word/settings.xml"));
} }
public addFooter(index: number): void { public addFooter(index: number): void {

View File

@ -17,7 +17,7 @@ describe("Body", () => {
expect(formatted) expect(formatted)
.to.have.property("w:sectPr") .to.have.property("w:sectPr")
.and.to.be.an.instanceof(Array); .and.to.be.an.instanceof(Array);
expect(formatted["w:sectPr"]).to.have.length(5); expect(formatted["w:sectPr"]).to.have.length(4);
}); });
}); });
@ -30,14 +30,14 @@ describe("Body", () => {
const formatted = new Formatter().format(body)["w:body"]; const formatted = new Formatter().format(body)["w:body"];
expect(formatted).to.be.an.instanceof(Array); expect(formatted).to.be.an.instanceof(Array);
const defaultSectionPr = formatted[0]["w:p"][1]["w:pPr"][0]["w:sectPr"]; const defaultSectionPr = formatted[0]["w:p"][0]["w:pPr"][0]["w:sectPr"];
// check that this is the default section and added first in paragraph // check that this is the default section and added first in paragraph
expect(defaultSectionPr[0]).to.deep.equal({ "w:pgSz": [{ _attr: { "w:h": 16838, "w:w": 11906, "w:orient": "portrait" } }] }); expect(defaultSectionPr[0]).to.deep.equal({ "w:pgSz": { _attr: { "w:h": 16838, "w:w": 11906, "w:orient": "portrait" } } });
// check for new section (since it's the last one, it's direct child of body) // check for new section (since it's the last one, it's direct child of body)
const newSection = formatted[1]["w:sectPr"]; const newSection = formatted[1]["w:sectPr"];
expect(newSection[0]).to.deep.equal({ "w:pgSz": [{ _attr: { "w:h": 10000, "w:w": 10000, "w:orient": "portrait" } }] }); expect(newSection[0]).to.deep.equal({ "w:pgSz": { _attr: { "w:h": 10000, "w:w": 10000, "w:orient": "portrait" } } });
}); });
it("should add section with default parameters", () => { it("should add section with default parameters", () => {
@ -52,15 +52,13 @@ describe("Body", () => {
"w:body": [ "w:body": [
{ {
"w:p": [ "w:p": [
{ "w:pPr": [] },
{ {
"w:pPr": [ "w:pPr": [
{ {
"w:sectPr": [ "w:sectPr": [
{ "w:pgSz": [{ _attr: { "w:w": 11906, "w:h": 16838, "w:orient": "portrait" } }] }, { "w:pgSz": { _attr: { "w:w": 11906, "w:h": 16838, "w:orient": "portrait" } } },
{
"w:pgMar": [
{ {
"w:pgMar": {
_attr: { _attr: {
"w:top": 1440, "w:top": 1440,
"w:right": 1440, "w:right": 1440,
@ -72,11 +70,9 @@ describe("Body", () => {
"w:mirrorMargins": false, "w:mirrorMargins": false,
}, },
}, },
],
}, },
{ "w:cols": [{ _attr: { "w:space": 708 } }] }, { "w:cols": { _attr: { "w:space": 708, "w:num": 1 } } },
{ "w:docGrid": [{ _attr: { "w:linePitch": 360 } }] }, { "w:docGrid": { _attr: { "w:linePitch": 360 } } },
{ "w:pgNumType": [{ _attr: { "w:fmt": "decimal" } }] },
], ],
}, },
], ],
@ -85,10 +81,9 @@ describe("Body", () => {
}, },
{ {
"w:sectPr": [ "w:sectPr": [
{ "w:pgSz": [{ _attr: { "w:w": 10000, "w:h": 10000, "w:orient": "portrait" } }] }, { "w:pgSz": { _attr: { "w:w": 10000, "w:h": 10000, "w:orient": "portrait" } } },
{
"w:pgMar": [
{ {
"w:pgMar": {
_attr: { _attr: {
"w:top": 1440, "w:top": 1440,
"w:right": 1440, "w:right": 1440,
@ -100,11 +95,9 @@ describe("Body", () => {
"w:mirrorMargins": false, "w:mirrorMargins": false,
}, },
}, },
],
}, },
{ "w:cols": [{ _attr: { "w:space": 708 } }] }, { "w:cols": { _attr: { "w:space": 708, "w:num": 1 } } },
{ "w:docGrid": [{ _attr: { "w:linePitch": 360 } }] }, { "w:docGrid": { _attr: { "w:linePitch": 360 } } },
{ "w:pgNumType": [{ _attr: { "w:fmt": "decimal" } }] },
], ],
}, },
], ],

View File

@ -37,7 +37,7 @@ export class Body extends XmlComponent {
} }
public prepForXml(): IXmlableObject | undefined { public prepForXml(): IXmlableObject | undefined {
if (this.sections.length === 1) { if (this.sections.length === 1) {
this.root.push(this.sections[0]); this.root.push(this.sections.pop() as SectionProperties);
} }
return super.prepForXml(); return super.prepForXml();

View File

@ -2,10 +2,12 @@ import { XmlAttributeComponent } from "file/xml-components";
export interface IColumnsAttributes { export interface IColumnsAttributes {
readonly space?: number; readonly space?: number;
readonly num?: number;
} }
export class ColumnsAttributes extends XmlAttributeComponent<IColumnsAttributes> { export class ColumnsAttributes extends XmlAttributeComponent<IColumnsAttributes> {
protected readonly xmlKeys = { protected readonly xmlKeys = {
space: "w:space", space: "w:space",
num: "w:num",
}; };
} }

View File

@ -2,11 +2,12 @@ import { XmlComponent } from "file/xml-components";
import { ColumnsAttributes } from "./columns-attributes"; import { ColumnsAttributes } from "./columns-attributes";
export class Columns extends XmlComponent { export class Columns extends XmlComponent {
constructor(space: number) { constructor(space: number, num: number) {
super("w:cols"); super("w:cols");
this.root.push( this.root.push(
new ColumnsAttributes({ new ColumnsAttributes({
space: space, space: space,
num: num,
}), }),
); );
} }

View File

@ -4,3 +4,4 @@ export * from "./header-reference";
export * from "./page-size"; export * from "./page-size";
export * from "./page-number"; export * from "./page-number";
export * from "./page-border"; export * from "./page-border";
export * from "./line-number";

View File

@ -0,0 +1 @@
export * from "./line-number";

View File

@ -0,0 +1,38 @@
// http://officeopenxml.com/WPsectionLineNumbering.php
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
export enum LineNumberRestartFormat {
CONTINUOUS = "continuous",
NEW_SECTION = "newSection",
NEW_PAGE = "newPage",
}
export interface ILineNumberAttributes {
readonly lineNumberCountBy?: number;
readonly lineNumberStart?: number;
readonly lineNumberRestart?: LineNumberRestartFormat;
readonly lineNumberDistance?: number;
}
export class LineNumberAttributes extends XmlAttributeComponent<ILineNumberAttributes> {
protected readonly xmlKeys = {
lineNumberCountBy: "w:countBy",
lineNumberStart: "w:start",
lineNumberRestart: "w:restart",
lineNumberDistance: "w:distance",
};
}
export class LineNumberType extends XmlComponent {
constructor(countBy?: number, start?: number, restart?: LineNumberRestartFormat, dist?: number) {
super("w:lnNumType");
this.root.push(
new LineNumberAttributes({
lineNumberCountBy: countBy,
lineNumberStart: start,
lineNumberRestart: restart,
lineNumberDistance: dist,
}),
);
}
}

View File

@ -21,8 +21,7 @@ describe("PageBorders", () => {
const tree = new Formatter().format(properties); const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:pgBorders"]); expect(Object.keys(tree)).to.deep.equal(["w:pgBorders"]);
expect(tree["w:pgBorders"]).to.be.an.instanceof(Array); expect(tree["w:pgBorders"]).to.deep.equal({ _attr: { "w:display": "firstPage" } });
expect(tree["w:pgBorders"][0]).to.deep.equal({ _attr: { "w:display": "firstPage" } });
}); });
it("should create page borders with full configuration", () => { it("should create page borders with full configuration", () => {
@ -58,32 +57,24 @@ describe("PageBorders", () => {
expect(tree["w:pgBorders"]).to.be.an.instanceof(Array); expect(tree["w:pgBorders"]).to.be.an.instanceof(Array);
expect(tree["w:pgBorders"][0]).to.deep.equal({ _attr: { "w:display": "firstPage", "w:zOrder": "back" } }); expect(tree["w:pgBorders"][0]).to.deep.equal({ _attr: { "w:display": "firstPage", "w:zOrder": "back" } });
expect(tree["w:pgBorders"][1]).to.deep.equal({ expect(tree["w:pgBorders"][1]).to.deep.equal({
"w:top": [ "w:top": {
{
_attr: { "w:color": "001122", "w:size": 10, "w:val": "doubleWave" }, _attr: { "w:color": "001122", "w:size": 10, "w:val": "doubleWave" },
}, },
],
}); });
expect(tree["w:pgBorders"][2]).to.deep.equal({ expect(tree["w:pgBorders"][2]).to.deep.equal({
"w:right": [ "w:right": {
{
_attr: { "w:color": "223344", "w:size": 20, "w:val": "double" }, _attr: { "w:color": "223344", "w:size": 20, "w:val": "double" },
}, },
],
}); });
expect(tree["w:pgBorders"][3]).to.deep.equal({ expect(tree["w:pgBorders"][3]).to.deep.equal({
"w:bottom": [ "w:bottom": {
{
_attr: { "w:color": "556677", "w:size": 30, "w:val": "single" }, _attr: { "w:color": "556677", "w:size": 30, "w:val": "single" },
}, },
],
}); });
expect(tree["w:pgBorders"][4]).to.deep.equal({ expect(tree["w:pgBorders"][4]).to.deep.equal({
"w:left": [ "w:left": {
{
_attr: { "w:color": "889900", "w:size": 40, "w:val": "dotted" }, _attr: { "w:color": "889900", "w:size": 40, "w:val": "dotted" },
}, },
],
}); });
}); });
}); });

View File

@ -1,6 +1,6 @@
// http://officeopenxml.com/WPsectionBorders.php // http://officeopenxml.com/WPsectionBorders.php
import { BorderStyle } from "file/styles"; import { BorderStyle } from "file/styles";
import { IXmlableObject, XmlAttributeComponent, XmlComponent } from "file/xml-components"; import { IgnoreIfEmptyXmlComponent, XmlAttributeComponent, XmlComponent } from "file/xml-components";
export enum PageBorderDisplay { export enum PageBorderDisplay {
ALL_PAGES = "allPages", ALL_PAGES = "allPages",
@ -64,7 +64,7 @@ class PageBordersAttributes extends XmlAttributeComponent<IPageBorderAttributes>
}; };
} }
export class PageBorders extends XmlComponent { export class PageBorders extends IgnoreIfEmptyXmlComponent {
constructor(options?: IPageBordersOptions) { constructor(options?: IPageBordersOptions) {
super("w:pgBorders"); super("w:pgBorders");
@ -97,10 +97,4 @@ export class PageBorders extends XmlComponent {
this.root.push(new PageBorder("w:left", options.pageBorderLeft)); this.root.push(new PageBorder("w:left", options.pageBorderLeft));
} }
} }
public prepForXml(): IXmlableObject | undefined {
if (this.root.length > 0) {
return super.prepForXml();
}
}
} }

View File

@ -12,8 +12,7 @@ describe("PageSize", () => {
const tree = new Formatter().format(properties); const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:pgSz"]); expect(Object.keys(tree)).to.deep.equal(["w:pgSz"]);
expect(tree["w:pgSz"]).to.be.an.instanceof(Array); expect(tree["w:pgSz"]).to.deep.equal({ _attr: { "w:h": 200, "w:w": 100, "w:orient": "portrait" } });
expect(tree["w:pgSz"][0]).to.deep.equal({ _attr: { "w:h": 200, "w:w": 100, "w:orient": "portrait" } });
}); });
it("should create page size with horizontal and invert the lengths", () => { it("should create page size with horizontal and invert the lengths", () => {
@ -21,8 +20,7 @@ describe("PageSize", () => {
const tree = new Formatter().format(properties); const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:pgSz"]); expect(Object.keys(tree)).to.deep.equal(["w:pgSz"]);
expect(tree["w:pgSz"]).to.be.an.instanceof(Array); expect(tree["w:pgSz"]).to.deep.equal({ _attr: { "w:h": 100, "w:w": 200, "w:orient": "landscape" } });
expect(tree["w:pgSz"][0]).to.deep.equal({ _attr: { "w:h": 100, "w:w": 200, "w:orient": "landscape" } });
}); });
}); });
}); });

View File

@ -25,7 +25,10 @@ describe("SectionProperties", () => {
footer: 708, footer: 708,
gutter: 0, gutter: 0,
mirror: false, mirror: false,
column: {
space: 708, space: 708,
count: 1,
},
linePitch: 360, linePitch: 360,
headers: { headers: {
default: new HeaderWrapper(media, 100), default: new HeaderWrapper(media, 100),
@ -39,10 +42,9 @@ describe("SectionProperties", () => {
const tree = new Formatter().format(properties); const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]); expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
expect(tree["w:sectPr"]).to.be.an.instanceof(Array); expect(tree["w:sectPr"]).to.be.an.instanceof(Array);
expect(tree["w:sectPr"][0]).to.deep.equal({ "w:pgSz": [{ _attr: { "w:h": 16838, "w:w": 11906, "w:orient": "portrait" } }] }); expect(tree["w:sectPr"][0]).to.deep.equal({ "w:pgSz": { _attr: { "w:h": 16838, "w:w": 11906, "w:orient": "portrait" } } });
expect(tree["w:sectPr"][1]).to.deep.equal({ expect(tree["w:sectPr"][1]).to.deep.equal({
"w:pgMar": [ "w:pgMar": {
{
_attr: { _attr: {
"w:bottom": 1440, "w:bottom": 1440,
"w:footer": 708, "w:footer": 708,
@ -54,14 +56,13 @@ describe("SectionProperties", () => {
"w:mirrorMargins": false, "w:mirrorMargins": false,
}, },
}, },
],
}); });
expect(tree["w:sectPr"][2]).to.deep.equal({ "w:cols": [{ _attr: { "w:space": 708 } }] }); expect(tree["w:sectPr"][2]).to.deep.equal({ "w:cols": { _attr: { "w:space": 708, "w:num": 1 } } });
expect(tree["w:sectPr"][3]).to.deep.equal({ "w:docGrid": [{ _attr: { "w:linePitch": 360 } }] }); expect(tree["w:sectPr"][3]).to.deep.equal({ "w:docGrid": { _attr: { "w:linePitch": 360 } } });
expect(tree["w:sectPr"][4]).to.deep.equal({ "w:headerReference": [{ _attr: { "r:id": "rId100", "w:type": "default" } }] }); expect(tree["w:sectPr"][4]).to.deep.equal({ "w:headerReference": { _attr: { "r:id": "rId100", "w:type": "default" } } });
expect(tree["w:sectPr"][5]).to.deep.equal({ "w:footerReference": [{ _attr: { "r:id": "rId200", "w:type": "even" } }] }); expect(tree["w:sectPr"][5]).to.deep.equal({ "w:footerReference": { _attr: { "r:id": "rId200", "w:type": "even" } } });
expect(tree["w:sectPr"][6]).to.deep.equal({ "w:pgNumType": [{ _attr: { "w:fmt": "cardinalText", "w:start": 10 } }] }); expect(tree["w:sectPr"][6]).to.deep.equal({ "w:pgNumType": { _attr: { "w:fmt": "cardinalText", "w:start": 10 } } });
}); });
it("should create section properties with no options", () => { it("should create section properties with no options", () => {
@ -69,10 +70,9 @@ describe("SectionProperties", () => {
const tree = new Formatter().format(properties); const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]); expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
expect(tree["w:sectPr"]).to.be.an.instanceof(Array); expect(tree["w:sectPr"]).to.be.an.instanceof(Array);
expect(tree["w:sectPr"][0]).to.deep.equal({ "w:pgSz": [{ _attr: { "w:h": 16838, "w:w": 11906, "w:orient": "portrait" } }] }); expect(tree["w:sectPr"][0]).to.deep.equal({ "w:pgSz": { _attr: { "w:h": 16838, "w:w": 11906, "w:orient": "portrait" } } });
expect(tree["w:sectPr"][1]).to.deep.equal({ expect(tree["w:sectPr"][1]).to.deep.equal({
"w:pgMar": [ "w:pgMar": {
{
_attr: { _attr: {
"w:bottom": 1440, "w:bottom": 1440,
"w:footer": 708, "w:footer": 708,
@ -84,11 +84,9 @@ describe("SectionProperties", () => {
"w:mirrorMargins": false, "w:mirrorMargins": false,
}, },
}, },
],
}); });
expect(tree["w:sectPr"][2]).to.deep.equal({ "w:cols": [{ _attr: { "w:space": 708 } }] }); expect(tree["w:sectPr"][2]).to.deep.equal({ "w:cols": { _attr: { "w:space": 708, "w:num": 1 } } });
expect(tree["w:sectPr"][3]).to.deep.equal({ "w:docGrid": [{ _attr: { "w:linePitch": 360 } }] }); expect(tree["w:sectPr"][3]).to.deep.equal({ "w:docGrid": { _attr: { "w:linePitch": 360 } } });
expect(tree["w:sectPr"][4]).to.deep.equal({ "w:pgNumType": [{ _attr: { "w:fmt": "decimal" } }] });
}); });
it("should create section properties with changed options", () => { it("should create section properties with changed options", () => {
@ -98,10 +96,9 @@ describe("SectionProperties", () => {
const tree = new Formatter().format(properties); const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]); expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
expect(tree["w:sectPr"]).to.be.an.instanceof(Array); expect(tree["w:sectPr"]).to.be.an.instanceof(Array);
expect(tree["w:sectPr"][0]).to.deep.equal({ "w:pgSz": [{ _attr: { "w:h": 16838, "w:w": 11906, "w:orient": "portrait" } }] }); expect(tree["w:sectPr"][0]).to.deep.equal({ "w:pgSz": { _attr: { "w:h": 16838, "w:w": 11906, "w:orient": "portrait" } } });
expect(tree["w:sectPr"][1]).to.deep.equal({ expect(tree["w:sectPr"][1]).to.deep.equal({
"w:pgMar": [ "w:pgMar": {
{
_attr: { _attr: {
"w:bottom": 1440, "w:bottom": 1440,
"w:footer": 708, "w:footer": 708,
@ -113,7 +110,6 @@ describe("SectionProperties", () => {
"w:mirrorMargins": false, "w:mirrorMargins": false,
}, },
}, },
],
}); });
}); });
@ -124,10 +120,9 @@ describe("SectionProperties", () => {
const tree = new Formatter().format(properties); const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]); expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
expect(tree["w:sectPr"]).to.be.an.instanceof(Array); expect(tree["w:sectPr"]).to.be.an.instanceof(Array);
expect(tree["w:sectPr"][0]).to.deep.equal({ "w:pgSz": [{ _attr: { "w:h": 16838, "w:w": 11906, "w:orient": "portrait" } }] }); expect(tree["w:sectPr"][0]).to.deep.equal({ "w:pgSz": { _attr: { "w:h": 16838, "w:w": 11906, "w:orient": "portrait" } } });
expect(tree["w:sectPr"][1]).to.deep.equal({ expect(tree["w:sectPr"][1]).to.deep.equal({
"w:pgMar": [ "w:pgMar": {
{
_attr: { _attr: {
"w:bottom": 0, "w:bottom": 0,
"w:footer": 708, "w:footer": 708,
@ -139,7 +134,6 @@ describe("SectionProperties", () => {
"w:mirrorMargins": false, "w:mirrorMargins": false,
}, },
}, },
],
}); });
}); });
@ -151,10 +145,9 @@ describe("SectionProperties", () => {
const tree = new Formatter().format(properties); const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]); expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
expect(tree["w:sectPr"]).to.be.an.instanceof(Array); expect(tree["w:sectPr"]).to.be.an.instanceof(Array);
expect(tree["w:sectPr"][0]).to.deep.equal({ "w:pgSz": [{ _attr: { "w:h": 0, "w:w": 0, "w:orient": "portrait" } }] }); expect(tree["w:sectPr"][0]).to.deep.equal({ "w:pgSz": { _attr: { "w:h": 0, "w:w": 0, "w:orient": "portrait" } } });
expect(tree["w:sectPr"][1]).to.deep.equal({ expect(tree["w:sectPr"][1]).to.deep.equal({
"w:pgMar": [ "w:pgMar": {
{
_attr: { _attr: {
"w:bottom": 1440, "w:bottom": 1440,
"w:footer": 708, "w:footer": 708,
@ -166,7 +159,6 @@ describe("SectionProperties", () => {
"w:mirrorMargins": false, "w:mirrorMargins": false,
}, },
}, },
],
}); });
}); });
@ -180,8 +172,28 @@ describe("SectionProperties", () => {
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]); expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
const pgBorders = tree["w:sectPr"].find((item) => item["w:pgBorders"] !== undefined); const pgBorders = tree["w:sectPr"].find((item) => item["w:pgBorders"] !== undefined);
expect(pgBorders).to.deep.equal({ expect(pgBorders).to.deep.equal({
"w:pgBorders": [{ _attr: { "w:offsetFrom": "page" } }], "w:pgBorders": { _attr: { "w:offsetFrom": "page" } },
}); });
});
it("should create section properties with page number type, but without start attribute", () => {
const properties = new SectionProperties({
pageNumberFormatType: PageNumberFormat.UPPER_ROMAN,
});
const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
const pgNumType = tree["w:sectPr"].find((item) => item["w:pgNumType"] !== undefined);
expect(pgNumType).to.deep.equal({
"w:pgNumType": { _attr: { "w:fmt": "upperRoman" } },
});
});
it("should create section properties without page number type", () => {
const properties = new SectionProperties({});
const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
const pgNumType = tree["w:sectPr"].find((item) => item["w:pgNumType"] !== undefined);
expect(pgNumType).to.equal(undefined);
}); });
}); });
}); });

View File

@ -4,17 +4,17 @@ import { HeaderWrapper } from "file/header-wrapper";
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { Columns } from "./columns/columns"; import { Columns } from "./columns/columns";
import { IColumnsAttributes } from "./columns/columns-attributes";
import { DocumentGrid } from "./doc-grid/doc-grid"; import { DocumentGrid } from "./doc-grid/doc-grid";
import { IDocGridAttributesProperties } from "./doc-grid/doc-grid-attributes"; import { IDocGridAttributesProperties } from "./doc-grid/doc-grid-attributes";
import { FooterReferenceType } from "./footer-reference"; import { FooterReferenceType } from "./footer-reference";
import { FooterReference } from "./footer-reference/footer-reference"; import { FooterReference } from "./footer-reference/footer-reference";
import { HeaderReferenceType } from "./header-reference"; import { HeaderReferenceType } from "./header-reference";
import { HeaderReference } from "./header-reference/header-reference"; import { HeaderReference } from "./header-reference/header-reference";
import { ILineNumberAttributes, LineNumberType } from "./line-number";
import { IPageBordersOptions, PageBorders } from "./page-border"; import { IPageBordersOptions, PageBorders } from "./page-border";
import { PageMargin } from "./page-margin/page-margin"; import { PageMargin } from "./page-margin/page-margin";
import { IPageMarginAttributes } from "./page-margin/page-margin-attributes"; import { IPageMarginAttributes } from "./page-margin/page-margin-attributes";
import { IPageNumberTypeAttributes, PageNumberFormat, PageNumberType } from "./page-number"; import { IPageNumberTypeAttributes, PageNumberType } from "./page-number";
import { PageSize } from "./page-size/page-size"; import { PageSize } from "./page-size/page-size";
import { IPageSizeAttributes, PageOrientation } from "./page-size/page-size-attributes"; import { IPageSizeAttributes, PageOrientation } from "./page-size/page-size-attributes";
import { TitlePage } from "./title-page/title-page"; import { TitlePage } from "./title-page/title-page";
@ -39,18 +39,24 @@ interface ITitlePageOptions {
export type SectionPropertiesOptions = IPageSizeAttributes & export type SectionPropertiesOptions = IPageSizeAttributes &
IPageMarginAttributes & IPageMarginAttributes &
IColumnsAttributes &
IDocGridAttributesProperties & IDocGridAttributesProperties &
IHeadersOptions & IHeadersOptions &
IFootersOptions & IFootersOptions &
IPageNumberTypeAttributes & IPageNumberTypeAttributes &
ILineNumberAttributes &
IPageBordersOptions & IPageBordersOptions &
ITitlePageOptions; ITitlePageOptions & {
readonly column?: {
readonly space?: number;
readonly count?: number;
};
};
// Need to decouple this from the attributes
export class SectionProperties extends XmlComponent { export class SectionProperties extends XmlComponent {
private readonly options: SectionPropertiesOptions; private readonly options: SectionPropertiesOptions;
constructor(options: SectionPropertiesOptions = {}) { constructor(options: SectionPropertiesOptions = { column: {} }) {
super("w:sectPr"); super("w:sectPr");
const { const {
@ -64,13 +70,17 @@ export class SectionProperties extends XmlComponent {
footer = 708, footer = 708,
gutter = 0, gutter = 0,
mirror = false, mirror = false,
space = 708, column = {},
linePitch = 360, linePitch = 360,
orientation = PageOrientation.PORTRAIT, orientation = PageOrientation.PORTRAIT,
headers, headers,
footers, footers,
pageNumberFormatType = PageNumberFormat.DECIMAL, pageNumberFormatType,
pageNumberStart, pageNumberStart,
lineNumberCountBy,
lineNumberStart,
lineNumberRestart,
lineNumberDistance,
pageBorders, pageBorders,
pageBorderTop, pageBorderTop,
pageBorderRight, pageBorderRight,
@ -82,13 +92,19 @@ export class SectionProperties extends XmlComponent {
this.options = options; this.options = options;
this.root.push(new PageSize(width, height, orientation)); this.root.push(new PageSize(width, height, orientation));
this.root.push(new PageMargin(top, right, bottom, left, header, footer, gutter, mirror)); this.root.push(new PageMargin(top, right, bottom, left, header, footer, gutter, mirror));
this.root.push(new Columns(space)); this.root.push(new Columns(column.space ? column.space : 708, column.count ? column.count : 1));
this.root.push(new DocumentGrid(linePitch)); this.root.push(new DocumentGrid(linePitch));
this.addHeaders(headers); this.addHeaders(headers);
this.addFooters(footers); this.addFooters(footers);
if (pageNumberStart || pageNumberFormatType) {
this.root.push(new PageNumberType(pageNumberStart, pageNumberFormatType)); this.root.push(new PageNumberType(pageNumberStart, pageNumberFormatType));
}
if (lineNumberCountBy || lineNumberStart || lineNumberRestart || lineNumberDistance) {
this.root.push(new LineNumberType(lineNumberCountBy, lineNumberStart, lineNumberRestart, lineNumberDistance));
}
if (pageBorders || pageBorderTop || pageBorderRight || pageBorderBottom || pageBorderLeft) { if (pageBorders || pageBorderTop || pageBorderRight || pageBorderBottom || pageBorderLeft) {
this.root.push( this.root.push(

View File

@ -11,8 +11,7 @@ describe("PageSize", () => {
const tree = new Formatter().format(properties); const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:titlePg"]); expect(Object.keys(tree)).to.deep.equal(["w:titlePg"]);
expect(tree["w:titlePg"]).to.be.an.instanceof(Array); expect(tree["w:titlePg"]).to.deep.equal({ _attr: { "w:val": "1" } });
expect(tree["w:titlePg"][0]).to.deep.equal({ _attr: { "w:val": "1" } });
}); });
}); });
}); });

View File

@ -52,14 +52,17 @@ describe("Document", () => {
expect(body[0]) expect(body[0])
.to.have.property("w:p") .to.have.property("w:p")
.which.includes({ .which.includes({
"w:r": [{ "w:rPr": [] }, { "w:t": [{ _attr: { "xml:space": "preserve" } }, "sample paragraph text"] }], "w:r": [{ "w:t": [{ _attr: { "xml:space": "preserve" } }, "sample paragraph text"] }],
}); });
}); });
}); });
describe("#createTable", () => { describe("#createTable", () => {
it("should create a new table and append it to body", () => { it("should create a new table and append it to body", () => {
const table = document.createTable(2, 3); const table = document.createTable({
rows: 2,
columns: 3,
});
expect(table).to.be.an.instanceof(Table); expect(table).to.be.an.instanceof(Table);
const body = new Formatter().format(document)["w:document"][1]["w:body"]; const body = new Formatter().format(document)["w:document"][1]["w:body"];
expect(body) expect(body)
@ -69,7 +72,10 @@ describe("Document", () => {
}); });
it("should create a table with the correct dimensions", () => { it("should create a table with the correct dimensions", () => {
document.createTable(2, 3); document.createTable({
rows: 2,
columns: 3,
});
const body = new Formatter().format(document)["w:document"][1]["w:body"]; const body = new Formatter().format(document)["w:document"][1]["w:body"];
expect(body) expect(body)
.to.be.an("array") .to.be.an("array")
@ -78,9 +84,9 @@ describe("Document", () => {
.to.have.property("w:tbl") .to.have.property("w:tbl")
.which.includes({ .which.includes({
"w:tblGrid": [ "w:tblGrid": [
{ "w:gridCol": [{ _attr: { "w:w": 1 } }] }, { "w:gridCol": { _attr: { "w:w": 100 } } },
{ "w:gridCol": [{ _attr: { "w:w": 1 } }] }, { "w:gridCol": { _attr: { "w:w": 100 } } },
{ "w:gridCol": [{ _attr: { "w:w": 1 } }] }, { "w:gridCol": { _attr: { "w:w": 100 } } },
], ],
}); });
expect(body[0]["w:tbl"].filter((x) => x["w:tr"])).to.have.length(2); expect(body[0]["w:tbl"].filter((x) => x["w:tr"])).to.have.length(2);

View File

@ -1,7 +1,7 @@
// http://officeopenxml.com/WPdocument.php // http://officeopenxml.com/WPdocument.php
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { Paragraph } from "../paragraph"; import { Paragraph } from "../paragraph";
import { Table } from "../table"; import { ITableOptions, Table } from "../table";
import { TableOfContents } from "../table-of-contents"; import { TableOfContents } from "../table-of-contents";
import { Body } from "./body"; import { Body } from "./body";
import { SectionPropertiesOptions } from "./body/section-properties"; import { SectionPropertiesOptions } from "./body/section-properties";
@ -53,12 +53,13 @@ export class Document extends XmlComponent {
return para; return para;
} }
public addTable(table: Table): void { public addTable(table: Table): Document {
this.body.push(table); this.body.push(table);
return this;
} }
public createTable(rows: number, cols: number): Table { public createTable(options: ITableOptions): Table {
const table = new Table(rows, cols); const table = new Table(options);
this.addTable(table); this.addTable(table);
return table; return table;
} }

View File

@ -3,12 +3,25 @@ import { assert } from "chai";
import { Utility } from "tests/utility"; import { Utility } from "tests/utility";
import { IDrawingOptions } from "../drawing"; import { IDrawingOptions } from "../drawing";
import { TextWrapStyle } from "../text-wrap"; import { TextWrappingType } from "../text-wrap";
import { Anchor } from "./anchor"; import { Anchor } from "./anchor";
function createAnchor(drawingOptions: IDrawingOptions): Anchor { function createAnchor(drawingOptions: IDrawingOptions): Anchor {
return new Anchor( return new Anchor(
1, {
fileName: "test.png",
stream: new Buffer(""),
dimensions: {
pixels: {
x: 0,
y: 0,
},
emus: {
x: 0,
y: 0,
},
},
},
{ {
pixels: { pixels: {
x: 100, x: 100,
@ -123,9 +136,6 @@ describe("Anchor", () => {
it("should create a Drawing with square text wrapping", () => { it("should create a Drawing with square text wrapping", () => {
anchor = createAnchor({ anchor = createAnchor({
textWrapping: {
textWrapStyle: TextWrapStyle.SQUARE,
},
floating: { floating: {
verticalPosition: { verticalPosition: {
offset: 0, offset: 0,
@ -133,6 +143,9 @@ describe("Anchor", () => {
horizontalPosition: { horizontalPosition: {
offset: 0, offset: 0,
}, },
wrap: {
type: TextWrappingType.SQUARE,
},
}, },
}); });
const newJson = Utility.jsonify(anchor); const newJson = Utility.jsonify(anchor);
@ -145,9 +158,6 @@ describe("Anchor", () => {
it("should create a Drawing with no text wrapping", () => { it("should create a Drawing with no text wrapping", () => {
anchor = createAnchor({ anchor = createAnchor({
textWrapping: {
textWrapStyle: TextWrapStyle.NONE,
},
floating: { floating: {
verticalPosition: { verticalPosition: {
offset: 0, offset: 0,
@ -155,6 +165,9 @@ describe("Anchor", () => {
horizontalPosition: { horizontalPosition: {
offset: 0, offset: 0,
}, },
wrap: {
type: TextWrappingType.NONE,
},
}, },
}); });
const newJson = Utility.jsonify(anchor); const newJson = Utility.jsonify(anchor);
@ -166,9 +179,6 @@ describe("Anchor", () => {
it("should create a Drawing with tight text wrapping", () => { it("should create a Drawing with tight text wrapping", () => {
anchor = createAnchor({ anchor = createAnchor({
textWrapping: {
textWrapStyle: TextWrapStyle.TIGHT,
},
floating: { floating: {
horizontalPosition: { horizontalPosition: {
offset: 0, offset: 0,
@ -176,6 +186,9 @@ describe("Anchor", () => {
verticalPosition: { verticalPosition: {
offset: 0, offset: 0,
}, },
wrap: {
type: TextWrappingType.TIGHT,
},
}, },
}); });
const newJson = Utility.jsonify(anchor); const newJson = Utility.jsonify(anchor);
@ -187,9 +200,6 @@ describe("Anchor", () => {
it("should create a Drawing with tight text wrapping", () => { it("should create a Drawing with tight text wrapping", () => {
anchor = createAnchor({ anchor = createAnchor({
textWrapping: {
textWrapStyle: TextWrapStyle.TOP_AND_BOTTOM,
},
floating: { floating: {
verticalPosition: { verticalPosition: {
offset: 0, offset: 0,
@ -197,6 +207,9 @@ describe("Anchor", () => {
horizontalPosition: { horizontalPosition: {
offset: 0, offset: 0,
}, },
wrap: {
type: TextWrappingType.TOP_AND_BOTTOM,
},
}, },
}); });
const newJson = Utility.jsonify(anchor); const newJson = Utility.jsonify(anchor);

View File

@ -1,10 +1,10 @@
// http://officeopenxml.com/drwPicFloating.php // http://officeopenxml.com/drwPicFloating.php
import { IMediaDataDimensions } from "file/media"; import { IMediaData, IMediaDataDimensions } from "file/media";
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { IDrawingOptions } from "../drawing"; import { IDrawingOptions } from "../drawing";
import { HorizontalPosition, IFloating, SimplePos, VerticalPosition } from "../floating"; import { HorizontalPosition, IFloating, SimplePos, VerticalPosition } from "../floating";
import { Graphic } from "../inline/graphic"; import { Graphic } from "../inline/graphic";
import { TextWrapStyle, WrapNone, WrapSquare, WrapTight, WrapTopAndBottom } from "../text-wrap"; import { TextWrappingType, WrapNone, WrapSquare, WrapTight, WrapTopAndBottom } from "../text-wrap";
import { DocProperties } from "./../doc-properties/doc-properties"; import { DocProperties } from "./../doc-properties/doc-properties";
import { EffectExtent } from "./../effect-extent/effect-extent"; import { EffectExtent } from "./../effect-extent/effect-extent";
import { Extent } from "./../extent/extent"; import { Extent } from "./../extent/extent";
@ -21,19 +21,26 @@ const defaultOptions: IFloating = {
}; };
export class Anchor extends XmlComponent { export class Anchor extends XmlComponent {
constructor(referenceId: number, dimensions: IMediaDataDimensions, drawingOptions: IDrawingOptions) { constructor(mediaData: IMediaData, dimensions: IMediaDataDimensions, drawingOptions: IDrawingOptions) {
super("wp:anchor"); super("wp:anchor");
const floating = { const floating = {
margins: {
top: 0,
bottom: 0,
left: 0,
right: 0,
},
...defaultOptions, ...defaultOptions,
...drawingOptions.floating, ...drawingOptions.floating,
}; };
this.root.push( this.root.push(
new AnchorAttributes({ new AnchorAttributes({
distT: 0, distT: floating.margins.top || 0,
distB: 0, distB: floating.margins.bottom || 0,
distL: 0, distL: floating.margins.left || 0,
distR: 0, distR: floating.margins.right || 0,
simplePos: "0", // note: word doesn't fully support - so we use 0 simplePos: "0", // note: word doesn't fully support - so we use 0
allowOverlap: floating.allowOverlap === true ? "1" : "0", allowOverlap: floating.allowOverlap === true ? "1" : "0",
behindDoc: floating.behindDocument === true ? "1" : "0", behindDoc: floating.behindDocument === true ? "1" : "0",
@ -49,18 +56,18 @@ export class Anchor extends XmlComponent {
this.root.push(new Extent(dimensions.emus.x, dimensions.emus.y)); this.root.push(new Extent(dimensions.emus.x, dimensions.emus.y));
this.root.push(new EffectExtent()); this.root.push(new EffectExtent());
if (drawingOptions.textWrapping !== undefined) { if (drawingOptions.floating !== undefined && drawingOptions.floating.wrap !== undefined) {
switch (drawingOptions.textWrapping.textWrapStyle) { switch (drawingOptions.floating.wrap.type) {
case TextWrapStyle.SQUARE: case TextWrappingType.SQUARE:
this.root.push(new WrapSquare(drawingOptions.textWrapping)); this.root.push(new WrapSquare(drawingOptions.floating.wrap, drawingOptions.floating.margins));
break; break;
case TextWrapStyle.TIGHT: case TextWrappingType.TIGHT:
this.root.push(new WrapTight(drawingOptions.textWrapping.distanceFromText)); this.root.push(new WrapTight(drawingOptions.floating.margins));
break; break;
case TextWrapStyle.TOP_AND_BOTTOM: case TextWrappingType.TOP_AND_BOTTOM:
this.root.push(new WrapTopAndBottom(drawingOptions.textWrapping.distanceFromText)); this.root.push(new WrapTopAndBottom(drawingOptions.floating.margins));
break; break;
case TextWrapStyle.NONE: case TextWrappingType.NONE:
default: default:
this.root.push(new WrapNone()); this.root.push(new WrapNone());
} }
@ -70,6 +77,6 @@ export class Anchor extends XmlComponent {
this.root.push(new DocProperties()); this.root.push(new DocProperties());
this.root.push(new GraphicFrameProperties()); this.root.push(new GraphicFrameProperties());
this.root.push(new Graphic(referenceId, dimensions.emus.x, dimensions.emus.y)); this.root.push(new Graphic(mediaData, dimensions.emus.x, dimensions.emus.y));
} }
} }

View File

@ -1,6 +1,6 @@
import { assert } from "chai"; import { expect } from "chai";
import { Utility } from "tests/utility"; import { Formatter } from "export/formatter";
import { Drawing, IDrawingOptions } from "./drawing"; import { Drawing, IDrawingOptions } from "./drawing";
@ -11,7 +11,6 @@ function createDrawing(drawingOptions?: IDrawingOptions): Drawing {
return new Drawing( return new Drawing(
{ {
fileName: "test.jpg", fileName: "test.jpg",
referenceId: 1,
stream: Buffer.from(imageBase64Data, "base64"), stream: Buffer.from(imageBase64Data, "base64"),
path: path, path: path,
dimensions: { dimensions: {
@ -33,16 +32,181 @@ describe("Drawing", () => {
let currentBreak: Drawing; let currentBreak: Drawing;
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create a Drawing with correct root key", () => {
currentBreak = createDrawing();
const newJson = Utility.jsonify(currentBreak);
assert.equal(newJson.rootKey, "w:drawing");
});
it("should create a drawing with inline element when there are no options passed", () => { it("should create a drawing with inline element when there are no options passed", () => {
currentBreak = createDrawing(); currentBreak = createDrawing();
const newJson = Utility.jsonify(currentBreak); const tree = new Formatter().format(currentBreak);
assert.equal(newJson.root[0].rootKey, "wp:inline"); expect(tree).to.deep.equal({
"w:drawing": [
{
"wp:inline": [
{
_attr: {
distB: 0,
distL: 0,
distR: 0,
distT: 0,
},
},
{
"wp:extent": {
_attr: {
cx: 952500,
cy: 952500,
},
},
},
{
"wp:effectExtent": {
_attr: {
b: 0,
l: 0,
r: 0,
t: 0,
},
},
},
{
"wp:docPr": {
_attr: {
descr: "",
id: 0,
name: "",
},
},
},
{
"wp:cNvGraphicFramePr": [
{
"a:graphicFrameLocks": {
_attr: {
// tslint:disable-next-line:object-literal-key-quotes
noChangeAspect: 1,
"xmlns:a": "http://schemas.openxmlformats.org/drawingml/2006/main",
},
},
},
],
},
{
"a:graphic": [
{
_attr: {
"xmlns:a": "http://schemas.openxmlformats.org/drawingml/2006/main",
},
},
{
"a:graphicData": [
{
_attr: {
uri: "http://schemas.openxmlformats.org/drawingml/2006/picture",
},
},
{
"pic:pic": [
{
_attr: {
"xmlns:pic": "http://schemas.openxmlformats.org/drawingml/2006/picture",
},
},
{
"pic:nvPicPr": [
{
"pic:cNvPr": {
_attr: {
desc: "",
id: 0,
name: "",
},
},
},
{
"pic:cNvPicPr": [
{
"a:picLocks": {
_attr: {
noChangeArrowheads: 1,
noChangeAspect: 1,
},
},
},
],
},
],
},
{
"pic:blipFill": [
{
"a:blip": {
_attr: {
// tslint:disable-next-line:object-literal-key-quotes
cstate: "none",
"r:embed": "rId{test.jpg}",
},
},
},
{
"a:srcRect": {},
},
{
"a:stretch": [
{
"a:fillRect": {},
},
],
},
],
},
{
"pic:spPr": [
{
_attr: {
bwMode: "auto",
},
},
{
"a:xfrm": [
{
"a:ext": {
_attr: {
cx: 952500,
cy: 952500,
},
},
},
{
"a:off": {
_attr: {
x: 0,
y: 0,
},
},
},
],
},
{
"a:prstGeom": [
{
_attr: {
prst: "rect",
},
},
{
"a:avLst": {},
},
],
},
],
},
],
},
],
},
],
},
],
},
],
});
}); });
it("should create a drawing with anchor element when there options are passed", () => { it("should create a drawing with anchor element when there options are passed", () => {
@ -56,8 +220,220 @@ describe("Drawing", () => {
}, },
}, },
}); });
const newJson = Utility.jsonify(currentBreak); const tree = new Formatter().format(currentBreak);
assert.equal(newJson.root[0].rootKey, "wp:anchor"); expect(tree).to.deep.equal({
"w:drawing": [
{
"wp:anchor": [
{
_attr: {
allowOverlap: "1",
behindDoc: "0",
distB: 0,
distL: 0,
distR: 0,
distT: 0,
layoutInCell: "1",
locked: "0",
relativeHeight: 952500,
simplePos: "0",
},
},
{
"wp:simplePos": {
_attr: {
x: 0,
y: 0,
},
},
},
{
"wp:positionH": [
{
_attr: {
relativeFrom: "page",
},
},
{
"wp:posOffset": ["0"],
},
],
},
{
"wp:positionV": [
{
_attr: {
relativeFrom: "page",
},
},
{
"wp:posOffset": ["0"],
},
],
},
{
"wp:extent": {
_attr: {
cx: 952500,
cy: 952500,
},
},
},
{
"wp:effectExtent": {
_attr: {
b: 0,
l: 0,
r: 0,
t: 0,
},
},
},
{
"wp:wrapNone": {},
},
{
"wp:docPr": {
_attr: {
descr: "",
id: 0,
name: "",
},
},
},
{
"wp:cNvGraphicFramePr": [
{
"a:graphicFrameLocks": {
_attr: {
// tslint:disable-next-line:object-literal-key-quotes
noChangeAspect: 1,
"xmlns:a": "http://schemas.openxmlformats.org/drawingml/2006/main",
},
},
},
],
},
{
"a:graphic": [
{
_attr: {
"xmlns:a": "http://schemas.openxmlformats.org/drawingml/2006/main",
},
},
{
"a:graphicData": [
{
_attr: {
uri: "http://schemas.openxmlformats.org/drawingml/2006/picture",
},
},
{
"pic:pic": [
{
_attr: {
"xmlns:pic": "http://schemas.openxmlformats.org/drawingml/2006/picture",
},
},
{
"pic:nvPicPr": [
{
"pic:cNvPr": {
_attr: {
desc: "",
id: 0,
name: "",
},
},
},
{
"pic:cNvPicPr": [
{
"a:picLocks": {
_attr: {
noChangeArrowheads: 1,
noChangeAspect: 1,
},
},
},
],
},
],
},
{
"pic:blipFill": [
{
"a:blip": {
_attr: {
// tslint:disable-next-line:object-literal-key-quotes
cstate: "none",
"r:embed": "rId{test.jpg}",
},
},
},
{
"a:srcRect": {},
},
{
"a:stretch": [
{
"a:fillRect": {},
},
],
},
],
},
{
"pic:spPr": [
{
_attr: {
bwMode: "auto",
},
},
{
"a:xfrm": [
{
"a:ext": {
_attr: {
cx: 952500,
cy: 952500,
},
},
},
{
"a:off": {
_attr: {
x: 0,
y: 0,
},
},
},
],
},
{
"a:prstGeom": [
{
_attr: {
prst: "rect",
},
},
{
"a:avLst": {},
},
],
},
],
},
],
},
],
},
],
},
],
},
],
});
}); });
}); });
}); });

View File

@ -3,7 +3,6 @@ import { XmlComponent } from "file/xml-components";
import { Anchor } from "./anchor"; import { Anchor } from "./anchor";
import { IFloating } from "./floating"; import { IFloating } from "./floating";
import { Inline } from "./inline"; import { Inline } from "./inline";
import { ITextWrapping } from "./text-wrap";
export interface IDistance { export interface IDistance {
readonly distT?: number; readonly distT?: number;
@ -13,7 +12,6 @@ export interface IDistance {
} }
export interface IDrawingOptions { export interface IDrawingOptions {
readonly textWrapping?: ITextWrapping;
readonly floating?: IFloating; readonly floating?: IFloating;
} }
@ -23,15 +21,11 @@ export class Drawing extends XmlComponent {
constructor(imageData: IMediaData, drawingOptions: IDrawingOptions = {}) { constructor(imageData: IMediaData, drawingOptions: IDrawingOptions = {}) {
super("w:drawing"); super("w:drawing");
if (imageData === undefined) {
throw new Error("imageData cannot be undefined");
}
if (!drawingOptions.floating) { if (!drawingOptions.floating) {
this.inline = new Inline(imageData.referenceId, imageData.dimensions); this.inline = new Inline(imageData, imageData.dimensions);
this.root.push(this.inline); this.root.push(this.inline);
} else { } else {
this.root.push(new Anchor(imageData.referenceId, imageData.dimensions, drawingOptions)); this.root.push(new Anchor(imageData, imageData.dimensions, drawingOptions));
} }
} }

View File

@ -1,6 +1,6 @@
import { assert } from "chai"; import { expect } from "chai";
import { Utility } from "tests/utility"; import { Formatter } from "export/formatter";
import { Align } from "./align"; import { Align } from "./align";
import { VerticalPositionAlign } from "./floating-position"; import { VerticalPositionAlign } from "./floating-position";
@ -8,9 +8,10 @@ import { VerticalPositionAlign } from "./floating-position";
describe("Align", () => { describe("Align", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create a element with correct root key", () => { it("should create a element with correct root key", () => {
const newJson = Utility.jsonify(new Align(VerticalPositionAlign.CENTER)); const tree = new Formatter().format(new Align(VerticalPositionAlign.CENTER));
assert.equal(newJson.rootKey, "wp:align"); expect(tree).to.deep.equal({
assert.include(newJson.root[0], VerticalPositionAlign.CENTER); "wp:align": ["center"],
});
}); });
}); });
}); });

View File

@ -1,4 +1,5 @@
// http://officeopenxml.com/drwPicFloating-position.php // http://officeopenxml.com/drwPicFloating-position.php
import { ITextWrapping } from "../text-wrap";
export enum HorizontalPositionRelativeFrom { export enum HorizontalPositionRelativeFrom {
CHARACTER = "character", CHARACTER = "character",
@ -50,6 +51,13 @@ export interface IVerticalPositionOptions {
readonly offset?: number; readonly offset?: number;
} }
export interface IMargins {
readonly left?: number;
readonly bottom?: number;
readonly top?: number;
readonly right?: number;
}
export interface IFloating { export interface IFloating {
readonly horizontalPosition: IHorizontalPositionOptions; readonly horizontalPosition: IHorizontalPositionOptions;
readonly verticalPosition: IVerticalPositionOptions; readonly verticalPosition: IVerticalPositionOptions;
@ -57,4 +65,6 @@ export interface IFloating {
readonly lockAnchor?: boolean; readonly lockAnchor?: boolean;
readonly behindDocument?: boolean; readonly behindDocument?: boolean;
readonly layoutInCell?: boolean; readonly layoutInCell?: boolean;
readonly margins?: IMargins;
readonly wrap?: ITextWrapping;
} }

View File

@ -1,6 +1,6 @@
import { assert } from "chai"; import { expect } from "chai";
import { Utility } from "tests/utility"; import { Formatter } from "export/formatter";
import { HorizontalPositionAlign, HorizontalPositionRelativeFrom } from "./floating-position"; import { HorizontalPositionAlign, HorizontalPositionRelativeFrom } from "./floating-position";
import { HorizontalPosition } from "./horizontal-position"; import { HorizontalPosition } from "./horizontal-position";
@ -8,35 +8,45 @@ import { HorizontalPosition } from "./horizontal-position";
describe("HorizontalPosition", () => { describe("HorizontalPosition", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create a element with position align", () => { it("should create a element with position align", () => {
const newJson = Utility.jsonify( const tree = new Formatter().format(
new HorizontalPosition({ new HorizontalPosition({
relative: HorizontalPositionRelativeFrom.MARGIN, relative: HorizontalPositionRelativeFrom.MARGIN,
align: HorizontalPositionAlign.CENTER, align: HorizontalPositionAlign.CENTER,
}), }),
); );
assert.equal(newJson.rootKey, "wp:positionH"); expect(tree).to.deep.equal({
assert.include(newJson.root[0].root, { "wp:positionH": [
{
_attr: {
relativeFrom: "margin", relativeFrom: "margin",
},
},
{
"wp:align": ["center"],
},
],
}); });
assert.equal(newJson.root[1].rootKey, "wp:align");
assert.include(newJson.root[1].root, "center");
}); });
it("should create a element with offset", () => { it("should create a element with offset", () => {
const newJson = Utility.jsonify( const tree = new Formatter().format(
new HorizontalPosition({ new HorizontalPosition({
relative: HorizontalPositionRelativeFrom.MARGIN, relative: HorizontalPositionRelativeFrom.MARGIN,
offset: 40, offset: 40,
}), }),
); );
assert.equal(newJson.rootKey, "wp:positionH"); expect(tree).to.deep.equal({
assert.include(newJson.root[0].root, { "wp:positionH": [
{
_attr: {
relativeFrom: "margin", relativeFrom: "margin",
}); },
},
assert.equal(newJson.root[1].rootKey, "wp:posOffset"); {
assert.include(newJson.root[1].root[0], 40); "wp:posOffset": ["40"],
},
],
});
}); });
}); });
}); });

View File

@ -1,15 +1,16 @@
import { assert } from "chai"; import { expect } from "chai";
import { Utility } from "tests/utility"; import { Formatter } from "export/formatter";
import { PositionOffset } from "./position-offset"; import { PositionOffset } from "./position-offset";
describe("PositionOffset", () => { describe("PositionOffset", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create a element with correct root key", () => { it("should create a element with correct root key", () => {
const newJson = Utility.jsonify(new PositionOffset(50)); const tree = new Formatter().format(new PositionOffset(50));
assert.equal(newJson.rootKey, "wp:posOffset"); expect(tree).to.deep.equal({
assert.equal(newJson.root[0], 50); "wp:posOffset": ["50"],
});
}); });
}); });
}); });

View File

@ -1,17 +1,20 @@
import { assert } from "chai"; import { expect } from "chai";
import { Utility } from "tests/utility"; import { Formatter } from "export/formatter";
import { SimplePos } from "./simple-pos"; import { SimplePos } from "./simple-pos";
describe("SimplePos", () => { describe("SimplePos", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create a element with correct root key", () => { it("should create a element with correct root key", () => {
const newJson = Utility.jsonify(new SimplePos()); const tree = new Formatter().format(new SimplePos());
assert.equal(newJson.rootKey, "wp:simplePos"); expect(tree).to.deep.equal({
assert.include(newJson.root[0].root, { "wp:simplePos": {
_attr: {
x: 0, x: 0,
y: 0, y: 0,
},
},
}); });
}); });
}); });

View File

@ -1,6 +1,6 @@
import { assert } from "chai"; import { expect } from "chai";
import { Utility } from "tests/utility"; import { Formatter } from "export/formatter";
import { VerticalPositionAlign, VerticalPositionRelativeFrom } from "./floating-position"; import { VerticalPositionAlign, VerticalPositionRelativeFrom } from "./floating-position";
import { VerticalPosition } from "./vertical-position"; import { VerticalPosition } from "./vertical-position";
@ -8,35 +8,45 @@ import { VerticalPosition } from "./vertical-position";
describe("VerticalPosition", () => { describe("VerticalPosition", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create a element with position align", () => { it("should create a element with position align", () => {
const newJson = Utility.jsonify( const tree = new Formatter().format(
new VerticalPosition({ new VerticalPosition({
relative: VerticalPositionRelativeFrom.MARGIN, relative: VerticalPositionRelativeFrom.MARGIN,
align: VerticalPositionAlign.INSIDE, align: VerticalPositionAlign.INSIDE,
}), }),
); );
assert.equal(newJson.rootKey, "wp:positionV"); expect(tree).to.deep.equal({
assert.include(newJson.root[0].root, { "wp:positionV": [
{
_attr: {
relativeFrom: "margin", relativeFrom: "margin",
},
},
{
"wp:align": ["inside"],
},
],
}); });
assert.equal(newJson.root[1].rootKey, "wp:align");
assert.include(newJson.root[1].root, "inside");
}); });
it("should create a element with offset", () => { it("should create a element with offset", () => {
const newJson = Utility.jsonify( const tree = new Formatter().format(
new VerticalPosition({ new VerticalPosition({
relative: VerticalPositionRelativeFrom.MARGIN, relative: VerticalPositionRelativeFrom.MARGIN,
offset: 40, offset: 40,
}), }),
); );
assert.equal(newJson.rootKey, "wp:positionV"); expect(tree).to.deep.equal({
assert.include(newJson.root[0].root, { "wp:positionV": [
{
_attr: {
relativeFrom: "margin", relativeFrom: "margin",
}); },
},
assert.equal(newJson.root[1].rootKey, "wp:posOffset"); {
assert.include(newJson.root[1].root[0], 40); "wp:posOffset": ["40"],
},
],
});
}); });
}); });
}); });

View File

@ -1,11 +1,13 @@
import { IMediaData } from "file/media";
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { GraphicDataAttributes } from "./graphic-data-attribute"; import { GraphicDataAttributes } from "./graphic-data-attribute";
import { Pic } from "./pic"; import { Pic } from "./pic";
export class GraphicData extends XmlComponent { export class GraphicData extends XmlComponent {
private readonly pic: Pic; private readonly pic: Pic;
constructor(referenceId: number, x: number, y: number) { constructor(mediaData: IMediaData, x: number, y: number) {
super("a:graphicData"); super("a:graphicData");
this.root.push( this.root.push(
@ -14,7 +16,7 @@ export class GraphicData extends XmlComponent {
}), }),
); );
this.pic = new Pic(referenceId, x, y); this.pic = new Pic(mediaData, x, y);
this.root.push(this.pic); this.root.push(this.pic);
} }

View File

@ -1,12 +1,15 @@
import { IMediaData } from "file/media";
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { Blip } from "./blip"; import { Blip } from "./blip";
import { SourceRectangle } from "./source-rectangle"; import { SourceRectangle } from "./source-rectangle";
import { Stretch } from "./stretch"; import { Stretch } from "./stretch";
export class BlipFill extends XmlComponent { export class BlipFill extends XmlComponent {
constructor(referenceId: number) { constructor(mediaData: IMediaData) {
super("pic:blipFill"); super("pic:blipFill");
this.root.push(new Blip(referenceId));
this.root.push(new Blip(mediaData));
this.root.push(new SourceRectangle()); this.root.push(new SourceRectangle());
this.root.push(new Stretch()); this.root.push(new Stretch());
} }

View File

@ -1,3 +1,4 @@
import { IMediaData } from "file/media";
import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
interface IBlipProperties { interface IBlipProperties {
@ -13,11 +14,11 @@ class BlipAttributes extends XmlAttributeComponent<IBlipProperties> {
} }
export class Blip extends XmlComponent { export class Blip extends XmlComponent {
constructor(referenceId: number) { constructor(mediaData: IMediaData) {
super("a:blip"); super("a:blip");
this.root.push( this.root.push(
new BlipAttributes({ new BlipAttributes({
embed: `rId${referenceId}`, embed: `rId{${mediaData.fileName}}`,
cstate: "none", cstate: "none",
}), }),
); );

View File

@ -1,5 +1,7 @@
// http://officeopenxml.com/drwPic.php // http://officeopenxml.com/drwPic.php
import { IMediaData } from "file/media";
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { BlipFill } from "./blip/blip-fill"; import { BlipFill } from "./blip/blip-fill";
import { NonVisualPicProperties } from "./non-visual-pic-properties/non-visual-pic-properties"; import { NonVisualPicProperties } from "./non-visual-pic-properties/non-visual-pic-properties";
import { PicAttributes } from "./pic-attributes"; import { PicAttributes } from "./pic-attributes";
@ -8,7 +10,7 @@ import { ShapeProperties } from "./shape-properties/shape-properties";
export class Pic extends XmlComponent { export class Pic extends XmlComponent {
private readonly shapeProperties: ShapeProperties; private readonly shapeProperties: ShapeProperties;
constructor(referenceId: number, x: number, y: number) { constructor(mediaData: IMediaData, x: number, y: number) {
super("pic:pic"); super("pic:pic");
this.root.push( this.root.push(
@ -20,7 +22,7 @@ export class Pic extends XmlComponent {
this.shapeProperties = new ShapeProperties(x, y); this.shapeProperties = new ShapeProperties(x, y);
this.root.push(new NonVisualPicProperties()); this.root.push(new NonVisualPicProperties());
this.root.push(new BlipFill(referenceId)); this.root.push(new BlipFill(mediaData));
this.root.push(new ShapeProperties(x, y)); this.root.push(new ShapeProperties(x, y));
} }

View File

@ -1,4 +1,6 @@
import { IMediaData } from "file/media";
import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
import { GraphicData } from "./graphic-data"; import { GraphicData } from "./graphic-data";
interface IGraphicProperties { interface IGraphicProperties {
@ -14,7 +16,7 @@ class GraphicAttributes extends XmlAttributeComponent<IGraphicProperties> {
export class Graphic extends XmlComponent { export class Graphic extends XmlComponent {
private readonly data: GraphicData; private readonly data: GraphicData;
constructor(referenceId: number, x: number, y: number) { constructor(mediaData: IMediaData, x: number, y: number) {
super("a:graphic"); super("a:graphic");
this.root.push( this.root.push(
new GraphicAttributes({ new GraphicAttributes({
@ -22,7 +24,7 @@ export class Graphic extends XmlComponent {
}), }),
); );
this.data = new GraphicData(referenceId, x, y); this.data = new GraphicData(mediaData, x, y);
this.root.push(this.data); this.root.push(this.data);
} }

View File

@ -1,8 +1,13 @@
import { XmlAttributeComponent } from "file/xml-components"; import { XmlAttributeComponent } from "file/xml-components";
import { IDistance } from "../drawing"; import { IDistance } from "../drawing";
// tslint:disable-next-line:no-empty-interface // distT, distB etc have no effect on inline images, only floating
export interface IInlineAttributes extends IDistance {} export interface IInlineAttributes extends IDistance {
readonly distT?: number;
readonly distB?: number;
readonly distL?: number;
readonly distR?: number;
}
export class InlineAttributes extends XmlAttributeComponent<IInlineAttributes> { export class InlineAttributes extends XmlAttributeComponent<IInlineAttributes> {
protected readonly xmlKeys = { protected readonly xmlKeys = {

View File

@ -1,5 +1,5 @@
// http://officeopenxml.com/drwPicInline.php // http://officeopenxml.com/drwPicInline.php
import { IMediaDataDimensions } from "file/media"; import { IMediaData, IMediaDataDimensions } from "file/media";
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { DocProperties } from "./../doc-properties/doc-properties"; import { DocProperties } from "./../doc-properties/doc-properties";
import { EffectExtent } from "./../effect-extent/effect-extent"; import { EffectExtent } from "./../effect-extent/effect-extent";
@ -12,7 +12,7 @@ export class Inline extends XmlComponent {
private readonly extent: Extent; private readonly extent: Extent;
private readonly graphic: Graphic; private readonly graphic: Graphic;
constructor(referenceId: number, private readonly dimensions: IMediaDataDimensions) { constructor(readonly mediaData: IMediaData, private readonly dimensions: IMediaDataDimensions) {
super("wp:inline"); super("wp:inline");
this.root.push( this.root.push(
@ -25,7 +25,7 @@ export class Inline extends XmlComponent {
); );
this.extent = new Extent(dimensions.emus.x, dimensions.emus.y); this.extent = new Extent(dimensions.emus.x, dimensions.emus.y);
this.graphic = new Graphic(referenceId, dimensions.emus.x, dimensions.emus.y); this.graphic = new Graphic(mediaData, dimensions.emus.x, dimensions.emus.y);
this.root.push(this.extent); this.root.push(this.extent);
this.root.push(new EffectExtent()); this.root.push(new EffectExtent());

View File

@ -1,14 +1,14 @@
// http://officeopenxml.com/drwPicFloating-textWrap.php // http://officeopenxml.com/drwPicFloating-textWrap.php
import { IDistance } from "../drawing"; import { IDistance } from "../drawing";
export enum TextWrapStyle { export enum TextWrappingType {
NONE, NONE,
SQUARE, SQUARE,
TIGHT, TIGHT,
TOP_AND_BOTTOM, TOP_AND_BOTTOM,
} }
export enum WrapTextOption { export enum TextWrappingSide {
BOTH_SIDES = "bothSides", BOTH_SIDES = "bothSides",
LEFT = "left", LEFT = "left",
RIGHT = "right", RIGHT = "right",
@ -16,7 +16,7 @@ export enum WrapTextOption {
} }
export interface ITextWrapping { export interface ITextWrapping {
readonly textWrapStyle: TextWrapStyle; readonly type: TextWrappingType;
readonly wrapTextOption?: WrapTextOption; readonly side?: TextWrappingSide;
readonly distanceFromText?: IDistance; readonly margins?: IDistance;
} }

View File

@ -1,10 +1,15 @@
// http://officeopenxml.com/drwPicFloating-textWrap.php // http://officeopenxml.com/drwPicFloating-textWrap.php
import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
import { ITextWrapping, WrapTextOption } from "."; import { ITextWrapping, TextWrappingSide } from ".";
import { IDistance } from "../drawing"; import { IDistance } from "../drawing";
import { IMargins } from "../floating";
interface IWrapSquareAttributes extends IDistance { interface IWrapSquareAttributes extends IDistance {
readonly wrapText?: WrapTextOption; readonly wrapText?: TextWrappingSide;
readonly distT?: number;
readonly distB?: number;
readonly distL?: number;
readonly distR?: number;
} }
class WrapSquareAttributes extends XmlAttributeComponent<IWrapSquareAttributes> { class WrapSquareAttributes extends XmlAttributeComponent<IWrapSquareAttributes> {
@ -18,13 +23,24 @@ class WrapSquareAttributes extends XmlAttributeComponent<IWrapSquareAttributes>
} }
export class WrapSquare extends XmlComponent { export class WrapSquare extends XmlComponent {
constructor(textWrapping: ITextWrapping) { constructor(
textWrapping: ITextWrapping,
margins: IMargins = {
top: 0,
bottom: 0,
left: 0,
right: 0,
},
) {
super("wp:wrapSquare"); super("wp:wrapSquare");
this.root.push( this.root.push(
new WrapSquareAttributes({ new WrapSquareAttributes({
wrapText: textWrapping.wrapTextOption || WrapTextOption.BOTH_SIDES, wrapText: textWrapping.side || TextWrappingSide.BOTH_SIDES,
...textWrapping.distanceFromText, distT: margins.top,
distB: margins.bottom,
distL: margins.left,
distR: margins.right,
}), }),
); );
} }

View File

@ -1,6 +1,7 @@
// http://officeopenxml.com/drwPicFloating-textWrap.php // http://officeopenxml.com/drwPicFloating-textWrap.php
import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
import { IDistance } from "../drawing";
import { IMargins } from "../floating";
interface IWrapTightAttributes { interface IWrapTightAttributes {
readonly distT?: number; readonly distT?: number;
@ -16,17 +17,17 @@ class WrapTightAttributes extends XmlAttributeComponent<IWrapTightAttributes> {
export class WrapTight extends XmlComponent { export class WrapTight extends XmlComponent {
constructor( constructor(
distanceFromText: IDistance = { margins: IMargins = {
distT: 0, top: 0,
distB: 0, bottom: 0,
}, },
) { ) {
super("wp:wrapTight"); super("wp:wrapTight");
this.root.push( this.root.push(
new WrapTightAttributes({ new WrapTightAttributes({
distT: distanceFromText.distT, distT: margins.top,
distB: distanceFromText.distB, distB: margins.bottom,
}), }),
); );
} }

View File

@ -1,6 +1,7 @@
// http://officeopenxml.com/drwPicFloating-textWrap.php // http://officeopenxml.com/drwPicFloating-textWrap.php
import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
import { IDistance } from "../drawing";
import { IMargins } from "../floating";
interface IWrapTopAndBottomAttributes { interface IWrapTopAndBottomAttributes {
readonly distT?: number; readonly distT?: number;
@ -16,17 +17,17 @@ class WrapTopAndBottomAttributes extends XmlAttributeComponent<IWrapTopAndBottom
export class WrapTopAndBottom extends XmlComponent { export class WrapTopAndBottom extends XmlComponent {
constructor( constructor(
distanceFromText: IDistance = { margins: IMargins = {
distT: 0, top: 0,
distB: 0, bottom: 0,
}, },
) { ) {
super("wp:wrapTopAndBottom"); super("wp:wrapTopAndBottom");
this.root.push( this.root.push(
new WrapTopAndBottomAttributes({ new WrapTopAndBottomAttributes({
distT: distanceFromText.distT, distT: margins.top,
distB: distanceFromText.distB, distB: margins.bottom,
}), }),
); );
} }

View File

@ -1,12 +1,15 @@
import { expect } from "chai"; import { expect } from "chai";
import * as sinon from "sinon";
import { Formatter } from "export/formatter"; import { Formatter } from "export/formatter";
import { File } from "./file"; import { File } from "./file";
import { Paragraph } from "./paragraph";
import { Table } from "./table";
describe("File", () => { describe("File", () => {
describe("#constructor", () => { describe("#constructor", () => {
it("should create with correct headers", () => { it("should create with correct headers and footers", () => {
const doc = new File(); const doc = new File();
const header = doc.createHeader(); const header = doc.createHeader();
const footer = doc.createFooter(); const footer = doc.createFooter();
@ -22,8 +25,28 @@ describe("File", () => {
const tree = new Formatter().format(doc.Document.Body); const tree = new Formatter().format(doc.Document.Body);
expect(tree["w:body"][1]["w:sectPr"][4]["w:headerReference"][0]._attr["w:type"]).to.equal("default"); expect(tree["w:body"][1]["w:sectPr"][4]["w:headerReference"]._attr["w:type"]).to.equal("default");
expect(tree["w:body"][1]["w:sectPr"][5]["w:footerReference"][0]._attr["w:type"]).to.equal("default"); expect(tree["w:body"][1]["w:sectPr"][5]["w:footerReference"]._attr["w:type"]).to.equal("default");
});
it("should create with first headers and footers", () => {
const doc = new File();
const header = doc.createHeader();
const footer = doc.createFooter();
doc.addSection({
headers: {
first: header,
},
footers: {
first: footer,
},
});
const tree = new Formatter().format(doc.Document.Body);
expect(tree["w:body"][1]["w:sectPr"][4]["w:headerReference"]._attr["w:type"]).to.equal("first");
expect(tree["w:body"][1]["w:sectPr"][5]["w:footerReference"]._attr["w:type"]).to.equal("first");
}); });
it("should create with correct headers", () => { it("should create with correct headers", () => {
@ -46,13 +69,105 @@ describe("File", () => {
const tree = new Formatter().format(doc.Document.Body); const tree = new Formatter().format(doc.Document.Body);
expect(tree["w:body"][1]["w:sectPr"][4]["w:headerReference"][0]._attr["w:type"]).to.equal("default"); expect(tree["w:body"][1]["w:sectPr"][4]["w:headerReference"]._attr["w:type"]).to.equal("default");
expect(tree["w:body"][1]["w:sectPr"][5]["w:headerReference"][0]._attr["w:type"]).to.equal("first"); expect(tree["w:body"][1]["w:sectPr"][5]["w:headerReference"]._attr["w:type"]).to.equal("first");
expect(tree["w:body"][1]["w:sectPr"][6]["w:headerReference"][0]._attr["w:type"]).to.equal("even"); expect(tree["w:body"][1]["w:sectPr"][6]["w:headerReference"]._attr["w:type"]).to.equal("even");
expect(tree["w:body"][1]["w:sectPr"][7]["w:footerReference"][0]._attr["w:type"]).to.equal("default"); expect(tree["w:body"][1]["w:sectPr"][7]["w:footerReference"]._attr["w:type"]).to.equal("default");
expect(tree["w:body"][1]["w:sectPr"][8]["w:footerReference"][0]._attr["w:type"]).to.equal("first"); expect(tree["w:body"][1]["w:sectPr"][8]["w:footerReference"]._attr["w:type"]).to.equal("first");
expect(tree["w:body"][1]["w:sectPr"][9]["w:footerReference"][0]._attr["w:type"]).to.equal("even"); expect(tree["w:body"][1]["w:sectPr"][9]["w:footerReference"]._attr["w:type"]).to.equal("even");
});
});
describe("#addParagraph", () => {
it("should call the underlying document's addParagraph", () => {
const file = new File();
const spy = sinon.spy(file.Document, "addParagraph");
file.addParagraph(new Paragraph());
expect(spy.called).to.equal(true);
});
});
describe("#addTable", () => {
it("should call the underlying document's addTable", () => {
const wrapper = new File();
const spy = sinon.spy(wrapper.Document, "addTable");
wrapper.addTable(
new Table({
rows: 1,
columns: 1,
}),
);
expect(spy.called).to.equal(true);
});
});
describe("#createTable", () => {
it("should call the underlying document's createTable", () => {
const wrapper = new File();
const spy = sinon.spy(wrapper.Document, "createTable");
wrapper.createTable({
rows: 1,
columns: 1,
});
expect(spy.called).to.equal(true);
});
});
describe("#addTableOfContents", () => {
it("should call the underlying document's addTableOfContents", () => {
const wrapper = new File();
const spy = sinon.spy(wrapper.Document, "addTableOfContents");
// tslint:disable-next-line:no-any
wrapper.addTableOfContents({} as any);
expect(spy.called).to.equal(true);
});
});
describe("#createParagraph", () => {
it("should call the underlying document's createParagraph", () => {
const wrapper = new File();
const spy = sinon.spy(wrapper.Document, "createParagraph");
wrapper.createParagraph("test");
expect(spy.called).to.equal(true);
});
});
describe("#addImage", () => {
it("should call the underlying document's addImage", () => {
const wrapper = new File();
const spy = sinon.spy(wrapper.Document, "addParagraph");
// tslint:disable-next-line:no-any
wrapper.addImage({} as any);
expect(spy.called).to.equal(true);
});
});
describe("#createImage", () => {
it("should call the underlying document's createImage", () => {
const wrapper = new File();
const spy = sinon.spy(wrapper.Media, "addMedia");
const wrapperSpy = sinon.spy(wrapper.Document, "addParagraph");
wrapper.createImage("");
expect(spy.called).to.equal(true);
expect(wrapperSpy.called).to.equal(true);
});
});
describe("#createFootnote", () => {
it("should call the underlying document's createFootnote", () => {
const wrapper = new File();
const spy = sinon.spy(wrapper.FootNotes, "createFootNote");
wrapper.createFootnote(new Paragraph(""));
expect(spy.called).to.equal(true);
}); });
}); });
}); });

View File

@ -3,6 +3,7 @@ import { ContentTypes } from "./content-types/content-types";
import { CoreProperties, IPropertiesOptions } from "./core-properties"; import { CoreProperties, IPropertiesOptions } from "./core-properties";
import { Document } from "./document"; import { Document } from "./document";
import { import {
FooterReference,
FooterReferenceType, FooterReferenceType,
HeaderReference, HeaderReference,
HeaderReferenceType, HeaderReferenceType,
@ -18,11 +19,12 @@ import { Image, Media } from "./media";
import { Numbering } from "./numbering"; import { Numbering } from "./numbering";
import { Bookmark, Hyperlink, Paragraph } from "./paragraph"; import { Bookmark, Hyperlink, Paragraph } from "./paragraph";
import { Relationships } from "./relationships"; import { Relationships } from "./relationships";
import { TargetModeType } from "./relationships/relationship/relationship";
import { Settings } from "./settings"; import { Settings } from "./settings";
import { Styles } from "./styles"; import { Styles } from "./styles";
import { ExternalStylesFactory } from "./styles/external-styles-factory"; import { ExternalStylesFactory } from "./styles/external-styles-factory";
import { DefaultStylesFactory } from "./styles/factory"; import { DefaultStylesFactory } from "./styles/factory";
import { Table } from "./table"; import { ITableOptions, Table } from "./table";
import { TableOfContents } from "./table-of-contents"; import { TableOfContents } from "./table-of-contents";
export class File { export class File {
@ -56,12 +58,13 @@ export class File {
this.coreProperties = new CoreProperties(options); this.coreProperties = new CoreProperties(options);
this.numbering = new Numbering(); this.numbering = new Numbering();
this.docRelationships = new Relationships(); this.docRelationships = new Relationships();
this.media = new Media();
this.fileRelationships = new Relationships(); this.fileRelationships = new Relationships();
this.appProperties = new AppProperties(); this.appProperties = new AppProperties();
this.footNotes = new FootNotes(); this.footNotes = new FootNotes();
this.contentTypes = new ContentTypes(); this.contentTypes = new ContentTypes();
this.media = fileProperties.template && fileProperties.template.media ? fileProperties.template.media : new Media();
if (fileProperties.template) { if (fileProperties.template) {
this.currentRelationshipId = fileProperties.template.currentRelationshipId + 1; this.currentRelationshipId = fileProperties.template.currentRelationshipId + 1;
} }
@ -71,7 +74,8 @@ export class File {
throw Error("can not use both template and external styles"); throw Error("can not use both template and external styles");
} }
if (fileProperties.template) { if (fileProperties.template) {
this.styles = fileProperties.template.styles; const stylesFactory = new ExternalStylesFactory();
this.styles = stylesFactory.newInstance(fileProperties.template.styles);
} else if (options.externalStyles) { } else if (options.externalStyles) {
const stylesFactory = new ExternalStylesFactory(); const stylesFactory = new ExternalStylesFactory();
this.styles = stylesFactory.newInstance(options.externalStyles); this.styles = stylesFactory.newInstance(options.externalStyles);
@ -108,8 +112,9 @@ export class File {
this.settings = new Settings(); this.settings = new Settings();
} }
public addTableOfContents(toc: TableOfContents): void { public addTableOfContents(toc: TableOfContents): File {
this.document.addTableOfContents(toc); this.document.addTableOfContents(toc);
return this;
} }
public addParagraph(paragraph: Paragraph): File { public addParagraph(paragraph: Paragraph): File {
@ -121,12 +126,13 @@ export class File {
return this.document.createParagraph(text); return this.document.createParagraph(text);
} }
public addTable(table: Table): void { public addTable(table: Table): File {
return this.document.addTable(table); this.document.addTable(table);
return this;
} }
public createTable(rows: number, cols: number): Table { public createTable(options: ITableOptions): Table {
return this.document.createTable(rows, cols); return this.document.createTable(options);
} }
public addImage(image: Image): File { public addImage(image: Image): File {
@ -153,7 +159,7 @@ export class File {
hyperlink.linkId, hyperlink.linkId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
link, link,
"External", TargetModeType.EXTERNAL,
); );
return hyperlink; return hyperlink;
} }
@ -218,6 +224,32 @@ export class File {
return headerWrapper; return headerWrapper;
} }
public createFirstPageFooter(): FooterWrapper {
const footerWrapper = this.createFooter();
this.document.Body.DefaultSection.addChildElement(
new FooterReference({
footerType: FooterReferenceType.FIRST,
footerId: footerWrapper.Footer.ReferenceId,
}),
);
return footerWrapper;
}
public createEvenPageFooter(): FooterWrapper {
const footerWrapper = this.createFooter();
this.document.Body.DefaultSection.addChildElement(
new FooterReference({
footerType: FooterReferenceType.EVEN,
footerId: footerWrapper.Footer.ReferenceId,
}),
);
return footerWrapper;
}
public getFooterByReferenceNumber(refId: number): FooterWrapper { public getFooterByReferenceNumber(refId: number): FooterWrapper {
const entry = this.footers.map((item) => item.footer).find((h) => h.Footer.ReferenceId === refId); const entry = this.footers.map((item) => item.footer).find((h) => h.Footer.ReferenceId === refId);
if (entry) { if (entry) {
@ -292,6 +324,11 @@ export class File {
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes",
"footnotes.xml", "footnotes.xml",
); );
this.docRelationships.createRelationship(
this.currentRelationshipId++,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings",
"settings.xml",
);
} }
private groupHeaders(headers: IDocumentHeader[], group: IHeaderFooterGroup<HeaderWrapper> = {}): IHeaderFooterGroup<HeaderWrapper> { private groupHeaders(headers: IDocumentHeader[], group: IHeaderFooterGroup<HeaderWrapper> = {}): IHeaderFooterGroup<HeaderWrapper> {
@ -299,12 +336,6 @@ export class File {
for (const header of headers) { for (const header of headers) {
switch (header.type) { switch (header.type) {
case HeaderReferenceType.DEFAULT:
newGroup = {
...newGroup,
default: header.header,
};
break;
case HeaderReferenceType.FIRST: case HeaderReferenceType.FIRST:
newGroup = { newGroup = {
...newGroup, ...newGroup,
@ -317,6 +348,7 @@ export class File {
even: header.header, even: header.header,
}; };
break; break;
case HeaderReferenceType.DEFAULT:
default: default:
newGroup = { newGroup = {
...newGroup, ...newGroup,
@ -334,12 +366,6 @@ export class File {
for (const footer of footers) { for (const footer of footers) {
switch (footer.type) { switch (footer.type) {
case FooterReferenceType.DEFAULT:
newGroup = {
...newGroup,
default: footer.footer,
};
break;
case FooterReferenceType.FIRST: case FooterReferenceType.FIRST:
newGroup = { newGroup = {
...newGroup, ...newGroup,
@ -352,6 +378,7 @@ export class File {
even: footer.footer, even: footer.footer,
}; };
break; break;
case FooterReferenceType.DEFAULT:
default: default:
newGroup = { newGroup = {
...newGroup, ...newGroup,

View File

@ -0,0 +1,88 @@
import { expect } from "chai";
import * as sinon from "sinon";
import { FooterWrapper } from "./footer-wrapper";
import { Media } from "./media";
import { Paragraph } from "./paragraph";
import { Table } from "./table";
describe("FooterWrapper", () => {
describe("#addParagraph", () => {
it("should call the underlying footer's addParagraph", () => {
const file = new FooterWrapper(new Media(), 1);
const spy = sinon.spy(file.Footer, "addParagraph");
file.addParagraph(new Paragraph());
expect(spy.called).to.equal(true);
});
});
describe("#addTable", () => {
it("should call the underlying footer's addParagraph", () => {
const file = new FooterWrapper(new Media(), 1);
const spy = sinon.spy(file.Footer, "addTable");
file.addTable(
new Table({
rows: 1,
columns: 1,
}),
);
expect(spy.called).to.equal(true);
});
});
describe("#createTable", () => {
it("should call the underlying footer's createTable", () => {
const wrapper = new FooterWrapper(new Media(), 1);
const spy = sinon.spy(wrapper.Footer, "createTable");
wrapper.createTable(1, 1);
expect(spy.called).to.equal(true);
});
});
describe("#createParagraph", () => {
it("should call the underlying footer's createParagraph", () => {
const file = new FooterWrapper(new Media(), 1);
const spy = sinon.spy(file.Footer, "addParagraph");
file.createParagraph();
expect(spy.called).to.equal(true);
});
});
describe("#addImage", () => {
it("should call the underlying footer's addImage", () => {
const file = new FooterWrapper(new Media(), 1);
const spy = sinon.spy(file.Footer, "addParagraph");
// tslint:disable-next-line:no-any
file.addImage({} as any);
expect(spy.called).to.equal(true);
});
});
describe("#createImage", () => {
it("should call the underlying footer's createImage", () => {
const file = new FooterWrapper(new Media(), 1);
const spy = sinon.spy(file.Media, "addMedia");
const fileSpy = sinon.spy(file, "addImage");
file.createImage("");
expect(spy.called).to.equal(true);
expect(fileSpy.called).to.equal(true);
});
});
describe("#addChildElement", () => {
it("should call the underlying footer's addChildElement", () => {
const file = new FooterWrapper(new Media(), 1);
const spy = sinon.spy(file.Footer, "addChildElement");
// tslint:disable-next-line:no-any
file.addChildElement({} as any);
expect(spy.called).to.equal(true);
});
});
});

View File

@ -2,7 +2,7 @@ import { XmlComponent } from "file/xml-components";
import { FooterReferenceType } from "./document"; import { FooterReferenceType } from "./document";
import { Footer } from "./footer/footer"; import { Footer } from "./footer/footer";
import { Image, IMediaData, Media } from "./media"; import { Image, Media } from "./media";
import { ImageParagraph, Paragraph } from "./paragraph"; import { ImageParagraph, Paragraph } from "./paragraph";
import { Relationships } from "./relationships"; import { Relationships } from "./relationships";
import { Table } from "./table"; import { Table } from "./table";
@ -43,29 +43,8 @@ export class FooterWrapper {
this.footer.addChildElement(childElement); this.footer.addChildElement(childElement);
} }
public addImageRelationship(image: Buffer, refId: number, width?: number, height?: number): IMediaData {
const mediaData = this.media.addMedia(image, refId, width, height);
this.relationships.createRelationship(
mediaData.referenceId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
`media/${mediaData.fileName}`,
);
return mediaData;
}
public addHyperlinkRelationship(target: string, refId: number, targetMode?: "External" | undefined): void {
this.relationships.createRelationship(
refId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
target,
targetMode,
);
}
public createImage(image: Buffer | string | Uint8Array | ArrayBuffer, width?: number, height?: number): void { public createImage(image: Buffer | string | Uint8Array | ArrayBuffer, width?: number, height?: number): void {
// TODO const mediaData = this.media.addMedia(image, width, height);
// tslint:disable-next-line:no-any
const mediaData = this.addImageRelationship(image as any, this.relationships.RelationshipCount, width, height);
this.addImage(new Image(new ImageParagraph(mediaData))); this.addImage(new Image(new ImageParagraph(mediaData)));
} }

View File

@ -53,7 +53,10 @@ export class Footer extends InitializableXmlComponent {
} }
public createTable(rows: number, cols: number): Table { public createTable(rows: number, cols: number): Table {
const table = new Table(rows, cols); const table = new Table({
rows: rows,
columns: cols,
});
this.addTable(table); this.addTable(table);
return table; return table;
} }

View File

@ -11,8 +11,7 @@ describe("Footnote", () => {
const tree = new Formatter().format(footnote); const tree = new Formatter().format(footnote);
expect(Object.keys(tree)).to.deep.equal(["w:footnote"]); expect(Object.keys(tree)).to.deep.equal(["w:footnote"]);
expect(tree["w:footnote"]).to.be.an.instanceof(Array); expect(tree["w:footnote"]).to.deep.equal({ _attr: { "w:type": "separator", "w:id": 1 } });
expect(tree["w:footnote"][0]).to.deep.equal({ _attr: { "w:type": "separator", "w:id": 1 } });
}); });
it("should create a footnote without a footnote type", () => { it("should create a footnote without a footnote type", () => {
@ -20,8 +19,7 @@ describe("Footnote", () => {
const tree = new Formatter().format(footnote); const tree = new Formatter().format(footnote);
expect(Object.keys(tree)).to.deep.equal(["w:footnote"]); expect(Object.keys(tree)).to.deep.equal(["w:footnote"]);
expect(tree["w:footnote"]).to.be.an.instanceof(Array); expect(tree["w:footnote"]).to.deep.equal({ _attr: { "w:id": 1 } });
expect(tree["w:footnote"][0]).to.deep.equal({ _attr: { "w:id": 1 } });
}); });
}); });
}); });

View File

@ -9,19 +9,78 @@ import { Table } from "./table";
describe("HeaderWrapper", () => { describe("HeaderWrapper", () => {
describe("#addParagraph", () => { describe("#addParagraph", () => {
it("should call the underlying header's addParagraph", () => { it("should call the underlying header's addParagraph", () => {
const file = new HeaderWrapper(new Media(), 1); const wrapper = new HeaderWrapper(new Media(), 1);
const spy = sinon.spy(file.Header, "addParagraph"); const spy = sinon.spy(wrapper.Header, "addParagraph");
file.addParagraph(new Paragraph()); wrapper.addParagraph(new Paragraph());
expect(spy.called).to.equal(true); expect(spy.called).to.equal(true);
}); });
}); });
describe("#addTable", () => { describe("#addTable", () => {
it("should call the underlying header's addParagraph", () => { it("should call the underlying header's addTable", () => {
const wrapper = new HeaderWrapper(new Media(), 1);
const spy = sinon.spy(wrapper.Header, "addTable");
wrapper.addTable(
new Table({
rows: 1,
columns: 1,
}),
);
expect(spy.called).to.equal(true);
});
});
describe("#createTable", () => {
it("should call the underlying header's createTable", () => {
const wrapper = new HeaderWrapper(new Media(), 1);
const spy = sinon.spy(wrapper.Header, "createTable");
wrapper.createTable(1, 1);
expect(spy.called).to.equal(true);
});
});
describe("#createParagraph", () => {
it("should call the underlying header's createParagraph", () => {
const file = new HeaderWrapper(new Media(), 1); const file = new HeaderWrapper(new Media(), 1);
const spy = sinon.spy(file.Header, "addTable"); const spy = sinon.spy(file.Header, "addParagraph");
file.addTable(new Table(1, 1)); file.createParagraph();
expect(spy.called).to.equal(true);
});
});
describe("#addImage", () => {
it("should call the underlying header's addImage", () => {
const file = new HeaderWrapper(new Media(), 1);
const spy = sinon.spy(file.Header, "addParagraph");
// tslint:disable-next-line:no-any
file.addImage({} as any);
expect(spy.called).to.equal(true);
});
});
describe("#createImage", () => {
it("should call the underlying header's createImage", () => {
const file = new HeaderWrapper(new Media(), 1);
const spy = sinon.spy(file.Media, "addMedia");
const fileSpy = sinon.spy(file, "addImage");
file.createImage("");
expect(spy.called).to.equal(true);
expect(fileSpy.called).to.equal(true);
});
});
describe("#addChildElement", () => {
it("should call the underlying header's addChildElement", () => {
const file = new HeaderWrapper(new Media(), 1);
const spy = sinon.spy(file.Header, "addChildElement");
// tslint:disable-next-line:no-any
file.addChildElement({} as any);
expect(spy.called).to.equal(true); expect(spy.called).to.equal(true);
}); });

View File

@ -2,7 +2,7 @@ import { XmlComponent } from "file/xml-components";
import { HeaderReferenceType } from "./document"; import { HeaderReferenceType } from "./document";
import { Header } from "./header/header"; import { Header } from "./header/header";
import { Image, IMediaData, Media } from "./media"; import { Image, Media } from "./media";
import { ImageParagraph, Paragraph } from "./paragraph"; import { ImageParagraph, Paragraph } from "./paragraph";
import { Relationships } from "./relationships"; import { Relationships } from "./relationships";
import { Table } from "./table"; import { Table } from "./table";
@ -43,29 +43,8 @@ export class HeaderWrapper {
this.header.addChildElement(childElement); this.header.addChildElement(childElement);
} }
public addImageRelationship(image: Buffer, refId: number, width?: number, height?: number): IMediaData {
const mediaData = this.media.addMedia(image, refId, width, height);
this.relationships.createRelationship(
mediaData.referenceId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
`media/${mediaData.fileName}`,
);
return mediaData;
}
public addHyperlinkRelationship(target: string, refId: number, targetMode?: "External" | undefined): void {
this.relationships.createRelationship(
refId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
target,
targetMode,
);
}
public createImage(image: Buffer | string | Uint8Array | ArrayBuffer, width?: number, height?: number): void { public createImage(image: Buffer | string | Uint8Array | ArrayBuffer, width?: number, height?: number): void {
// TODO const mediaData = this.media.addMedia(image, width, height);
// tslint:disable-next-line:no-any
const mediaData = this.addImageRelationship(image as any, this.relationships.RelationshipCount, width, height);
this.addImage(new Image(new ImageParagraph(mediaData))); this.addImage(new Image(new ImageParagraph(mediaData)));
} }

View File

@ -64,7 +64,10 @@ export class Header extends InitializableXmlComponent {
} }
public createTable(rows: number, cols: number): Table { public createTable(rows: number, cols: number): Table {
const table = new Table(rows, cols); const table = new Table({
rows: rows,
columns: cols,
});
this.addTable(table); this.addTable(table);
return table; return table;
} }

View File

@ -9,3 +9,5 @@ export * from "./document";
export * from "./styles"; export * from "./styles";
export * from "./table-of-contents"; export * from "./table-of-contents";
export * from "./xml-components"; export * from "./xml-components";
export * from "./header-wrapper";
export * from "./footer-wrapper";

Some files were not shown because too many files have changed in this diff Show More