更改 CharacterRun 的字体类型
Change font type of CharacterRun
我有一个使用 Apache POI 和 HWPF 生成的文档 (.doc),我想更改字体类型。我猜想更改它的地方应该是在每个段落中运行的字符上。
CharacterRun 有 .setBold()
.setColor()
和 .getFontName()
等方法,但我找不到任何设置字体的方法。
在 XWPF 中有一个 .setFontFamily()
但是有没有办法用 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.
}
}
更改 CharacterRun 字体类型的方法是 .setFtcAscii()
,它将字体更改为文档的嵌入字体之一。我使用的文档的字体如下 table。
╔═══╦═════════════════╗
║ ║ Font Family ║
╠═══╬═════════════════╣
║ 0 ║ Times New Roman ║
║ 1 ║ Symbol ║
║ 2 ║ Arial ║
║ 3 ║ Calibri ║
║ 4 ║ Courier New ║
║ 5 ║ Cambria Math ║
╚═══╩═════════════════╝
我需要将字体更改为 Courier New
所以我使用了:
run.setFtcAscii(4);
--
其他文档可能有不同的字体 tables 所以我创建了一个 for-loop 来设置字体索引,然后使用 .getFontName()
打印出字体名称
此外,我发现 run.setFtcOther(int)
和 run.setFtcAscii(int)
做同样的事情
参见:(0x4A4F)
https://msdn.microsoft.com/en-us/library/dd947480(v=office.12).aspx
我有一个使用 Apache POI 和 HWPF 生成的文档 (.doc),我想更改字体类型。我猜想更改它的地方应该是在每个段落中运行的字符上。
CharacterRun 有 .setBold()
.setColor()
和 .getFontName()
等方法,但我找不到任何设置字体的方法。
在 XWPF 中有一个 .setFontFamily()
但是有没有办法用 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.
}
}
更改 CharacterRun 字体类型的方法是 .setFtcAscii()
,它将字体更改为文档的嵌入字体之一。我使用的文档的字体如下 table。
╔═══╦═════════════════╗
║ ║ Font Family ║
╠═══╬═════════════════╣
║ 0 ║ Times New Roman ║
║ 1 ║ Symbol ║
║ 2 ║ Arial ║
║ 3 ║ Calibri ║
║ 4 ║ Courier New ║
║ 5 ║ Cambria Math ║
╚═══╩═════════════════╝
我需要将字体更改为 Courier New
所以我使用了:
run.setFtcAscii(4);
--
其他文档可能有不同的字体 tables 所以我创建了一个 for-loop 来设置字体索引,然后使用 .getFontName()
此外,我发现 run.setFtcOther(int)
和 run.setFtcAscii(int)
参见:(0x4A4F)
https://msdn.microsoft.com/en-us/library/dd947480(v=office.12).aspx