【问题标题】:How to add custom fonts in Apache POI ppt如何在 Apache POI ppt 中添加自定义字体
【发布时间】:2017-12-21 08:22:41
【问题描述】:

我可以添加 Apache POI ppt 中默认的字体,但无法添加自定义字体。这是我目前拥有的:

XSLFTextBox categoryTitleShape = indexslide.createTextBox();
categoryTitleShape.setAnchor(new java.awt.Rectangle(25, 40, 120, 30));
XSLFTextRun categoryTitle = categoryTitleShape.addNewTextParagraph().addNewTextRun();
categoryTitle.setText("CATEGORIES"); // visible text
categoryTitle.setFontSize(20.);
categoryTitle.setFontColor(Color.BLACK);
categoryTitle.setBold(true);
categoryTitle.setFontFamily(HSSFFont.FONT_ARIAL, FontGroup.EAST_ASIAN);

上面的代码添加了Apache POI ppt中可用的字体,但我需要添加自定义字体。请帮忙。

【问题讨论】:

    标签: java fonts apache-poi powerpoint


    【解决方案1】:

    Microsoft Office 文档中似乎可以嵌入字体。至少在 PowerPoint 和 Word 中。请参阅 How to embed fonts in PowerPointHow to embed a TrueType font in a document。但不幸的是,apache poi不支持将此字体文件存储在 Office Open XML 文档文件的/fonts/ 部分中。

    所以到目前为止使用apache poi 所使用的字体必须安装在操作系统中。我们只能在XSLFTextRun.setFontFamily 中给出一个字符串作为typeface。如果操作系统中安装了该字体,则使用该字体,否则在渲染文件时会猜到类似字体。

    例子:

    import java.io.FileOutputStream;
    
    import org.apache.poi.xslf.usermodel.*;
    import org.apache.poi.sl.usermodel.*;
    
    import java.awt.Rectangle;
    
    public class CreatePPTXTextBoxSpecialFont {
    
     public static void main(String[] args) throws Exception {
    
      XMLSlideShow slideShow = new XMLSlideShow();
    
      XSLFSlide slide = slideShow.createSlide();
    
      XSLFTextBox textbox = slide.createTextBox(); 
      textbox.setAnchor(new Rectangle(50, 100, 570, 100));
      XSLFTextParagraph paragraph = textbox.addNewTextParagraph(); 
      XSLFTextRun run = paragraph.addNewTextRun();
      run.setText("Arial ");
      run.setFontFamily("Arial");
      run.setFontSize(24d);
      run = paragraph.addNewTextRun();
      run.setText("Algerian ");
      run.setFontFamily("Algerian");
      run.setFontSize(24d);
      run = paragraph.addNewTextRun();
      run.setText("Courier ");
      run.setFontFamily("Courier");
      run.setFontSize(24d);
      run = paragraph.addNewTextRun();
      run.setText("Times New Roman ");
      run.setFontFamily("Times New Roman");
      run.setFontSize(24d);
    
      FileOutputStream out = new FileOutputStream("CreatePPTXTextBoxSpecialFont.pptx");
      slideShow.write(out);
      out.close();
     }
    }
    

    PowerPoint Windows 10 中的结果:

    在 Libreoffice Impress Ubuntu Linux 中的结果:

    【讨论】:

    • ... 但对于 XSLF fonts can be embedded ... 但这种方法也有限制(安装字体的权限)
    • @kiwiwings:谢谢你。我不知道。 blogs.office.com/en-us/2015/07/06/… 似乎提供了进一步的见解。但是 SSL 证书已经过时了。 grmbl微软!尽管如此,还是会尝试一下并阅读此内容。
    • @kiwiwings:这些见解并不像耸人听闻的标题“文档字体嵌入揭秘”所承诺的那样多 ;-)。你知道apache poi 是否计划将来在XWPF 和/或XSLF 中提供字体嵌入功能?
    • 我做过some work on it,但是因为过于依赖我不敢放进去
    猜你喜欢
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    • 2019-12-12
    • 2021-09-08
    相关资源
    最近更新 更多