I am having an issue setting fonts. It only happens when I use a custom font 'MyFont'. The font was created using a font generating tool. If I use a standard font like Arial Bold no problem.
I am basically modifying the font on a content control using the following code:
Using java Syntax Highlighting
// Build a new RFont object
org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory();
RFonts rfonts = factory.createRFonts();
rfonts.setAscii(sFontName);
rfonts.setCs(sFontName);
rfonts.setHAnsi(sFontName);
// Locate RPr object inside SdtElement
SdtPr sdtPr = sdtElement.getSdtPr();
RPr rpr = null;
List<Object> aList = sdtPr.getRPrOrAliasOrLock();
for (int i = 0; i < aList.size(); i++) {
Object o1 = XmlUtils.unwrap(aList.get(i));
if (o1 instanceof RPr) {
rpr = (RPr) o1;
break;
}
}
// Create new rpr if not found
if(rpr == null) {
rpr = factory.createRPr();
sdtPr.getRPrOrAliasOrLock().add(rpr);
}
// Set fonts
rpr.setRFonts(rfonts);
// Locate the run inside the SdtContect
R sdtR = null;
List<Object> sdtContent = sdtElement.getSdtContent().getContent();
for(Object o1 : sdtContent) {
if (o1 instanceof P) {
List<Object> pContent = ((P)o1).getContent();
for(Object o2 : pContent) {
if(o2 instanceof R) {
sdtR = (R) o2;
break;
}
}
break;
}
}
// locate the rpr object inside the run
rpr = sdtR.getRPr();
// Add RPr element if not found
if(rpr == null) {
rpr = factory.createRPr();
}
// Set the signature font
rpr.setRFonts(rfonts);
sdtR.setRPr(rpr);
Parsed in 0.018 seconds, using GeSHi 1.0.8.4
For some reason it sets all the fonts correctly on the rPr, but not on the run. when setting fonts on the run it only sets the w:cs correctly. w:ascii and w:hAnsi are not changed. Please see generated XML.
Using xml Syntax Highlighting
<w:sdt>
<w:sdtPr>
<w:rPr>
<w:rFonts w:ascii="MyFont" w:hAnsi="MyFont" w:cs="MyFont"/>
<w:color w:val="000000"/>
<w:sz w:val="96"/>
</w:rPr>
<w:alias w:val="Signature"/>
<w:tag w:val="#Signature"/>
<w:id w:val="37793253"/>
<w:dataBinding w:xpath="//document//signature" w:storeItemID="{CD59EE91-E450-47CD-B5B3-94785B5201F1}"/>
<w:text w:multiLine="1"/>
</w:sdtPr>
<w:sdtEndPr/>
<w:sdtContent>
<w:p w:rsidR="00D34090" w:rsidRDefault="00B4607B">
<w:pPr>
<w:spacing w:after="0" w:line="240" w:lineRule="auto"/>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="MyFont"/>
<w:color w:val="000000"/>
<w:sz w:val="96"/>
</w:rPr>
<w:t>A</w:t>
</w:r>
</w:p>
</w:sdtContent>
</w:sdt>
<w:sdtPr>
<w:rPr>
<w:rFonts w:ascii="MyFont" w:hAnsi="MyFont" w:cs="MyFont"/>
<w:color w:val="000000"/>
<w:sz w:val="96"/>
</w:rPr>
<w:alias w:val="Signature"/>
<w:tag w:val="#Signature"/>
<w:id w:val="37793253"/>
<w:dataBinding w:xpath="//document//signature" w:storeItemID="{CD59EE91-E450-47CD-B5B3-94785B5201F1}"/>
<w:text w:multiLine="1"/>
</w:sdtPr>
<w:sdtEndPr/>
<w:sdtContent>
<w:p w:rsidR="00D34090" w:rsidRDefault="00B4607B">
<w:pPr>
<w:spacing w:after="0" w:line="240" w:lineRule="auto"/>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="MyFont"/>
<w:color w:val="000000"/>
<w:sz w:val="96"/>
</w:rPr>
<w:t>A</w:t>
</w:r>
</w:p>
</w:sdtContent>
</w:sdt>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
Any ideas?
Thanks,
Jeff