テキストボックスにテキストの配置を適用/設定する方法

Contents
[ ]

TextBox は、ドキュメントや図の表現力を向上させることができ、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-.NET
// The path to the documents directory.
string dataDir = "";
if (!System.IO.Directory.Exists(dataDir))
{
System.IO.Directory.CreateDirectory(dataDir);
}
//Instantiating a Workbook object
Workbook workbook = new Workbook();
ShapeCollection shapes = workbook.Worksheets[0].Shapes;
//add a TextBox
Shape shape = shapes.AddTextBox(2, 0, 2, 0, 50, 120);
shape.Text = "This is a test.";
//set alignment
shape.TextHorizontalAlignment = TextAlignmentType.Center;
shape.TextVerticalAlignment = TextAlignmentType.Center;
//Save the excel file.
workbook.Save(dataDir + "result.xlsx");

また、適切な HTML テキストを使用して、TextBox 図形内の一部のテキストのテキスト配置を変更することもできます。次のサンプル コードは、TextBox 内の部分的なテキストにテキスト配置を適用します。

ソースファイル

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// 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
var sourceTextBox = sourceWb.Worksheets[0].Shapes[0];
// Create and object of the target workbook
var destWb = new Workbook();
// Access first worksheet from the collection
var _sheet = destWb.Worksheets[0];
//Create new textbox
TextBox _textBox = (TextBox)_sheet.Shapes.AddShape( MsoDrawingType.TextBox,1, 0, 1, 0, 200, 200);
// Alternatively text box can be added using following line as well
// TextBox _textBox = _sheet.Shapes.AddTextBox(1, 0, 1, 0, 200, 200);
// Use Html string from a template file textbox
_textBox.HtmlText = sourceTextBox.HtmlText;
// Save the workbook on disc
destWb.Save("Output.xlsx");