Text Formatting
Highlight Text
New HighlightText method has been added to ITextFrame and TextFrame classes. It allows to highlight text part with background color using text sample, similar to Text Highlight Color tool in PowerPoint 2019.
The code snippet below shows how to use this feature:
Highlight Text using Regular Expression
New HighlightRegex method has been added to ITextFrame and TextFrame classes. It allows to highlight text part with background color using regex, similar to Text Highlight Color tool in PowerPoint 2019.
The code snippet below shows how to use this feature:
Set Text Background Color
Aspose.Slides allows you to specify your preferred color for the background of a text.
This C++ code shows you how to set the background color for an entire text:
{
auto pres = System::MakeObject<Presentation>();
System::SharedPtr<IAutoShape> autoShape = pres->get_Slide(0)->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50.0f, 50.0f, 200.0f, 100.0f);
auto paragraphs = autoShape->get_TextFrame()->get_Paragraphs();
paragraphs->Clear();
System::SharedPtr<Paragraph> para = System::MakeObject<Paragraph>();
auto portion1 = System::MakeObject<Portion>(u"Black");
portion1->get_PortionFormat()->set_FontBold(NullableBool::True);
auto portion2 = System::MakeObject<Portion>(u" Red ");
auto portion3 = System::MakeObject<Portion>(u"Black");
portion3->get_PortionFormat()->set_FontBold(NullableBool::True);
auto paragraphPortions = para->get_Portions();
paragraphPortions->Add(portion1);
paragraphPortions->Add(portion2);
paragraphPortions->Add(portion3);
paragraphs->Add(para);
pres->Save(u"text.pptx", SaveFormat::Pptx);
}
{
auto pres = System::MakeObject<Presentation>(u"text.pptx");
auto autoShape = System::ExplicitCast<IAutoShape>(pres->get_Slide(0)->get_Shape(0));
auto portions = autoShape->get_TextFrame()->get_Paragraph(0)->get_Portions();
for (auto&& portion : portions)
{
portion->get_PortionFormat()->get_HighlightColor()->set_Color(System::Drawing::Color::get_Blue());
}
pres->Save(u"text-red.pptx", SaveFormat::Pptx);
}
This C++ code shows you how to set the background color for only a portion of a text:
{
auto pres = System::MakeObject<Presentation>();
System::SharedPtr<IAutoShape> autoShape = pres->get_Slide(0)->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50.0f, 50.0f, 200.0f, 100.0f);
auto paragraphs = autoShape->get_TextFrame()->get_Paragraphs();
paragraphs->Clear();
System::SharedPtr<Paragraph> para = System::MakeObject<Paragraph>();
auto portion1 = System::MakeObject<Portion>(u"Black");
portion1->get_PortionFormat()->set_FontBold(NullableBool::True);
auto portion2 = System::MakeObject<Portion>(u" Red ");
auto portion3 = System::MakeObject<Portion>(u"Black");
portion3->get_PortionFormat()->set_FontBold(NullableBool::True);
auto paragraphPortions = para->get_Portions();
paragraphPortions->Add(portion1);
paragraphPortions->Add(portion2);
paragraphPortions->Add(portion3);
paragraphs->Add(para);
pres->Save(u"text.pptx", SaveFormat::Pptx);
}
{
auto pres = System::MakeObject<Presentation>(u"text.pptx");
auto autoShape = System::ExplicitCast<IAutoShape>(pres->get_Slide(0)->get_Shape(0));
auto predicate = [](System::SharedPtr<IPortion> portion) -> bool {
return portion->get_Text().Contains(u"Red");
};
auto portions = autoShape->get_TextFrame()->get_Paragraph(0)->get_Portions();
System::SharedPtr<IPortion> redPortion;
for (auto&& portion : portions)
if (predicate(portion))
redPortion = portion;
redPortion->get_PortionFormat()->get_HighlightColor()->set_Color(System::Drawing::Color::get_Red());
pres->Save(u"text-red.pptx", SaveFormat::Pptx);
}
Align Text Paragraph
Text formatting is one of the key elements while creating any kind of documents or presentations. We know that Aspose.Slides for C++ supports adding text to slides but in this topic, we will see that how can we control the alignment of the text paragraphs in a slide. Please follow the steps below to align text paragraphs using Aspose.Slides for C++ :
- Create an instance of Presentation class.
- Obtain the reference of a slide by using its Index.
- Access the Placeholder shapes present in the slide and typecast them as an AutoShape.
- Get the Paragraph (that needs to be aligned) from the TextFrame exposed by AutoShape.
- Align the Paragraph. A paragraph can be aligned to Right, Left, Center & Justify.
- Write the modified presentation as a PPTX file.
The implementation of the above steps is given below.
Set Transparency for Text
This article demonstrates how to set transparency property to any text shape using Aspose.Slides. In order to set the transparency to text. Please follow the steps below:
- Create an instance of Presentation class.
- Get reference of a slide.
- Set shadow color
- Write the presentation as a PPTX file.
The implementation of the above steps is given below.
Set Character Spacing for Text
Aspose.Slides allows you to set the space between letters in a textbox. This way, you get to adjust the visual density of a line or block of text by expanding or condensing the spacing between characters.
This C++ code shows you how to expand the spacing for one line of text and condense the spacing for another line:
auto presentation = System::MakeObject<Presentation>(u"in.pptx");
auto slide = presentation->get_Slides()->idx_get(0);
auto textBox1 = System::ExplicitCast<IAutoShape>(slide->get_Shapes()->idx_get(0));
auto textBox2 = System::ExplicitCast<IAutoShape>(slide->get_Shapes()->idx_get(1));
textBox1->get_TextFrame()->get_Paragraph(0)->get_ParagraphFormat()->get_DefaultPortionFormat()->set_Spacing(20.0f); // expand
textBox2->get_TextFrame()->get_Paragraph(0)->get_ParagraphFormat()->get_DefaultPortionFormat()->set_Spacing(-2.0f); // condense
presentation->Save(u"out.pptx", SaveFormat::Pptx);
Manage Paragraph’s Font Properties
Presentations usually contain both text and images. The text can be formatted in a various way, either to highlight specific sections and words, or to conform with corporate styles. Text formatting helps users vary the look and feel of the presentation content. This article shows how to use Aspose.Slides for C++ to configure the font properties of paragraphs of text on slides. To manage the font properties of a paragraph using Aspose.Slides for C++ :
- Create an instance of the
Presentation
class. - Obtain a slide’s reference by using its index.
- Access the Placeholder shapes in the slide and typecast them to AutoShape.
- Get the Paragraph from the TextFrame exposed by AutoShape.
- Justify the paragraph.
- Access a Paragraph’s text Portion.
- Define the font using FontData and set the Font of the text Portion accordingly.
- Set the font to bold.
- Set the font to italic.
- Set the font color using the FillFormat exposed by the Portion object.
- Write the modified presentation to a PPTX file.
The implementation of the above steps is given below. It takes an unadorned presentation and formats the fonts on one of the slides.
Manage Font Family of Text
A portion is used to hold text with similar formatting style in a paragraph. This article shows how to use Aspose.Slides for C++ to create a textbox with some text and then define a particular font, and various other properties of the font family category. To create a textbox and set font properties of the text in it:
- Create an instance of the
Presentation
class. - Obtain the reference of a slide by using its index.
- Add an AutoShape of the type Rectangle to the slide.
- Remove the fill style associated with the AutoShape.
- Access the AutoShape’s TextFrame.
- Add some text to the TextFrame.
- Access the Portion object associated with the TextFrame.
- Define the font to be used for the Portion.
- Set other font properties like bold, italic, underline, color and height using the relevant properties as exposed by the Portion object.
- Write the modified presentation as a PPTX file.
The implementation of the above steps is given below.
Set Font Size for Text
Aspose.Slides allows you to choose your preferred font size for existing text in a paragraph and other texts that may be added to the paragraph later.
This C++ code shows you how to set the font size for texts contained in a paragraph:
auto presentation = System::MakeObject<Presentation>(u"example.pptx");
// Gets the first shape, for example.
auto shape = presentation->get_Slide(0)->get_Shape(0);
if (System::ObjectExt::Is<IAutoShape>(shape))
{
auto autoShape = System::ExplicitCast<IAutoShape>(shape);
// Gets the first paragraph, for example.
auto paragraph = autoShape->get_TextFrame()->get_Paragraph(0);
// Sets the default font size to 20 pt for all text portions in the paragraph.
paragraph->get_ParagraphFormat()->get_DefaultPortionFormat()->set_FontHeight(20.0f);
// Sets the font size to 20 pt for current text portions in the paragraph.
for (auto&& portion : paragraph->get_Portions())
{
portion->get_PortionFormat()->set_FontHeight(20.0f);
}
}
presentation->Save(u"output.pptx", SaveFormat::Pptx);
Set Text Rotation
Aspose.Slides for C++ allows developers to rotate the text. Text could be set to appear as Horizontal, Vertical, Vertical270, WordArtVertical, EastAsianVertical, MongolianVertical or WordArtVerticalRightToLeft. To rotate the text of any TextFrame, please follow the steps below:
- Create an instance of
Presentation
class. - Access the first slide.
- Add any Shape to the slide.
- Access the TextFrame.
- Rotate the text.
- Save file to disk.
Tabs and EffectiveTabs in Presentation
- EffectiveTabs.ExplicitTabCount (2 in our case) property is equal to Tabs.Count.
- EffectiveTabs collection includes all tabs (from Tabs collection and default tabs)
- EffectiveTabs.ExplicitTabCount (2 in our case) property is equal to Tabs.Count.
- EffectiveTabs.DefaultTabSize (294) property shows distance between default tabs (3 and 4 in our example).
- EffectiveTabs.GetTabByIndex(index) with index = 0 will return first explicit tab (Position = 731), index = 1 - second tab (Position = 1241). If you try to get next tab with index = 2 it will return first default tab (Position = 1470) and etc.
- EffectiveTabs.GetTabAfterPosition(pos) used for getting next tabulation after some text. For example you have text: “Helloworld!”. To render such text you should know where to start draw “world!”. At first, you should calculate length of “Hello” in pixels and call GetTabAfterPosition with this value. You will get next tab position to draw “world!”.
Line Spacing of Paragraph
Aspose.Slides provides properties under ParagraphFormat
—SpaceAfter
, SpaceBefore
and SpaceWithin
—that allow you to manage the line spacing for a paragraph. The three properties are used this way:
- To specify the line spacing for a paragraph in percentage, use a positive value.
- To specify the line spacing for a paragraph in points, use a negative value.
For example, you can apply a 16pt line spacing for a paragraph by setting the SpaceBefore
property to -16.
This is how you specify the line spacing for a specific paragraph:
- Load a presentation containing an AutoShape with some text in it.
- Get a slide’s reference through its index.
- Access the TextFrame.
- Access the Paragraph.
- Set the Paragraph properties.
- Save the presentation.
This C++ code shows you how to specify the line spacing for a paragraph:
// The path to the documents directory.
System::String dataDir = GetDataPath();
// Create an instance of Presentation class
auto presentation = System::MakeObject<Presentation>(dataDir + u"Fonts.pptx");
// Obtain a slide's reference by its index
auto sld = presentation->get_Slides()->idx_get(0);
// Access the TextFrame
auto tf1 = (System::ExplicitCast<IAutoShape>(sld->get_Shapes()->idx_get(0)))->get_TextFrame();
// Access the Paragraph
auto para = tf1->get_Paragraphs()->idx_get(0);
// Set properties of Paragraph
para->get_ParagraphFormat()->set_SpaceWithin(80.0f);
para->get_ParagraphFormat()->set_SpaceBefore(40.0f);
para->get_ParagraphFormat()->set_SpaceAfter(40.0f);
// Save Presentation
presentation->Save(dataDir + u"LineSpacing_out.pptx", SaveFormat::Pptx);
Set AutofitType Property of Text Frame
In this topic, we will explore the different formatting properties of text frame. This article covers how to Set the AutofitType property of text frame, anchor of text and rotating the text in presentation. Aspose.Slides for C++ allows developers to set AutofitType property of any text frame. AutofitType could be set to Normal or Shape. If set to Normal then shape will remain the same whereas the text will be adjusted without causing the shape to change itself whereas If AutofitType is set to shape, then shape will be modified such that only required text is contained in it. To set the AutofitType property of a text frame, please follow the steps below:
- Create an instance of Presentation class.
- Access the first slide.
- Add any shape to the slide.
- Access the TextFrame.
- Set the AutofitType of the TextFrame.
- Save file to disk.
Set Anchor of TextFrame
Aspose.Slides for C++ allows developers to Anchor of any TextFrame. TextAnchorType specifies that where is that text placed in the shape. TextAnchorType could be set to Top, Center, Bottom, Justified or Distributed. To set Anchor of any TextFrame, please follow the steps below:
- Create an instance of
Presentation
class. - Access the first slide.
- Add any shape to the slide.
- Access the TextFrame.
- Set TextAnchorType of the TextFrame.
- Save file to disk.
Set Custom Rotation Angle for TextFrame
Aspose.Slides for C++ now supports, Setting custom rotation angle for textframe. In this topic, we will see with example how to set the RotationAngle property in Aspose.Slides. The new property RotationAngle has been added to IChartTextBlockFormat and ITextFrameFormat interfaces, allows to set the custom rotation angle for textframe. In order to set the RotationAngle property, Please follow the steps below:
- Create an instance of Presentation class.
- Add a chart on slide.
- Set RotationAngle property.
- Write the presentation as a PPTX file.
In the example given below, we set the RotationAngle property.
Set Proofing Language
Aspose.Slides provides the LanguageId property (exposed by the PortionFormat class) to allow you to set the proofing language for a PowerPoint document. The proofing language is the language for which spellings and grammar in the PowerPoint are checked.
This C++ code shows you how to set the proofing language for a PowerPoint:
System::SharedPtr<Presentation> pres = System::MakeObject<Presentation>(pptxFileName);
System::SharedPtr<AutoShape> autoShape = System::ExplicitCast<AutoShape>(pres->get_Slide(0)->get_Shape(0));
System::SharedPtr<IParagraph> paragraph = autoShape->get_TextFrame()->get_Paragraph(0);
System::SharedPtr<IPortionCollection> portions = paragraph->get_Portions();
portions->Clear();
System::SharedPtr<Portion> newPortion = System::MakeObject<Portion>();
System::SharedPtr<IFontData> font = System::MakeObject<FontData>(u"SimSun");
System::SharedPtr<IPortionFormat> portionFormat = newPortion->get_PortionFormat();
portionFormat->set_ComplexScriptFont(font);
portionFormat->set_EastAsianFont(font);
portionFormat->set_LatinFont(font);
portionFormat->set_LanguageId(u"zh-CN");
// set the Id of a proofing language
newPortion->set_Text(u"1。");
portions->Add(newPortion);
Set Default Language
This C++ code shows you how to set the default language for an entire PowerPoint presentation:
System::SharedPtr<LoadOptions> loadOptions = System::MakeObject<LoadOptions>();
loadOptions->set_DefaultTextLanguage(u"en-US");
System::SharedPtr<Presentation> pres = System::MakeObject<Presentation>(loadOptions);
// Adds a new rectangle shape with text
System::SharedPtr<IAutoShape> shp = pres->get_Slide(0)->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50.0f, 50.0f, 150.0f, 50.0f);
System::SharedPtr<ITextFrame> textFrame = shp->get_TextFrame();
textFrame->set_Text(u"New Text");
// Checks the first portion language
System::Console::WriteLine(textFrame->get_Paragraph(0)->get_Portion(0)->get_PortionFormat()->get_LanguageId());
Set Default Text Style
If you need to apply the same default text formatting to all text elements of a presentation at once, then you can use the get_DefaultTextStyle
method from the IPresentation interface and set the preferred formatting. The code example below shows how to set the default bold font (14 pt) for the text on all slides in a new presentation.
auto presentation = MakeObject<Presentation>();
// Get the top level paragraph format.
auto paragraphFormat = presentation->get_DefaultTextStyle()->GetLevel(0);
if (paragraphFormat != NULL) {
paragraphFormat->get_DefaultPortionFormat()->set_FontHeight(14);
paragraphFormat->get_DefaultPortionFormat()->set_FontBold(NullableBool::True);
}
presentation->Save(u"DefaultTextStyle.pptx", SaveFormat::Pptx);
presentation->Dispose();