ワークシート内の図形と一緒にテキストを回転する
可能な使用シナリオ
Microsoft Excelで任意の図形にテキストを追加できます。Microsoft Excel 2003という非常に古いバージョンで図形を追加すると、テキストは図形とともに回転しません。ただし、Microsoft Excelの新しいバージョン(2007、2010、2013、または2016など)を使用して図形を追加すると、テキストは図形とともに回転します。ShapeTextAlignment.RotateTextWithShapeプロパティを使用して、テキストが図形と一緒に回転するかどうかを制御できます。これのデフォルト値はtrueです。これはテキストが図形と一緒に回転することを意味しますが、false に設定すると、テキストは図形と一緒に回転しません。
ワークシート内の図形と一緒にテキストを回転する
次のサンプルコードは、三角形の図形とそのテキストが図形とともに回転するサンプルエクセルファイルをロードします。サンプルエクセルファイルをMicrosoft Excelで開き、三角形の図形を回転させると、テキストも一緒に回転します。コード後に、ShapeTextAlignment.RotateTextWithShapeプロパティをfalseに設定し、出力エクセルファイルとして保存します。今度は、出力エクセルファイルをMicrosoft Excelで開いて三角形の図形を回転させると、テキストは図形と一緒に回転しません。参考のために、コードの効果を示す以下のスクリーンショットをご覧ください。
サンプルコード
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Load sample Excel file. | |
Workbook wb = new Workbook(sourceDir + "sampleRotateTextWithShapeInsideWorksheet.xlsx"); | |
//Access first worksheet. | |
Worksheet ws = wb.Worksheets[0]; | |
//Access cell B4 and add message inside it. | |
Cell b4 = ws.Cells["B4"]; | |
b4.PutValue("Text is not rotating with shape because RotateTextWithShape is false."); | |
//Access first shape. | |
Shape sh = ws.Shapes[0]; | |
//Access shape text alignment. | |
Aspose.Cells.Drawing.Texts.ShapeTextAlignment shapeTextAlignment = sh.TextBody.TextAlignment; | |
//Do not rotate text with shape by setting RotateTextWithShape as false. | |
shapeTextAlignment.RotateTextWithShape = false; | |
//Save the output Excel file. | |
wb.Save(outputDir + "outputRotateTextWithShapeInsideWorksheet.xlsx"); |