如何将文本对齐应用于文本框

Contents
[ ]

TextBoxes 可以提高我们的文档和图表的表现力,对 TextBox 的不同部分应用不同的对齐方式可以帮助突出读者的兴趣点。但是 TextBox 的默认对齐并不能满足我们所有的需求。为此,您可能需要调整每个 TextBox 以满足您的目标要求。如果您没有很多要调整的 TextBox 对象,那么您很幸运。如果有那么多TextBox要调整,我想你会很麻烦。现在别担心,Aspose.Cells提供了这样一个 API 接口来帮助你做到这一点。

以下示例代码将文本对齐应用于 TextBox。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = "";
File file = new File(dataDir);
if(!file.exists())
file.mkdir();
//Instantiating a Workbook object
Workbook workbook = new Workbook();
ShapeCollection shapes = workbook.getWorksheets().get(0).getShapes();
//add a TextBox
Shape shape = shapes.addTextBox(2, 0, 2, 0, 50, 120);
shape.setText("This is a test.");
//set alignment
shape.setTextHorizontalAlignment(TextAlignmentType.CENTER);
shape.setTextVerticalAlignment(TextAlignmentType.CENTER);
//Save the excel file.
workbook.save(dataDir + "result.xlsx");

您还可以使用适当的 HTML 文本更改文本框形状内某些文本的文本对齐方式。以下示例代码将文本对齐方式应用于文本框内的部分文本。

源文件

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Intialize an object of the Workbook class to load template file
Workbook sourceWb = new Workbook("SampleTextboxExcel2016.xlsx");
// Access the target textbox whose text is to be aligned
Shape sourceTextBox = sourceWb.getWorksheets().get(0).getShapes().get(0);
// Create and object of the target workbook
Workbook destWb = new Workbook();
// Access first worksheet from the collection
Worksheet _sheet = destWb.getWorksheets().get(0);
// Create new textbox
TextBox _textBox = (TextBox)_sheet.getShapes().addShape(6,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("Output.xlsx");