Page 1 of 1

Change font and font size

PostPosted: Thu Sep 22, 2011 9:21 pm
by Stijn
Dear all,

I have been using docx4j to automatically build reports from our database and I keep coming across the problem of fonts and font sizes. There have been a view posts on this forum about this but I can't seem to get it working. I am looking for a way to print lines with font "Courier New" and font size 8. I have looked trough styles.xml, knownstyles.xml, even tried to do it the way as in docx-java-f6/setting-font-in-default-styles-t388.html but I can't seem to get it working. Is there no easy way to apply a font to a generated paragraph?

It is obviously something a lot of people come across but there seems to be no well documented way of how to do it.

Many thanks in advance!

Stijn

Re: Change font and font size

PostPosted: Thu Sep 22, 2011 11:19 pm
by jason
Are you modifying an existing docx, or creating a new one from scratch?

It sounds like you might want to set Courier new font size 8 in your docDefaults:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
<w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:docDefaults>
    <w:rPrDefault>
      <w:rPr>
        <w:rFonts w:asciiTheme="minorHAnsi" w:eastAsiaTheme="minorHAnsi" w:hAnsiTheme="minorHAnsi" w:cstheme="minorBidi"/>
        <w:sz w:val="16"/>
        <w:szCs w:val="16"/>
        <w:lang w:val="en-AU" w:eastAsia="en-US" w:bidi="ar-SA"/>
      </w:rPr>
    </w:rPrDefault>

 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Alternatively, and a bit easier, you could modify your Normal style (or DefaultParagraphFont), which will affect all styles based on that:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  <w:style w:type="paragraph" w:default="1" w:styleId="Normal">
    <w:name w:val="Normal"/>
    <w:qFormat/>
    <w:rPr>
      <w:rFonts w:ascii="Courier New" w:hAnsi="Courier New"/>
      <w:sz w:val="16"/>
    </w:rPr>
  </w:style>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Or you could simply add direct formatting:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
    <w:rPr>
      <w:rFonts w:ascii="Courier New" w:hAnsi="Courier New"/>
      <w:sz w:val="16"/>
    </w:rPr>
 
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


to each paragraph.

As you can see, the answer depends on your starting point and what you want to do...

Re: Change font and font size

PostPosted: Fri Sep 23, 2011 12:34 am
by Stijn
Hi Jason,

Thanks for the quick reply! I am creating a new word document with multiple pages, I use one Heading1 at the top of each page and the rest of the page I need in Courier New 8. I needed the document to be in landscape orientation with narrow margins so I have this template.docx with these settings which it loads, then it adds paragraphs for each database record and it finaly saves it to report.docx.

I tried to edit my Normal Style in docx4j-2.7.0.jar\org\docx4j\openpackaging\parts\WordprocessingML\styles.xml but this didn't give any effect. Could you please explain where to modify this?

Thanks in advance

Stijn

Re: Change font and font size

PostPosted: Fri Sep 23, 2011 8:21 am
by jason
You need to change the styles.xml part in your template.docx, so that the entry for Normal includes:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
 <w:style w:type="paragraph" w:default="1" w:styleId="Normal">
    <w:name w:val="Normal"/>
    <w:qFormat/>
    <w:rPr>
      <w:rFonts w:ascii="Courier New" w:hAnsi="Courier New"/>
      <w:sz w:val="16"/>
    </w:rPr>
  </w:style>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


You can do that programmatically with docx4j, or in Word (easiest), or by unzipping and cut/paste.

If you do it using docx4j, start by getting the styles part:

Code: Select all
wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart();

Re: Change font and font size

PostPosted: Fri Sep 23, 2011 6:56 pm
by Stijn
Thank you very much, it works like a charm! Is it me or is there very little documentation about how to do these kind of things?

Re: Change font and font size

PostPosted: Sat Sep 24, 2011 5:58 pm
by jason
What docx4j documentation there is (ie the Getting Started guide) builds on what you can read in the OpenXML spec's Primer section (I think there's a link to it in the GS guide).

I hope you have Office installed .. that makes it much easier to see what XML you need to create.

Re: Change font and font size

PostPosted: Fri Oct 21, 2011 11:24 pm
by Beni
jason wrote:
If you do it using docx4j, start by getting the styles part:

Code: Select all
wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart();



How would this look like? Could you please write a short sample or if you did this before, tell me where i can find this sample. I stick on this font thing, i hope u can help me.

Beni

Re: Change font and font size

PostPosted: Sat Oct 22, 2011 5:50 am
by lucasfgc
Beni, try it

Code: Select all
WordprocessingMLPackage WordMLPackage = WordprocessingMLPackage.load(new File(docxPath));
StyleDefinitionsPart style = WordMLPackage.getMainDocumentPart().getStyleDefinitionsPart();


Now you have the styles of your document and u can change it in your way.

Correct me Jason, if i'm worng =P