Hi
I want to create a word document using only the font(s) in a folder. I copied all the Arial fonts to a folder and tried to create a word document using FolderFontSource. I see that the document that got created has the text in Times New Roman font.
Please see the code below-
FolderFontSource folderFontSource = new FolderFontSource("C:\\weblogs\\font", true, 0);
Document doc = new Document();
doc.setFontSettings(new FontSettings());
doc.getFontSettings().setFontsSources(new FontSourceBase[]{folderFontSource});
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Testing Font");
doc.save("C:\\weblogs\\WorkingWithFonts.docx");
Can you please help me create a word document with only the fonts I copy to a folder or load to the memory using MemoryFontSource ?
Thanks
Lakshmi
@lakshmi.vijayaraghavan Usually, MS Word and other flows document formats do not require fonts, i.e. the fonts are not embedded into the documents by default. The consumer applications simply use the fonts available in the environment where document is opened to display the content. So you can simply use the following code:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getFont().setName("Arial");
builder.write("Testing Font");
doc.save("C:\\Temp\\out.docx");
If you would like to embed fonts used in the document you can instruct Aspose.Words to do this. In this can the fonts must be physically available in the environment where the document is generated:
// Specify folder where Aspose.Words will look for fonts.
// You can use any other font sources here.
// If not configured Aspose.Words will look for fonts in the system font source.
FontSettings.getDefaultInstance().setFontsFolder("C:\\Temp\\fonts", true);
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getFont().setName("Arial");
builder.write("Testing Font");
// Configure Aspose.Words to embed fonts.
doc.getFontInfos().setEmbedTrueTypeFonts(true);
// If it is required to embed system fonts.
doc.getFontInfos().setEmbedSystemFonts(true);
// If it is supposed to edit document, it is recommended to embed full fonts.
// Node this will significantly increase the resulting document size.
doc.getFontInfos().setSaveSubsetFonts(false);
doc.save("C:\\Temp\\out.docx");
Thank you very much for the help. I ran into another problem, when I tried to save the word document as a PDF.
FileInputStream fin = new FileInputStream(new File("C:\\weblogs\\TestFontEmbedding.docx"));
Document document = new Document(fin);
FontInfoCollection fontInfos = document.getFontInfos();
LOG.debug("GET EMBEDDED FONT INFO>>>" + fontInfos.getEmbedSystemFonts());
for (FontInfo fontInfo : fontInfos) {
LOG.debug("Font Name:getPdfDocumentObjectFromDocxStream: " + fontInfo.getName());
// Check if the font is embedded. This usually indicates presence of embedded font data
if (fontInfo.getEmbeddedFont(EmbeddedFontFormat.OPEN_TYPE, EmbeddedFontStyle.REGULAR) != null) {
LOG.debug("\tThis font is embedded.");
} else {
LOG.debug("\tThis font is not embedded.");
}
}
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setFontEmbeddingMode(PdfFontEmbeddingMode.EMBED_ALL);
pdfSaveOptions.setEmbedFullFonts(true);
pdfSaveOptions.setUseCoreFonts(false);
//pdfSaveOptions.setUseTrueTypeFontForASCII(false);
final FileOutputStream pdfBaos = new FileOutputStream("C:\\weblogs\\TestFontEmbeddingGenerated.pdf");
document.save(pdfBaos, pdfSaveOptions);
FileInputStream finPdf = new FileInputStream(new File("C:\\weblogs\\TestFontEmbeddingGenerated.pdf"));
com.aspose.pdf.Document pdfDoc = new com.aspose.pdf.Document(finPdf);
Font[] fonts = pdfDoc.getFontUtilities().getAllFonts();
for (Font font : fonts) {
// Check if the font is embedded
boolean isEmbedded = font.isEmbedded();
LOG.debug("PDF Font Name: " + font.getFontName());
LOG.debug("PDF Is Embedded: " + isEmbedded);
// Optional: Check if it is a subset
LOG.debug("Is Subset: " + font.isSubset());
LOG.debug("-------------------------");
}
pdfDoc.close();
} catch(Exception e) {
e.printStackTrace();
}
When I run this I find only 2 fonts embedded in the PDF generated - ArialMT and Arial-BoldMT. But the word document has Calibri, Times New Roman, Arial, Helvetica, Calibri Light. Can you please let me know why the PDF does not have other fonts embedded? Please see the attached files.
Thanks
TestFontEmbeddingGenerated.pdf (1.0 MB)
TestFontEmbedding.docx (5.4 MB)
@lakshmi.vijayaraghavan It is expected, only the fonts which are actually used in the document content are embedded into the output PDF document. If other fonts are embedded into the source document, but are not actually used in the document content, they will not be embedded into the output PDF.
Though, I see in your document Helvetica font is used and this font is embedded into the source document, but in the output PDF Arial font is used. This does not cause any visual difference because Arial font is MS equivalent of Helvetica font. Digging deeper showed that the fonts embedded into your document as Helvetica are actually Arial fonts. I used the following code to extract fonts:
Document doc = new Document("C:\\Temp\\in.docx");
ExtractEmbeddedFonts(doc, "C:\\Temp\\");
public static void ExtractEmbeddedFonts(Document doc, String outputFolder) throws IOException {
int[] fontStyles = new int[]
{
EmbeddedFontStyle.REGULAR,
EmbeddedFontStyle.BOLD,
EmbeddedFontStyle.ITALIC,
EmbeddedFontStyle.BOLD_ITALIC
};
for (FontInfo info : doc.getFontInfos())
{
for (int style : fontStyles)
{
byte[] embeddedFont = info.getEmbeddedFont(EmbeddedFontFormat.OPEN_TYPE, style);
if (embeddedFont != null)
{
String fontFileName =outputFolder + info.getName() + "_" + EmbeddedFontStyle.getName(style) +".otf";
FileOutputStream fos = new FileOutputStream(fontFileName);
fos.write(embeddedFont);
}
}
}
}
Here are the extracted fonts: fonts.zip (5.2 MB)
Thank you very much for the help!
1 Like