首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Apache POI ppt中添加自定义字体

如何在Apache POI ppt中添加自定义字体
EN

Stack Overflow用户
提问于 2017-12-21 16:22:42
回答 1查看 1.8K关注 0票数 0

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

代码语言:javascript
运行
复制
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中提供的字体,但我需要添加自定义字体。请帮帮忙。

EN

回答 1

Stack Overflow用户

发布于 2017-12-21 18:10:25

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。如果该字体安装在操作系统中,则将使用该字体,否则,如果呈现该文件,则会猜测类似的字体。

示例:

代码语言:javascript
运行
复制
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 Windows10中的结果:

Libreoffice Impress Ubuntu Linux的结果:

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47920767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档