Free Support Forum - aspose.com

Apply custom font in Word doc not working despite loading


#1

Hello,
Am trying to apply custom font that are not installed on my machine to a word document am creating. Am using apose v18.11.0. I can load the font correctly apply it correctly in my word document but i dont see any effect. So font are not applyied at all. Here is my code
ArrayList fontSources = new ArrayList(FontSettings.DefaultInstance.GetFontsSources());
FolderFontSource folderFontSource = new FolderFontSource(@“path\to\my\font\folder”, true);
fontSources.Add(folderFontSource);
FontSourceBase[] updatedFontSources = (FontSourceBase[])fontSources.ToArray(typeof(FontSourceBase));
FontSettings.DefaultInstance.SetFontsSources(updatedFontSources);

So I checked and font are found a loaded correctly. Here is how I apply the font

Style style = doc.Styles.Add(StyleType.Paragraph, "DocumentTitle");
                        style.Font.Name = "Oswald-Bold";                     
                        builder.ParagraphFormat.Style = style;
                        builder.ParagraphFormat.Alignment = ParagraphAlignment.Justify;

When I open the result word doc my font is listed in the word UI font drop downlist. But it is not applyid at all. words are still showing in the word default font

Can somebody help please?

Regards


#2

@marcelluswalace,

Please ZIP and attach your input Word document, Aspose.Words generated output Word document showing the undesired behavior and a simplified console application to reproduce the exact same issue on our end. We will then investigate the scenario on our end and provide you more information.


#3

WordDocWithFonts.zip (3.1 MB)

Hello please find requested document here. Basically document title needs to have oswald bold font


#4

here the code of my console app

        License license = new License();
        license.SetLicense("Aspose.Total.lic");

        var doc = new Document(@"Input.docx");
        string description = "Dude Impact is a new data-driven solution that enables brands to attribute business value for earned media campaigns, applying the same technologies to earned media programs as paid Gnus3 media initiatives.";
        doc.BuiltInDocumentProperties["Title"].Value = "Patate";
        doc.BuiltInDocumentProperties["Subject"].Value = "Patate Subject";
        doc.BuiltInDocumentProperties["Comments"].Value = "Patate Comment";
        doc.BuiltInDocumentProperties["Company"].Value = "Patate Company";
        DocumentBuilder builder = new DocumentBuilder(doc);

        //Adding the fonts
        ArrayList fontSources = new ArrayList(FontSettings.DefaultInstance.GetFontsSources());
        FolderFontSource folderFontSource = new FolderFontSource(@"\Fonts\Oswald\", true);
        var fonts = folderFontSource.GetAvailableFonts();
        fontSources.Add(folderFontSource);
        FontSourceBase[] updatedFontSources = (FontSourceBase[])fontSources.ToArray(typeof(FontSourceBase));
        FontSettings.DefaultInstance.SetFontsSources(updatedFontSources);

        //Adding description
        NodeCollection nodes = doc.GetChildNodes(NodeType.Shape, true);
        for (int i = 0; i < nodes.Count; i++)
        {
            Shape shape = (Shape)nodes[i];
            string s = shape.GetText();
            if (shape.GetText().Contains("Dude_REPORT_DESCRIPTION"))
            {
                shape.RemoveAllChildren();
                shape.AppendChild(new Paragraph(doc));
                builder.MoveTo(shape.FirstParagraph);
                Style style = doc.Styles.Add(StyleType.Paragraph, "DudeDocumentDesc");
                style.Font.Name = "Oswald Regular";
                style.Font.Color = Color.White;
                builder.ParagraphFormat.Style = style;
                builder.ParagraphFormat.Alignment = ParagraphAlignment.Justify;
                builder.Writeln(description);
            }
            else if (shape.GetText().Contains("Impact for Earned Media"))
            {
                var paras = shape.GetChildNodes(NodeType.Paragraph, true);
                foreach (var each in paras)
                {
                    if (each.GetText().Contains("Impact for Earned Media"))
                    {

                        builder.MoveTo(each);
                        Style style = doc.Styles.Add(StyleType.Paragraph, "DudeDocumentTitle");
                        style.Font.Name = "Oswald Bold";
                        // style.Font.Color = Color.Green;
                        // style.Font.Size = 24;
                        style.Font.NameBi = "Oswald Bold";
                        builder.ParagraphFormat.Style = style;
                        builder.ParagraphFormat.Alignment = ParagraphAlignment.Justify;
                    }
                    else if (each.GetText().Contains("Industry Search"))
                    {
                        builder.MoveTo(each);
                        Style style = doc.Styles.Add(StyleType.Paragraph, "DudeDocumentSubTitle");
                        style.Font.Name = "Oswald Bold";
                        style.Font.Color = Color.Green;
                        builder.ParagraphFormat.Style = style;
                        builder.ParagraphFormat.Alignment = ParagraphAlignment.Justify;
                    }
                    else if (each.GetText().Contains("Oct 19, 2018 – Jan 17, 2019"))
                    {
                        builder.MoveTo(each);
                        Style style = doc.Styles.Add(StyleType.Paragraph, "DudeDocumentSubSubTitle");
                        style.Font.Name = "Oswald Bold";
                        style.Font.Color = Color.Green;
                        builder.ParagraphFormat.Style = style;
                        builder.ParagraphFormat.Alignment = ParagraphAlignment.Justify;
                    }
                }
            }
        }


        doc.Save(@"Output.docx");

#5

@marcelluswalace,

The following code will only work when saving to fixed-page formats such as PDF, XPS, Image formats, HtmlFixed format etc.

Document doc = new Document("E:\\WordDocWithFonts\\output.docx");

FontSettings fs = new FontSettings();

ArrayList fontSources = new ArrayList(fs.GetFontsSources());
FolderFontSource folderFontSource = new FolderFontSource("E:\\WordDocWithFonts\\Fonts\\Oswald\\", true);
fontSources.Add(folderFontSource);
FontSourceBase[] updatedFontSources = (FontSourceBase[])fontSources.ToArray(typeof(FontSourceBase));
fs.SetFontsSources(updatedFontSources);

doc.FontSettings = fs;

doc.Save("E:\\WordDocWithFonts\\output.pdf");

Aspose.Words can assign correct font name to certain text but in order for MS Word to properly render that text, you need to install fonts. There is another option you can try is try to embed the Font files inside the DOCX file.

Document doc = new Document("E:\\WordDocWithFonts\\output.docx");

doc.FontInfos.EmbedTrueTypeFonts = true;
doc.FontInfos.EmbedSystemFonts = true;

doc.Save("E:\\WordDocWithFonts\\19.1.docx");

#6

this doesnt change anything how to make sure that font are embedded?


#7

It is not specified in your documentation that the above code is not working for docx. We chose to use aspose assuming what we read in the doc. so i really hope you guys will help us to solve this


#8

i ve done some progress here. Can you confirm that we dont need MS WORD to be install on the local machine to generate a word document? Thanks in advance for you answer


#9

@marcelluswalace,

Yes, you do not need to install MS Word.

Aspose.Words does not depend on MS Word. It can process/manipulate documents without utilizing Microsoft Office.