1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C#、VB.NETでPowerPointドキュメント内のテキストを配置する

Posted at

PPTのテキスト配置(Text Alignment)は、テキスト書式設定ツールの一種として利用されます。これにより、テキストを左揃え、右揃え、中央揃え、両端揃えといったスタイルで整列させることができます。デフォルトではテキストは右揃えになっていますが、ユーザーが任意に配置をカスタマイズすることも可能です。以下のスクリーンショットは、配置スタイルごとの効果を示しており、例えば「Left」は左揃え、「Center」は中央揃えになっていることが分かります。

Spire.Presentation for .NETは、Microsoft Officeの自動化を使わずにPowerPointドキュメントを操作できる、プロフェッショナルな.NET向けPPTコンポーネントです。この記事では、Spire.Presentation for .NETを使用してC#またはVB.NETでテキストを配置する方法を紹介します。

方法の概要

まず、新しいPPTドキュメントを作成し、スライドを設定します。次に、すべての配置スタイルをループ処理し、新しい段落を作成してそれぞれの配置を表示します。最後に、段落にフォントと塗りつぶしスタイルを設定します。

Spire.Presentation for .NETをダウンロードしてインストールし、以下のコードを使用して、PowerPointドキュメント内のテキストを整列する方法を体験してみてください。

完全なコード例:

using System;
using System.Drawing;
using Spire.Presentation;
using Spire.Presentation.Drawing;

namespace Alignment
{
    class Program
    {
        static void Main(string[] args)
        {
            Presentation presentation = new Presentation();

            // 背景画像を設定
            string ImageFile = @"bg.png";
            RectangleF rect = new RectangleF(0, 0, presentation.SlideSize.Size.Width, presentation.SlideSize.Size.Height);
            presentation.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect);
            presentation.Slides[0].Shapes[0].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite;

            // 新しい図形を追加
            IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 70, 600, 400));
            shape.TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Left;
            shape.Fill.FillType = FillFormatType.None;
            shape.TextFrame.Text = "Demo about Alignment";

            foreach (TextAlignmentType textAlign in Enum.GetValues(typeof(TextAlignmentType)))
            {
                TextRange textRange = new TextRange(textAlign.ToString());
                TextParagraph paragraph = new TextParagraph();
                paragraph.TextRanges.Append(textRange);
                paragraph.Alignment = textAlign;
                shape.TextFrame.Paragraphs.Append(paragraph);
            }

            // フォントと塗りつぶしスタイルを設定
            foreach (TextParagraph paragraph in shape.TextFrame.Paragraphs)
            {
                paragraph.TextRanges[0].LatinFont = new TextFont("Arial Black");
                paragraph.TextRanges[0].Fill.FillType = FillFormatType.Solid;
                paragraph.TextRanges[0].Fill.SolidColor.Color = Color.Black;
            }

            // ファイルを保存
            presentation.SaveToFile("alignment.pptx", FileFormat.Pptx2010);
            System.Diagnostics.Process.Start("alignment.pptx");
        }
    }
}

一時ライセンスの申請

生成されたドキュメントに評価用メッセージを表示させたくない場合、または機能制限を解除したい場合は、30日間の試用ライセンスを申請してください。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?