gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:24:59 31 4
gpt4 key购买 nike

我可以添加 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 中可用的字体,但我需要添加自定义字体。请帮忙。

最佳答案

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 中的结果:

enter image description here

Libreoffice Impress Ubuntu Linux 中的结果:

enter image description here

关于java - 如何在 Apache POI ppt 中添加自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47920767/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com