Reputation: 20518
I have a document (.doc) that I've generated using Apache POI with HWPF and I want to change the font type. I'm guessing that the place to change it would be on the character runs inside each paragraph.
CharacterRun has methods such as .setBold()
.setColor()
and .getFontName()
but there isn't any method to set the font that I've been able to find.
In XWPF there is a .setFontFamily()
but is there a way to do the same thing with HWPF?
Range after = doc.getRange();
int numParagraphs = after.numParagraphs();
for(int i = 0; i < numParagraphs; i++){
Paragraph paragraph = after.getParagraph(i);
int charRuns = paragraph.numCharacterRuns();
for(int j = 0; j < charRuns; j++){
int size = 9;
CharacterRun run = paragraph.getCharacterRun(j);
run.setFontSize(size*2); // In half sizes.
}
}
Upvotes: 2
Views: 542
Reputation: 20518
The method for changing the font type on a CharacterRun is .setFtcAscii()
which changes the font to one of the document's embedded fonts. The document I was using had the font table below.
╔═══╦═════════════════╗
║ ║ Font Family ║
╠═══╬═════════════════╣
║ 0 ║ Times New Roman ║
║ 1 ║ Symbol ║
║ 2 ║ Arial ║
║ 3 ║ Calibri ║
║ 4 ║ Courier New ║
║ 5 ║ Cambria Math ║
╚═══╩═════════════════╝
I needed to change the font to Courier New
so I used:
run.setFtcAscii(4);
--
Other documents may have different font tables so I created a for-loop that set the font index and then printed out font name using .getFontName()
Also, I found that run.setFtcOther(int)
does the same thing as run.setFtcAscii(int)
See: (0x4A4F)
https://msdn.microsoft.com/en-us/library/dd947480(v=office.12).aspx
Upvotes: 3