简体   繁体   English

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

[英]How to add custom fonts in Apache POI ppt

I am able to add fonts which are default in Apache POI ppt but not able to add custom fonts. 我可以添加Apache POI ppt中默认的字体,但不能添加自定义字体。 This is what I currently have: 这是我目前拥有的:

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);

The above code adds fonts which are available in Apache POI ppt, but I need to add custom fonts. 上面的代码添加了Apache POI ppt中可用的字体,但是我需要添加自定义字体。 Please help. 请帮忙。

There seems to be a font embedding possible in Microsoft Office documents. 在Microsoft Office文档中似乎可以嵌入字体。 At least in PowerPoint and Word. 至少在PowerPoint和Word中。 See How to embed fonts in PowerPoint and How to embed a TrueType font in a document . 请参阅如何在PowerPoint中嵌入字体如何在文档中嵌入TrueType字体 But unfortunately apache poi does not supporting storing this font files in /fonts/ part of the Office Open XML document files. 但是很遗憾, apache poi不支持将此字体文件存储在Office Open XML文档文件的/fonts/部分中。

So using apache poi until now the fonts used must be installed in the operating system. 因此,直到现在使用apache poi必须在操​​作系统中安装使用的字体。 We only can give a string as the typeface in XSLFTextRun.setFontFamily . 我们只能在XSLFTextRun.setFontFamily中将字符串作为typeface If this font is installed in the operating system, then it will be used, else a similar font will be guessed if the file is rendered. 如果在操作系统中安装了该字体,则将使用它,否则,如果呈现文件,则将猜测类似的字体。

Example: 例:

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();
 }
}

Result in PowerPoint Windows 10: 在PowerPoint Windows 10中的结果:

在此处输入图片说明

Result in Libreoffice Impress Ubuntu Linux: Libreoffice的结果给Ubuntu Linux留下了深刻的印象:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM