Docx: How can I set the default styling?

Created on 9 Aug 2018  路  7Comments  路  Source: dolanmiu/docx

Hi guys!
Infortunally, there is no documentation in the Wiki for setting the document default styles.
Is it possible to do that? If yes, how can I do it? I will be glad to contribute to improve the documentation for that. If no, I will be glad to make a pull request for that.

Most helpful comment

The documentation says:

There are a few different ways of styling this content in DOCX, which somewhat resemble the HTML/CSS approach. In order of greatest to lowest priority:

  1. Direct formatting (AKA inline formatting)
  2. Centrally defined styles (similar to external CSS)
  3. Document defaults (similar to a * rule in CSS)

I'm talking about item 3. There's no example of how to do that.

All 7 comments

Setting default styles is not documented, but it can be found here:

https://github.com/dolanmiu/docx/blob/master/demo/demo2.js

Or you can do this:

https://docx.js.org/#/usage/styling-with-xml

Is this what you mean?

The documentation says:

There are a few different ways of styling this content in DOCX, which somewhat resemble the HTML/CSS approach. In order of greatest to lowest priority:

  1. Direct formatting (AKA inline formatting)
  2. Centrally defined styles (similar to external CSS)
  3. Document defaults (similar to a * rule in CSS)

I'm talking about item 3. There's no example of how to do that.

AFAIK, it is not implemented yet

Here is the default styles:

https://github.com/dolanmiu/docx/blob/master/src/file/styles/sample/index.ts

And here is where it adds document defaults to the document:

https://github.com/dolanmiu/docx/blob/master/src/file/styles/index.ts#L19-L21

If you can make a PR for number 3, that would be nice

I wasn't part of the development of this feature, so admittedly I don't know much about it

Was the above implemented? (Document defaults. [specifically font & size])
if so can you please explain how to use it?

Hello, I also try to use default styling on my document
No problem with declarative style, but I'd like to set a font family and font-size by default for ALL the paragraphs without applying a class on every paragraphs for the style to be applied...

As @sergiofm I'm trying to implement : Document defaults (similar to a * rule in CSS)...

Answer is really here but it's unclear, IMHO.
I've tried to change different props and found out that name should set to Normal if u want to override default styles.

For example, set default text color to red and make it bold:

const doc = new Document({
    styles: {
      paragraphStyles: [
        {
          // Only `name` prop required, `id` not necessary
          name: 'Normal',
          run: {
            color: '#ff0000',
            bold: true,
        },
      ],
    },
  })

@dolanmiu, pls point this in docs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zahrafali picture zahrafali  路  5Comments

sleepinzombie picture sleepinzombie  路  3Comments

barberousse picture barberousse  路  5Comments

liquidslr picture liquidslr  路  4Comments

iainfreestone picture iainfreestone  路  3Comments