Cookbook

Good practices when working with font families

When working with Word documents you can choose any font family you want, besides the usual Calilbri or Times New Roman ones.

To see a font properly, it has to be either installed in the operating system or embedded in the document.

Setting fonts in the added contents

javadocx has methods and options to set the font family of the document, either the default one for all contents, which you can set with the setDefaultFont method, or for specific parts of the content with the 'font' option, which is available in the methods to add texts.

For example, let's set Arial Narrow as the default font for all contents of the document. This would be the code:

To assign fonts with the methods of the javadocx API, like texts for the main body, footnotes, endnotes or any other type of content, you must use the font parameter besides its family name. E.g.: the following code adds a text with the Arial font and another one with Times New Roman.

Adding new fonts to a DOCX

If you need to write using a specific font family but you are unsure if it will be available in the computers that will read it, you need to embed the font in the document.

javadocx doesn't include a method to embed fonts, so you need to open MS Word and generate a document with the font embedded in it. Then you can choose that document as a template for javadocx. The embedded fonts must be TrueType or OpenType.

To embed fonts in a DOCX, just enable the MS Word option Embed fonts in the file. E.g.: You can find this option in MS Word 2010 in FILE > Save > Preserve fidelity when sharing this document > Embed fonts in the file.

After saving the document you can open it as a javadocx template:

Now you can apply the new font families just like any other one:

Fonts in the conversion plugin

The conversion plugin, available in the Advanced and Premium licenses, works with the font families available in the operating system. This way, to keep the fonts in the resulting PDF and other compatible formats, they must be installed.

To install a new font in Windows is as easy as to double click in the file that contains the font and then accept the installation.

To install new fonts in Linux, just copy the fonts in the file system directory where the rest of the fonts are, e.g., /usr/share/fonts/truetype. After that, refresh the available fonts by executing:

To see the available fonts list, type the folowing command:

Tip

To use regular MS Word like Arial or Courier, it is advisable to download and install the msttcorefonts package, which you can find in the majority of Linux distributions, instead of manually installing them.

To install fonts in Mac, double click in the file that contains the font (the one with a .ttf extension). Mac then will open the Font Book application, where you can see a preview of the font and its possible variations (italic, bold and so on). Click the Install font button and you're done.