本文介绍了如何在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");//可见文本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 中.请参阅

在 Libreoffice Impress Ubuntu Linux 中的结果:

I am able to add fonts which are default in Apache POI ppt but not able to add custom fonts. 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. Please help.

解决方案

There seems to be a font embedding possible in Microsoft Office documents. At least in PowerPoint and Word. See How to embed fonts in PowerPoint and How to embed a TrueType font in a document. But unfortunately apache poidoes not supporting storing this font files in /fonts/ part of the Office Open XML document files.

So using apache poi until now the fonts used must be installed in the operating system. We only can give a string as the typeface in XSLFTextRun.setFontFamily. 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:

Result in Libreoffice Impress Ubuntu Linux:

这篇关于如何在Apache POI ppt中添加自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 12:10