【问题标题】:Change font type of CharacterRun更改 CharacterRun 的字体类型
【发布时间】:2016-05-08 18:53:45
【问题描述】:

我有一个使用 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.
    }
}

【问题讨论】:

    标签: java apache-poi doc hwpf


    【解决方案1】:

    在 CharacterRun 上更改字体类型的方法是 .setFtcAscii(),它将字体更改为文档的嵌入字体之一。我使用的文档有下面的字体表。

    ╔═══╦═════════════════╗
    ║   ║ Font Family     ║
    ╠═══╬═════════════════╣
    ║ 0 ║ Times New Roman ║
    ║ 1 ║ Symbol          ║
    ║ 2 ║ Arial           ║
    ║ 3 ║ Calibri         ║
    ║ 4 ║ Courier New     ║
    ║ 5 ║ Cambria Math    ║
    ╚═══╩═════════════════╝
    

    我需要将字体更改为Courier New,所以我使用了:

    run.setFtcAscii(4);
    

    --

    其他文档可能有不同的字体表,所以我创建了一个 for 循环来设置字体索引,然后使用 .getFontName() 打印出字体名称

    另外,我发现run.setFtcOther(int)run.setFtcAscii(int) 做同样的事情


    请参阅:(0x4A4F)

    https://msdn.microsoft.com/en-us/library/dd947480(v=office.12).aspx

    【讨论】:

      猜你喜欢
      • 2014-09-27
      • 1970-01-01
      • 2018-09-19
      • 2020-05-31
      • 2012-02-29
      • 2015-12-03
      • 2017-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多