Files
docx-js/docs/examples.md

220 lines
6.4 KiB
Markdown
Raw Normal View History

2018-08-04 03:40:41 +01:00
# Examples
2018-08-05 04:24:04 +01:00
> All examples can run independently and can be found in the `/demo` folder of the project
2018-08-09 03:20:46 +01:00
All the examples below can be ran locally, to do so, run the following command:
```sh
npm run demo
```
This command will run the `demo selector app` in the `/demo` folder. It will prompt you to select a demo number, which will run a demo from that folder.
2018-08-05 04:14:25 +01:00
## Simple
2018-08-05 03:28:51 +01:00
A simple hello world of the `docx` library:
2019-03-06 00:18:00 +00:00
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo1.ts ':include')
2018-08-05 03:28:51 +01:00
2018-08-22 01:14:17 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo1.ts_
2018-08-05 04:14:25 +01:00
## Styles
2018-08-06 02:57:25 +01:00
### Styling with JS
2018-08-05 04:14:25 +01:00
This example shows how to customise the look and feel of a document using JS configuration
2019-03-06 00:18:00 +00:00
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo2.ts ':include')
2018-08-06 02:57:25 +01:00
2018-08-22 01:14:17 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo2.ts_
2018-08-06 02:57:25 +01:00
### Styling with XML
2018-08-05 04:14:25 +01:00
2018-08-06 02:57:25 +01:00
This example shows how to customise the look and feel of a document using XML configuration
2018-08-05 04:14:25 +01:00
2019-03-06 00:18:00 +00:00
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo13.ts ':include')
2018-08-06 02:57:25 +01:00
2018-08-22 01:14:17 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo13.ts_
2018-08-05 04:14:25 +01:00
2018-08-05 04:18:12 +01:00
## Numbering
2018-08-05 04:14:25 +01:00
2018-08-05 04:18:12 +01:00
This example shows many levels of numbering
2018-08-05 04:14:25 +01:00
2019-03-06 00:18:00 +00:00
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo3.ts ':include')
2018-08-05 04:14:25 +01:00
2018-08-22 01:14:17 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo3.ts_
2018-08-05 04:14:25 +01:00
## Table
Example of simple table
2019-03-06 00:18:00 +00:00
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo4.ts ':include')
2018-08-06 02:57:25 +01:00
2018-08-22 01:14:17 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo4.ts_
2018-08-05 04:14:25 +01:00
2018-08-06 02:57:25 +01:00
### Styling table borders
Styling the borders of a table
2019-03-06 00:18:00 +00:00
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo20.ts ':include')
2018-08-06 02:57:25 +01:00
2018-08-22 01:14:17 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo20.ts_
2018-08-05 04:24:04 +01:00
## Images
2018-08-06 02:57:25 +01:00
### Add image to the document
2018-08-05 04:24:04 +01:00
Importing Images from file system path
2019-03-06 00:18:00 +00:00
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo5.ts ':include')
2018-08-06 02:57:25 +01:00
2018-08-22 01:14:17 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo5.ts_
2018-08-06 02:57:25 +01:00
### Add images to header and footer
Example showing how to add image to headers and footers
2019-03-06 00:18:00 +00:00
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo9.ts ':include')
2018-08-06 02:57:25 +01:00
2018-08-22 01:14:17 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo9.ts_
2018-08-06 02:57:25 +01:00
### Scaling images
Example showing how to scale images
2019-03-06 00:18:00 +00:00
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo12.ts ':include')
2018-08-06 02:57:25 +01:00
2018-08-22 01:14:17 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo12.ts_
2018-08-06 02:57:25 +01:00
### Add Image to media before adding to document
2018-08-05 04:24:04 +01:00
2018-08-06 02:57:25 +01:00
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
2019-03-06 00:18:00 +00:00
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo23.ts ':include')
2018-08-06 02:57:25 +01:00
2018-08-22 01:14:17 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo23.ts_
2018-08-06 02:57:25 +01:00
### Add image to table
As before, to add an image to a table, you would need to add it to the `Media` object first
2019-03-06 00:18:00 +00:00
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo24.ts ':include')
2018-08-06 02:57:25 +01:00
2018-08-22 01:14:17 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo24.ts_
2018-08-06 02:57:25 +01:00
### Images using Base64 URI
If you want to use a Base64 image instead
2019-03-06 00:18:00 +00:00
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo18.ts ':include')
2018-08-06 02:57:25 +01:00
2018-08-22 01:14:17 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo18.ts_
2018-08-05 04:24:04 +01:00
## Margins
2019-04-18 13:55:18 +10:00
Example showing how to set custom margins
2018-08-05 04:24:04 +01:00
2019-03-06 00:18:00 +00:00
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo6.ts ':include')
2018-08-05 04:24:04 +01:00
2018-08-22 01:14:17 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo6.ts_
2018-08-05 04:24:04 +01:00
## Orientation
Example showing how to set the document to `landscape` or `portrait`
2019-03-06 00:18:00 +00:00
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo7.ts ':include')
2018-08-06 02:57:25 +01:00
2018-08-22 01:14:17 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo7.ts_
2018-08-06 02:57:25 +01:00
## Headers & Footers
Example showing how to add headers and footers
2019-03-06 00:18:00 +00:00
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo8.ts ':include')
2018-08-06 02:57:25 +01:00
2018-08-22 01:14:17 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo8.ts_
2018-08-06 02:57:25 +01:00
## Multiple headers and footers
Check out `Sections` for this feature
## Page Breaks
### Normal page breaks
Example showing how to page break
2019-03-06 00:18:00 +00:00
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo14.ts ':include')
2018-08-06 02:57:25 +01:00
2018-08-22 01:14:17 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo14.ts_
2018-08-06 02:57:25 +01:00
### Page break before
Example showing how to page break before like in Word
2019-03-06 00:18:00 +00:00
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo15.ts ':include')
2018-08-06 02:57:25 +01:00
2018-08-22 01:14:17 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo15.ts_
2018-08-06 02:57:25 +01:00
## Sections
2018-11-09 09:26:27 -02:00
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.
2018-08-06 02:57:25 +01:00
2019-03-06 00:18:00 +00:00
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo16.ts ':include')
2018-08-06 02:57:25 +01:00
2018-08-22 01:14:17 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo16.ts_
2018-08-06 02:57:25 +01:00
## Footnotes
Example of how to add footnotes. Good for references
2019-03-06 00:18:00 +00:00
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo17.ts ':include')
2018-08-06 02:57:25 +01:00
2018-08-22 01:14:17 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo17.ts_
2018-08-06 02:57:25 +01:00
## Packers
2018-08-22 01:14:17 +01:00
## Buffer output
2018-08-06 02:57:25 +01:00
Example showing how to use the Buffer packer and then write that buffer to the file system
2019-03-06 00:18:00 +00:00
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo19.ts ':include')
2018-08-06 02:57:25 +01:00
2018-08-22 01:14:17 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo19.ts_
2018-08-06 02:57:25 +01:00
2018-08-06 03:53:27 +01:00
2018-08-06 02:57:25 +01:00
## Bookmarks
Example showing how to make bookmarks to make internal hyperlinks within the document
2019-03-06 00:18:00 +00:00
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo21.ts ':include')
2018-08-06 02:57:25 +01:00
2018-08-22 01:14:17 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo21.ts_
2018-08-06 02:57:25 +01:00
## Bidirectional text
Example showing how to use bidirectional text for certain languages such as Hebrew
2019-03-06 00:18:00 +00:00
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo22.ts ':include')
2018-08-06 02:57:25 +01:00
2018-08-22 01:14:17 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo22.ts_
2018-08-06 02:57:25 +01:00
## Showcase
### My CV
Example showing how to add headers and footers
2019-03-06 00:18:00 +00:00
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo10.ts ':include')
2018-08-06 02:57:25 +01:00
2018-08-22 01:14:17 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo10.ts_
2018-08-06 02:57:25 +01:00
### Style and Images
This example shows how to customise the look and feel of a document and add images
2019-03-06 00:18:00 +00:00
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo11.ts ':include')
2018-08-05 04:24:04 +01:00
2018-08-22 01:14:17 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo11.ts_