How to apply/set text alignment to textbox with C++
TextBoxes can improve the expressiveness of our documents and diagrams, and applying different alignments to different parts of a TextBox can help highlight points of interest to readers. But the default alignment of TextBox does not meet all our needs. For this, you may need to adjust each TextBox to meet your target requirements. If you don’t have a lot of TextBox objects to tweak, you’re in luck. If there are so many TextBoxes to adjust, I think you will be in trouble. Don’t worry now, Aspose.Cells provides such an API interface to help you do just that.
The following sample code applies text alignment to a TextBox.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace Aspose::Cells::Drawing;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Create a new workbook
Workbook workbook;
// Get the shapes collection from the first worksheet
ShapeCollection shapes = workbook.GetWorksheets().Get(0).GetShapes();
// Add a TextBox to the worksheet
Shape shape = shapes.AddTextBox(2, 0, 2, 0, 50, 120);
// Set the text of the TextBox
shape.SetText(u"This is a test.");
// Set the horizontal and vertical alignment of the text
shape.SetTextHorizontalAlignment(TextAlignmentType::Center);
shape.SetTextVerticalAlignment(TextAlignmentType::Center);
// Save the workbook to the output directory
workbook.Save(outDir + u"result.xlsx");
std::cout << "TextBox added and workbook saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
You can also change the text alignment of some text inside a TextBox shape with the appropriate HTML text. The following sample code applies the text alignment to partial text inside the TextBox.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace Aspose::Cells::Drawing;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input excel file
U16String inputFilePath = srcDir + u"SampleTextboxExcel2016.xlsx";
// Path of output excel file
U16String outputFilePath = outDir + u"Output.xlsx";
// Initialize an object of the Workbook class to load template file
Workbook sourceWb(inputFilePath);
// Access the target textbox whose text is to be aligned
auto sourceTextBox = sourceWb.GetWorksheets().Get(0).GetShapes().Get(0);
// Create an object of the target workbook
Workbook destWb;
// Access first worksheet from the collection
auto sheet = destWb.GetWorksheets().Get(0);
// Create new textbox
TextBox textBox = sheet.GetShapes().AddShape(MsoDrawingType::TextBox, 1, 0, 1, 0, 200, 200);
// Alternatively text box can be added using following line as well
// TextBox textBox = sheet.GetShapes().AddTextBox(1, 0, 1, 0, 200, 200);
// Use Html string from a template file textbox
textBox.SetHtmlText(sourceTextBox.GetHtmlText());
// Save the workbook on disc
destWb.Save(outputFilePath);
std::cout << "Textbox copied and saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}