セル内のレイアウト

このチュートリアルでは、Aspose.Words for .NET を使用して Word 文書の表のセル内に図形をレイアウトする方法について説明します。図形のプロパティを調整し、レイアウト オプションを使用することで、セル内の図形の位置と外観を制御できます。

前提条件

このチュートリアルを実行するには、次のものが必要です。

  • Aspose.Words for .NET ライブラリがインストールされています。
  • C# と Word 文書を使用した Words Processing に関する基本的な知識。

ステップ1: ドキュメントディレクトリを設定する

まず、ドキュメントディレクトリへのパスを設定します。"YOUR DOCUMENT DIRECTORY"ドキュメントを保存するディレクトリへの実際のパスを入力します。

string dataDir = "YOUR DOCUMENT DIRECTORY";

ステップ2: 新しいドキュメントとDocumentBuilderを作成する

新しいインスタンスを作成するDocumentクラスとDocumentBuilderドキュメントを操作するオブジェクト。

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

ステップ3: テーブルを作成する

使用StartTable, EndTable, InsertCell 、 そしてWriteDocumentBuilderオブジェクトを使用して表を作成します。目的の行の高さと高さのルールを設定するには、RowFormatプロパティ。

builder.StartTable();
builder.RowFormat.Height = 100;
builder.RowFormat.HeightRule = HeightRule.Exactly;
for (int i = 0; i < 31; i++)
{
    if (i != 0 && i % 7 == 0) builder.EndRow();
    builder.InsertCell();
    builder.Write("Cell contents");
}
builder.EndTable();

ステップ4: 図形を作成して書式設定する

作成するShapeオブジェクトを作成し、そのプロパティを設定して透かしを定義します。セル内にレイアウトする図形を設定するには、IsLayoutInCell財産。

Shape watermark = new Shape(doc, ShapeType.TextPlainText)
{
    RelativeHorizontalPosition = RelativeHorizontalPosition.Page,
    RelativeVerticalPosition = RelativeVerticalPosition.Page,
    IsLayoutInCell = true,
    Width = 300,
    Height = 70,
    HorizontalAlignment = HorizontalAlignment.Center,
    VerticalAlignment = VerticalAlignment.Center,
    Rotation = -40
};

ステップ5: 形状をカスタマイズする

透かし図形の外観とテキストをカスタマイズするには、次のようなプロパティを設定します。FillColor, StrokeColor, TextPath, Name, WrapTypeなど

watermark.FillColor = Color.Gray;
watermark.StrokeColor = Color.Gray;
watermark.TextPath.Text = "watermarkText";
watermark.TextPath.FontFamily = "Arial";
watermark.Name = $"WaterMark_{Guid.NewGuid()}";
watermark.WrapType = WrapType.None;

ステップ6: ドキュメントに図形を挿入する

透かし図形を文書に挿入するには、InsertNode方法のDocumentBuilderオブジェクト。MoveToドキュメント内の最後の実行の後に配置する方法。

Run run = doc.GetChildNodes(NodeType.Run, true)[doc.GetChildNodes(NodeType.Run, true).Count - 1] as Run;
builder.MoveTo(run);
builder.InsertNode(watermark);

ステップ7: ドキュメントを保存する

指定されたディレクトリにドキュメントを保存するには、Saveメソッド。適切なファイル拡張子を持つファイル名を指定します。この例では、ドキュメントを「WorkingWithShapes.LayoutInCell.docx」として保存します。

doc.CompatibilityOptions.OptimizeFor(MsWordVersion.Word2010);
doc

.Save(dataDir + "WorkingWithShapes.LayoutInCell.docx");

Aspose.Words for .NET を使用したセル内レイアウトのサンプル ソース コード

	//ドキュメントディレクトリへのパス
	string dataDir = "YOUR DOCUMENT DIRECTORY";

	Document doc = new Document();
	DocumentBuilder builder = new DocumentBuilder(doc);
	builder.StartTable();
	builder.RowFormat.Height = 100;
	builder.RowFormat.HeightRule = HeightRule.Exactly;
	for (int i = 0; i < 31; i++)
	{
		if (i != 0 && i % 7 == 0) builder.EndRow();
		builder.InsertCell();
		builder.Write("Cell contents");
	}
	builder.EndTable();
	Shape watermark = new Shape(doc, ShapeType.TextPlainText)
	{
		RelativeHorizontalPosition = RelativeHorizontalPosition.Page,
		RelativeVerticalPosition = RelativeVerticalPosition.Page,
		IsLayoutInCell = true, //図形をセル内に配置した場合は、表のセルの外側に図形を表示します。
		Width = 300,
		Height = 70,
		HorizontalAlignment = HorizontalAlignment.Center,
		VerticalAlignment = VerticalAlignment.Center,
		Rotation = -40
	};
	watermark.FillColor = Color.Gray;
	watermark.StrokeColor = Color.Gray;
	watermark.TextPath.Text = "watermarkText";
	watermark.TextPath.FontFamily = "Arial";
	watermark.Name = $"WaterMark_{Guid.NewGuid()}";
	watermark.WrapType = WrapType.None;
	Run run = doc.GetChildNodes(NodeType.Run, true)[doc.GetChildNodes(NodeType.Run, true).Count - 1] as Run;
	builder.MoveTo(run);
	builder.InsertNode(watermark);
	doc.CompatibilityOptions.OptimizeFor(MsWordVersion.Word2010);
	doc.Save(dataDir + "WorkingWithShapes.LayoutInCell.docx");

これで完了です。Aspose.Words for .NET を使用して、Word 文書の表のセル内に図形を正常にレイアウトできました。