Spire.Presentation is a professional PowerPoint® compatible library that enables developers to create, read, write, modify, convert and Print PowerPoint documents. Get free and professional technical support for Spire.Presentation for .NET, Java, Android, C++, Python.

Wed Apr 20, 2022 7:57 am

Hello ,

I want to get font of texts but I can not get from neither TextRange nor Paragraph, Is there a way of achieving this.

Code: Select all
Presentation templatePresentation = new Presentation();
            //templatePresentation.LoadFromFile("template-overlays.pptx");
            templatePresentation.LoadFromFile("placeholder_features.pptx");


            //Create New Presentation
            Spire.Presentation.Presentation presentation = new Spire.Presentation.Presentation();
            presentation.Slides.RemoveAt(0);

            //Get slide from template presentation. index:1
            ISlide cloneSlide = templatePresentation.Slides.ToArray().ElementAtOrDefault(0);

            //---
            //Get Chart                       
            IAutoShape textBox = cloneSlide.Shapes.ToArray().FirstOrDefault(x => x.Name == "Title 1") as IAutoShape;

            var text=textBox.TextFrame.Text ;
            var textfontSize = textBox.TextFrame.TextRange.FontHeight;
            var textfont = textBox.TextFrame.TextRange.LatinFont;
            var textAllignment = textBox.TextFrame.AnchoringType;
            Console.WriteLine("***Title1 Text Level***");
            Console.WriteLine("text: "+ text);
            Console.WriteLine("textfontSize: " + textfontSize);
            Console.WriteLine("textfont: "+ textfont);
            Console.WriteLine("textAllignment: "+ textAllignment);
           


            var paragpaphText = textBox.TextFrame.Paragraphs[0].Text;
            var paragpaphTextFontSize = textBox.TextFrame.Paragraphs[0].TextRanges[0].FontHeight;
            var paragpaphTextFont = textBox.TextFrame.Paragraphs[0].TextRanges[0].LatinFont;
            var paragraphAllignment = textBox.TextFrame.Paragraphs[0].Alignment;
            Console.WriteLine("--------------");
            Console.WriteLine("***Title1 Paragraph Level***");
            Console.WriteLine("paragpaphText: " + paragpaphText);
            Console.WriteLine("paragpaphTextFontSize: " + paragpaphTextFontSize);
            Console.WriteLine("paragpaphTextFont: " + paragpaphTextFont);
            Console.WriteLine("paragraphAllignment: " + paragraphAllignment);

numankizil
 
Posts: 26
Joined: Tue Nov 09, 2021 8:00 pm

Wed Apr 20, 2022 10:16 am

Hello,

Thanks for your inquiry.
Thanks for your post. I am looking into the issue now. I will update you about the progress accordingly.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 194
Joined: Mon Dec 27, 2021 2:23 am

Thu Apr 21, 2022 9:28 am

Hello,

Thanks for your patience.
Please refer to the code below to get the font. If there is any other questions, just feel free to write back.
Code: Select all
static void Main(string[] args)
        {
            Presentation templatePresentation = new Presentation();
            //templatePresentation.LoadFromFile("template-overlays.pptx");
            templatePresentation.LoadFromFile(@"placeholder_features.pptx");
            //Create New Presentation
            Spire.Presentation.Presentation presentation = new Spire.Presentation.Presentation();
            presentation.Slides.RemoveAt(0);
            //Get slide from template presentation. index:1
            ISlide cloneSlide = templatePresentation.Slides.ToArray().ElementAtOrDefault(0);
            //Get Chart                       
            IAutoShape textBox = cloneSlide.Shapes.ToArray().FirstOrDefault(x => x.Name == "Title 1") as IAutoShape;
            var paragpaphText = textBox.TextFrame.Paragraphs[0].Text;
            TextRange range = textBox.TextFrame.Paragraphs[0].TextRanges[0];
            var paragpaphTextFontSize = textBox.TextFrame.Paragraphs[0].TextRanges[0].FontHeight;
            var fontName = GetFontNameOfTextRange(range, textBox);
            var paragraphAllignment = textBox.TextFrame.Paragraphs[0].Alignment;
            Console.WriteLine("--------------");
            Console.WriteLine("***Title1 Paragraph Level***");
            Console.WriteLine("paragpaphText: " + paragpaphText);
            Console.WriteLine("paragpaphTextFontSize: " + paragpaphTextFontSize);
            Console.WriteLine("paragpaphTextFont: " + fontName);
            Console.WriteLine("paragraphAllignment: " + paragraphAllignment);

        }

public static string GetFontNameOfTextRange(TextRange range, IAutoShape shape)
        {
            string usefont = "";
            //This textrange has no custom font
            if (range.EastAsianFont == null)
            {
                short level = range.Paragraph.Depth;
                TextParagraphProperties properties = shape.TextFrame.TextStyle.GetListLevelTextStyle(level);
                if (properties == null)
                {
                    usefont = shape.Slide.Theme.FontScheme.MajorFont.LatinFont.FontName;                   
                }
                else
                {
                    TextFont eastAsianFont = properties.DefaultTextRangeProperties.EastAsianFont;
                    usefont = eastAsianFont.FontName;
                }
            }
            //This textrange uses a custom font
            else
            {
                usefont = range.EastAsianFont.FontName;

                if (usefont.Contains("mj"))
                {
                    if (usefont.Contains("ea"))
                    {
                        usefont = shape.Slide.Theme.FontScheme.MajorFont.EastAsianFont.FontName;
                    }
                    else
                    {
                        usefont = shape.Slide.Theme.FontScheme.MajorFont.LatinFont.FontName;
                    }
                }
                else if (usefont.Contains("mn"))
                {
                    if (usefont.Contains("ea"))
                    {
                        usefont = shape.Slide.Theme.FontScheme.MinorFont.EastAsianFont.FontName;
                    }
                    else
                    {
                        usefont = shape.Slide.Theme.FontScheme.MajorFont.LatinFont.FontName;
                    }
                }
               
            }
            return usefont;
        }


Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 194
Joined: Mon Dec 27, 2021 2:23 am

Tue Apr 26, 2022 8:12 am

Hello,

thanks for your answer. It is working I can get fonts.

numankizil
 
Posts: 26
Joined: Tue Nov 09, 2021 8:00 pm

Tue Apr 26, 2022 8:41 am

Hello,

Thanks for your response.
Glad to hear that! If you encounter any issues related to our products in the future, just feel free to contact us.
Wish you all the best!

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 194
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.Presentation