[英]How to add custom fonts in Apache POI ppt
我可以添加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中。 請參閱如何在PowerPoint中嵌入字體和如何在文檔中嵌入TrueType字體 。 但是很遺憾, 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的結果給Ubuntu Linux留下了深刻的印象:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.