This is all my current examples. I can’t write the TTF font content into pptx, but I just embed the font into pptx, but the result I want is that the TTF content is written into pptx. At present, I haven’t figured out the correct api code. It seems that the TTF file content can’t be written in, so I can only embed the TTF file font
public class TtfToPptx {
public static void PrintCharInfo(Font font, char character)
{
final String message = “’%s’ character will be displayed: %s”;
System.out.println(String.format(message, character, font.canDisplay(character)));
}
public static void main(String[] args) throws Exception {
// InputStream fontStream = new FileInputStream(“D:\ppt\H-TTF-BuMingTi-CuTi\H-TTF-BuMing-B-2.ttf”);
// Font newFont = Font.createFont(Font.TRUETYPE_FONT, fontStream);
//
// PrintCharInfo(newFont, ‘A’);
// PrintCharInfo(newFont, ‘1’);
// PrintCharInfo(newFont, ‘树’);
// Presentation pres = new Presentation();
//
// IFontData[] embeddedFonts = pres.getFontsManager().getEmbeddedFonts();
// Font newFont = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(“D:\ppt\H-TTF-BuMingTi-CuTi\H-TTF-BuMing-B-2.ttf”));
//
// boolean isAlreadyEmbedded = false;
// for (IFontData embeddedFont : embeddedFonts) {
// if (embeddedFont.getFontName().equals(newFont.getFontName())) {
// isAlreadyEmbedded = true;
// break;
// }
// }
//
// if (!isAlreadyEmbedded) {
// byte[] fontData = Files.readAllBytes(Paths.get(“D:\ppt\H-TTF-BuMingTi-CuTi\H-TTF-BuMing-B-2.ttf”));
// pres.getFontsManager().addEmbeddedFont(fontData, EmbedFontCharacters.All);
// }
// pres.save(“D:\usr\3.pptx”, SaveFormat.Pptx);
// pres.dispose();
// Presentation pres = new Presentation(“Fonts.pptx”);
// try {
// IFontData[] allFonts = pres.getFontsManager().getFonts();
// IFontData[] embeddedFonts = pres.getFontsManager().getEmbeddedFonts();
//
// for (IFontData font : allFonts)
// {
// boolean embeddedFontsContainsFont = false;
// for (int i = 0; i < embeddedFonts.length; i++)
// {
// if (embeddedFonts.equals(font)) embeddedFontsContainsFont = true;
// }
// if (!embeddedFontsContainsFont)
// {
// pres.getFontsManager().addEmbeddedFont(font, EmbedFontCharacters.All);
// }
// }
//
// // Save the presentation
// pres.save(“AddEmbeddedFont_out.pptx”, SaveFormat.Pptx);
// } finally {
// if (pres != null) pres.dispose();
// }
//
// byte[] memoryFont1 = Files.readAllBytes(Paths.get(“D:\ppt\H-TTF-BuMingTi-CuTi\H-TTF-BuMing-B-2.ttf”));
// System.out.println(memoryFont1);
// byte[] memoryFont2 = Files.readAllBytes(Paths.get(“customfonts/CustomFont2.ttf”));
// LoadOptions loadOptions = new LoadOptions();
// loadOptions.getDocumentLevelFontSources().setFontFolders(new String[] { “assets/fonts”, “global/fonts” });
// loadOptions.getDocumentLevelFontSources().setMemoryFonts(new byte[][] { memoryFont1});
// pres.getFontsManager().addEmbeddedFont(memoryFont1, EmbedFontCharacters.All);
// Presentation pres = new Presentation(“D:\usr\1.pptx”);
// pres.getFontsManager().addEmbeddedFont(memoryFont1, EmbedFontCharacters.All);
//
//
// if (pres != null) {
// pres.save(“D:\usr\1.pptx”, SaveFormat.Pptx);
// pres.dispose();
// }
// // folders to seek fonts
// byte[] bytes = Files.readAllBytes(Paths.get(“C:\Users\xuhui\Documents\Tencent Files\1905659868\FileRecv\zh46hmmt.ttf”));
//// String[] folders = new String[] { externalFontsDir };
//
//// Load the custom font directory fonts
//// FontsLoader.loadExternalFonts(new String(bytes));
// System.out.println(new String(bytes));
// FontsLoader.loadExternalFont(bytes);
//
//// Do Some work and perform presentation/slides rendering
// Presentation pres = new Presentation(“D:\usr\1.pptx”);
// try {
// pres.save(“D:\usr\1.pptx”, SaveFormat.Pptx);
// } finally {
// if (pres != null) {
// pres.dispose();
// }
//
// // Clear Font Cachce
// FontsLoader.clearCache();
// }
// IPresentation presentation = new Presentation();
// try {
// // Read SVG file content
// byte[] svgContent = Files.readAllBytes(Paths.get(“C:\Users\xuhui\Documents\Tencent Files\1905659868\FileRecv\test2.svg”));
// System.out.println(new String(svgContent));
//
// // Create SvgImage object
// ISvgImage svgImage = new SvgImage(svgContent);
//
// // Get slide size
// Dimension2D slideSize = presentation.getSlideSize().getSize();
//
// // Convert SVG image to group of shapes scaling it to slide size
// presentation.getSlides().get_Item(0).getShapes().
// addGroupShape(svgImage, 100, 100, (float)slideSize.getWidth(), (float)slideSize.getHeight());
//
// // Save presentation in PPTX format
// presentation.save(“D:\data\output1.pptx”, SaveFormat.Pptx);
// } catch (IOException e) {
// } finally {
// if (presentation != null) presentation.dispose();
// }
byte[] memoryFont1 = Files.readAllBytes(Paths.get("D:\\ppt\\H-TTF-BuMingTi-CuTi\\H-TTF-BuMing-B-2.ttf"));
LoadOptions loadOptions = new LoadOptions();
loadOptions.getDocumentLevelFontSources().setFontFolders(new String[] { "D:\\usr\\assets\\fonts", "D:\\usr\\global\\fonts" });
loadOptions.getDocumentLevelFontSources().setMemoryFonts(new byte[][] { memoryFont1 });
Presentation pres = new Presentation("D:\\usr\\3.pptx", loadOptions);
try {
//work with the presentation
//CustomFont1, CustomFont2 as well as fonts from assets\fonts & global\fonts folders and their subfolders are available to the presentation
} finally {
if (pres != null) pres.dispose();
}
}
}