Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
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. However, the default alignment of a 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 many TextBoxes to adjust, you may be in trouble. Don’t worry, Aspose.Cells provides 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();
// Output 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 the 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 for the target workbook
Workbook destWb;
// Access the first worksheet from the collection
auto sheet = destWb.GetWorksheets().Get(0);
// Create a new TextBox
TextBox textBox = sheet.GetShapes().AddShape(MsoDrawingType::TextBox, 1, 0, 1, 0, 200, 200);
// Alternatively, a TextBox can be added using the following line as well
// TextBox textBox = sheet.GetShapes().AddTextBox(1, 0, 1, 0, 200, 200);
// Use the HTML string from the template file's TextBox
textBox.SetHtmlText(sourceTextBox.GetHtmlText());
// Save the workbook to disc
destWb.Save(outputFilePath);
std::cout << "TextBox copied and saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.