How to set the font family of the text in a textframe

hi there,

I'm a a green hand using Aspose.Slides. Could any one help me to figure out how to set the font family of the text in a textframe?Thanks very much!

Hi Daizy,

Thanks for considering Aspose.Slides.

Please use the code snippet below for setting the font related porperties. The font related properties are either applied on paragraph level for all the portions in paragraph or they can be applied on individual portion level. At parapah level, the font properties can be set by using “DefaultCharacterProperties” property of ParagraphEx class. The code snippet below contains the implementation of setting font properties on portion level an as well as on pargarph level in commented code section.

PresentationEx pres = new PresentationEx("D://ppt//notext.pptx");

//Access the first slide
SlideEx sld = pres.Slides[0];

//Creating AutoShape
int Id = sld.Shapes.AddAutoShape(ShapeTypeEx.Rectangle, 50, 150, 150, 50);

AutoShapeEx ashp = (AutoShapeEx)sld.Shapes[Id];//Creating Slide note instance
TextFrameEx textframe = ashp.TextFrame;
ParagraphEx paragraph = textframe.Paragraphs[0];
PortionEx portion = paragraph.Portions[0];

//Setting Font
FontDataEx fontEx = new FontDataEx("Calibri");

// Change font for a portion of text only
portion.RawLatinFont = fontEx;

// Change default font properties for all portions in a paragraph
// TextCharacterPropertiesEx defProperties = paragraph.DefaultCharacterProperties;
// defProperties.RawLatinFont = fontEx;
//defProperties.FontBold = Aspose.Slides.Pptx.NullableBool.True;
//defProperties.FontHeight = 18;

//Setting Font bold property
portion.FontBold = Aspose.Slides.Pptx.NullableBool.True;

//Setting Font Heihgt property
portion.FontHeight = 18;

//Setting Font Italic property
portion.FontItalic = Aspose.Slides.Pptx.NullableBool.True;

//Setting Portion text
portion.Text = "Welcome to Aspose.Slides";

//Saving Presentation
pres.Save("D://pptx//testpres.pptx", Aspose.Slides.Export.SaveFormat.Pptx);

Thanks and Regards,

Hi Mudassir,

Thanks for your help. But what if I am dealing with ppt other than pptx? Because I need to create a new ppt and pptx doesn't have a constructor recieving no arguments.

Hi Daizy,

Please use the code snippet below for setting font related properties in PPT. Please also visit this documentation link for further details.

Presentation pres = new Presentation();
Slide sld = pres.Slides[0];
Aspose.Slides.Rectangle Header_rect = sld.Shapes.AddRectangle(420, 1345, 4900, 920);

//Hiding the lines of rectangle
Header_rect.AddTextFrame("Hello world");
Header_rect.TextFrame.Paragraphs[0].Portions[0].FontUnderline = true;
Header_rect.TextFrame.Paragraphs[0].Portions[0].FontBold = true;

pres.Fonts[Header_rect.TextFrame.Paragraphs[0].Portions[0].FontIndex].FontName = "Times New Roman";

//Writing the presentation as a PPT file
pres.Write("D:\\modified.ppt");

Thanks and Regards,

Thanks Mudassir,

but the new pres.Fonts you create just have one item,and the Portion[0].FontIndex may be greater than 0, thus it will cause a null reference exception. In ppt version, adding a new FontEntity into the pres.Fonts needs to use another ppt's fonts resouce, does it have to do so? I think it's so unconvenient. I'm looking forward to your reply. :-)

Hi Daizy,

Please follow this link to see how to set new font family for presentation and using that font in presentation. Hope, this will clear your query.

Thanks and Regards,

Thanks a lot!

Hi Daizy,

The following topics from our online documentation explain handling of font related attributes in the presentations:

1) For MS PowerPint 97 - 2003 (PPT) format:

http://www.aspose.com/documentation/.net-components/aspose.slides-for-.net/managing-font-family-of-the-text.html

2) For MS PowerPoint 2007 (PPTX) format:

http://www.aspose.com/documentation/.net-components/aspose.slides-for-.net/managing-font-family-of-the-text-1.html

Best Regards

Hi Mudassir,

I tried below code…

tf.Paragraphs[0].Portions[0];

But I am getting error ‘Paragraphs cannot be resolved’.

How to solve this issue?

Thanks,
Ketan

Hi Ketan,

Thanks for your interest in Aspose.Slides.

If you are getting the paragraph cannot be resolved error then it may be due to the fact that the text frame object that you are using is "null" valued. Please share your source presentation data, so that I may investigate the issue on my end.

Thanks and Regards,

Hi Mudassir,

Thanks for giving quick reply.

Below is my code snippet…

PresentationEx pres = new PresentationEx(“c:\demo.pptx”);
//Access first slide
SlideEx sld = pres.getSlides().get(0);
//Define columns with widths and rows with heights
double[] dblCols = { 200,70,70,70,70,70,70,70,70 };
double[] dblRows = { 40,40,40,40,40,40,40,40,40};

//Add table shape to slide
int idx = sld.getShapes().addTable(10, 50, dblCols, dblRows);
TableEx tbl = (TableEx)sld.getShapes().get(idx);

//Set border format for each cell
for( int i=0;i < tbl.getRows().size();i++ )
for( int j=0;j< tbl.getColumns().size();j++ )
{
CellEx cell= tbl.get(j, i);
cell.getBorderTop().getFillFormat().setFillType(FillTypeEx.SOLID);
cell.getBorderTop().getFillFormat().getSolidFillColor().setColor(Color.black);
cell.getBorderTop().setWidth(1);
cell.getBorderBottom().getFillFormat().setFillType(FillTypeEx.SOLID);
cell.getBorderBottom().getFillFormat().getSolidFillColor().setColor(Color.black);
cell.getBorderBottom().setWidth(1);
cell.getBorderRight().getFillFormat().setFillType(FillTypeEx.SOLID);
cell.getBorderRight().getFillFormat().getSolidFillColor().setColor(Color.black);
cell.getBorderRight().setWidth(1);
cell.getBorderLeft().getFillFormat().setFillType(FillTypeEx.SOLID);
cell.getBorderLeft().getFillFormat().getSolidFillColor().setColor(Color.black);
cell.getBorderLeft().setWidth(1);
}
//Merge cells 1 & 2 of row 1
tbl.mergeCells(tbl.get(0, 0), tbl.get(8, 0), false);
tbl.get(0, 0).setAnchorCenter(true);
TextFrameEx tf = tbl.get(0, 0).getTextFrame();
tf.Paragraphs[0].Portions[0].FontBold = true;
tf.setText(“Metrics”);

tbl.mergeCells(tbl.get(0, 1), tbl.get(8, 1), false);



//Write PPTX to Disk
pres.write(“c:\tableJ.pptx”);


I am getting error at the Bold portion.

Thanks,
Ketan

Hi Ketan,

I have observed the code snippet provided by you. There is a syntax error on the line where you are trying to access the portion and setting the font to bold. Actually the used syntax works for .NET environment. The correct format of the text is as under. Please use the following code snippet.

textFrame.getParagraphs().get(0).getPortions().get(0).setFontBold(true);

Thanks and Regards,