TTF Font File Is Written to PPTX but It Is Not Written Successfully

hello!
byte[] memoryFont1 = Files.readAllBytes(Paths.get(“C:\Users\xuhui\Documents\Tencent Files\1905659868\FileRecv\zh46hmmt.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});

    Presentation pres = new Presentation("D:\\usr\\1.pptx", loadOptions);


    if (pres != null) {
        pres.dispose();
    }

@XUHUI,
Welcome to our community! Thank you for contacting support.

To investigate this case, please share and specify the following:

  • your presentation file (1.pptx)
  • font file you used (zh46hmmt.ttf)
  • OS version on which the code was run
  • JDK version you used

You can zip the files and upload them here.

jdk1.8 zh46hmmt.zip (6.9 MB)

@XUHUI,
I’ve performed some research. It looks like you are trying to embed the font in the presentation. This can be done like this:

pres.getFontsManager().addEmbeddedFont(memoryFont1, EmbedFontCharacters.All);

But you also have to check if the font is already embedded before.
Documents: Embedded Font

If this is not the case, please describe your purposes in more detail.

OK, thank you. Can you send me the complete example

hello! I just want to write the TTF font file into pptx. The Aspose API can support this function. The example I gave you earlier is this operation, but it is a little confused that the TTF file is written into pptx and is not displayed. Can you write an example on my basis? I saw the connection you sent, which means to write the font in pptx into another pptx, It doesn’t seem to be the result I want. Can you provide an example

@XUHUI,
You can try to use the following code snippet to embed the font to the presentation:

IFontData[] embeddedFonts = pres.getFontsManager().getEmbeddedFonts();
Font newFont = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(fontPath));

boolean isAlreadyEmbedded = false;
for (IFontData embeddedFont : embeddedFonts) {
    if (embeddedFont.getFontName().equals(newFont.getFontName())) {
        isAlreadyEmbedded = true;
        break;
    }
}

if (!isAlreadyEmbedded) {
    byte[] fontData = Files.readAllBytes(Paths.get(fontPath));
    pres.getFontsManager().addEmbeddedFont(fontData, EmbedFontCharacters.All);
}

API Reference: IFontsManager interface

I used your example, but I didn’t see the written font. What’s the problem
image.png (87.9 KB)
My original idea was that the font content should also be written into pptx
This font is not displayed in pptx
image.png (54.8 KB)
The content of this red line font is not displayed in pptx. Why?

@XUHUI,
It looks like this font can’t display latin characters. Please take a look at the results of the following code examples:

InputStream fontStream = new FileInputStream("zh46hmmt.ttf");
Font newFont = Font.createFont(Font.TRUETYPE_FONT, fontStream);

PrintCharInfo(newFont, 'A');
PrintCharInfo(newFont, '1');
PrintCharInfo(newFont, '树');
    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)));
    }

Output:

'A' character will be displayed: false
'1' character will be displayed: true
'树' character will be displayed: true

API Reference: Class Font

Hello, I still have no idea to write the content of pptx file into pptx. It seems that it is still impossible to rely on Aspose. The previous operation only writes the font definition into pptx. In fact, the content of TTF file is still not written. Do you need to parse TTF file, obtain content, and write text and font type into pptx through Aspose?

@XUHUI,
Please load a presentation, embed the font as shown above, save the presentation, unzip it and go to \ppt\fonts folder of the package. Then you will see the embedded font.

I still don’t know how to operate. With the existing code demo, I can’t write the contents of TTF file into pptx

@XUHUI,
Please describe the current problem and how to reproduce it on our side in more detail.

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

}

}

@XUHUI,
Please note that presentations are not saved in your code examples. Could this be the reason?

If the issue persists, please share the following:

  • H-TTF-BuMing-B-2.ttf file
  • 3.pptx presentation file
  • screenshot with the problem

Hello, I want to ask if there is any function in Aspose to parse the TTF content and write it into pptx? Or do you first type the font type into pptx through the Aspose API, then parse the TTF file information content and write it into pptx?

@XUHUI,
Far as I know, Aspose.Slides does not provide such a function. You can try using Aspose.Font API for your purposes. It would be great if you could share the font and a presentation file example with the desired content that you want to generate with Aspose.Slides.

Another is that if a third-party font is not installed in the computer, then it cannot be written into pptx through the Aspose API. I have tested it many times, but I still can’t find the third-party font type in the font type. Is it the wrong use of Aspose or? For example, in this example, is the font file on the page?

Presentation pres = new Presentation(“C:\Users\xuhui\Downloads\2.pptx”);

IFontData[] embeddedFonts = pres.getFontsManager(). getEmbeddedFonts();

Font newFont = Font. Createfont (font.truetype_font, new FileInputStream (“C: \ \ users \ \ Xuhui \ \ documents \ \ tent files \ \ 1905659868 \ \ filerecv \ \ zihun 146 - leisurely Zizai. 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 (“C: \ \ users \ \ Xuhui \ \ documents \ \ tenant files \ \ 1905659868 \ \ filerecv \ \ zihun 146 - leisurely zizati. TTF”);

pres.getFontsManager(). addEmbeddedFont(fontData, EmbedFontCharacters.All);

// }

pres.save(“C:\Users\xuhui\Downloads\3.pptx”, SaveFormat.Pptx);

pres.dispose();

@XUHUI,
To help you in the best way, we need some additional data. Please share the following:

  • your input 2.pptx file and output 3.pptx file
  • “zihun 146 - leisurely Zizai.TTF” font