Word文書のテキストボックスは、ページ内の任意の位置に配置できるテキストコンテナです。段落の傍にテキストを追加したり、関連情報を与えたり、特定の内容を強調するために使用されることが多いです。テキストボックスに文字や画像を挿入することができます。この記事では、無料の Free Spire.Doc for .NET を使用して、Word文書にテキストボックスを追加したり、文書からテキストボックスを削除したりする方法を説明します。
【依存関係の追加】
まず、Free Spire.Doc for.NET パッケージに含まれるDLLファイルを.NETプロジェクトにリファレンスとして追加する必要があります。DLLファイルは、このリンクからダウンロードするか、NuGet経由でインストールすることができます。
PM> Install-Package FreeSpire.Doc
Word文書にテキストボックスを挿入する
Free Spire.Doc for .NETは、指定した段落にテキストボックスを挿入する Paragraph.AppendTextBox(float width, float height) メソッドを提供します。テキストボックスが挿入された後、Free Spire.Doc for .NETは、テキストボックスのプロパティを設定してフォーマットする TextBox クラスも提供します。詳細な手順は以下の通りです。
- Document のインスタンスを作成し、Document.LoadFromFile() メソッドを使用して Word 文書を読み込みます。
- Document.Sections[] プロパティで最初のセクションを取得し、Section.AddParagraph() メソッドを使用してセクションにパラグラフを追加します。
- Paragraph.AppendTextBox(float width, float height) メソッドを使用して、段落にテキストボックスを追加します。
- TextBox.Format プロパティでテキストボックスのフォーマットを取得し、TextBoxFormatクラスのプロパティでテキストボックスの折り返しタイプ、位置、ボーダー色、塗りつぶし色などを設定します。
- TextBox.Body.AddParagraph() メソッドを使用してテキストボックスに段落を追加し、その揃えを設定します。
- Paragraph.AppendPicture() メソッドを使用して段落に画像を挿入し、挿入する画像のサイズを設定します。
- Paragraph.AppendText() を使って、テキストボックスにテキストを挿入し、テキストのフォントを設定します。
- Document.SaveToFile() メソッドを使用して、ドキュメントを別のファイルに保存します。
C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace InsertTextbox
{
class Program
{
static void Main(string[] args)
{
//Documentのインスタンスを作成する
Document document = new Document();
//Word文書を読み込む
document.LoadFromFile("C:/テキスト ボックス.docx");
//テキストボックスを挿入し、その折り返しスタイルを設定する
TextBox TB = document.Sections[0].AddParagraph().AppendTextBox(100, 250);
TB.Format.TextWrappingStyle = TextWrappingStyle.Square;
//テキストボックスの位置を設定する
TB.Format.HorizontalOrigin = HorizontalOrigin.RightMarginArea;
TB.Format.HorizontalPosition = -100;
TB.Format.VerticalOrigin = VerticalOrigin.Page;
TB.Format.VerticalPosition = 130f;
//テキストボックスのボーダースタイルと塗りつぶし色を設定する
TB.Format.LineColor = Color.DarkBlue;
TB.Format.FillColor = Color.LightCyan;
//テキストボックスに段落として画像を挿入する
Paragraph para = TB.Body.AddParagraph();
DocPicture picture = para.AppendPicture(@"C:\Logo.png");
//段落の揃えを設定する
para.Format.HorizontalAlignment = HorizontalAlignment.Center;
//挿入する画像の大きさを設定する
picture.Height = 48;
picture.Width = 48;
//テキストボックスへ2段落目としてテキストを挿入する
TextRange TR = para.AppendText("Word文書内のページの任意の位置にテキストボックスを挿入することができます。");
//段落の揃えを設定する
para.Format.HorizontalAlignment = HorizontalAlignment.Center;
//テキストのフォントを設定する
TR.CharacterFormat.FontName = "Yu Mincho";
TR.CharacterFormat.FontSize = 12;
TR.CharacterFormat.Italic = true;
//ドキュメントを保存する
document.SaveToFile("テキストボックスの追加.docx", FileFormat.Docx);
}
}
}
VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System.Drawing
Imports System.Reflection.Metadata
Namespace InsertTextbox
Class Program
Shared Sub Main(ByVal args() As String)
'Documentのインスタンスを作成する
Dim document As Document = New Document()
'Word文書を読み込む
document.LoadFromFile("C:/テキスト ボックス.docx")
'テキストボックスを挿入し、その折り返しスタイルを設定する
Dim TB As TextBox = document.Sections(0).AddParagraph().AppendTextBox(100, 250)
TB.Format.TextWrappingStyle = TextWrappingStyle.Square
'テキストボックスの位置を設定する
TB.Format.HorizontalOrigin = HorizontalOrigin.RightMarginArea
TB.Format.HorizontalPosition = -100
TB.Format.VerticalOrigin = VerticalOrigin.Page
TB.Format.VerticalPosition = 130.0F
'テキストボックスのボーダースタイルと塗りつぶし色を設定する
TB.Format.LineColor = Color.DarkBlue
TB.Format.FillColor = Color.LightCyan
'テキストボックスに段落として画像を挿入する
Dim para As Paragraph = TB.Body.AddParagraph()
Dim picture As DocPicture = para.AppendPicture("C:\Logo.png")
'段落の揃えを設定する
para.Format.HorizontalAlignment = HorizontalAlignment.Center
'挿入する画像の大きさを設定する
picture.Height = 48
picture.Width = 48
'テキストボックスへ2段落目としてテキストを挿入する
Dim TR As TextRange = para.AppendText("Word文書内のページの任意の位置にテキストボックスを挿入することができます。")
'段落の揃えを設定する
para.Format.HorizontalAlignment = HorizontalAlignment.Center
'テキストのフォントを設定する
TR.CharacterFormat.FontName = "Yu Mincho"
TR.CharacterFormat.FontSize = 12
TR.CharacterFormat.Italic = True
'ドキュメントを保存する
document.SaveToFile("テキストボックスの追加.docx", FileFormat.Docx)
End Sub
End Class
End Namespace
Word文書からテキストボックスを削除する
Free Spire.Doc for .NETでは、指定したテキストボックスを削除する Document.TextBoxes.RemoveAt(int index) メソッドを提供しています。Word文書からすべてのテキストボックスを削除したい場合は、Document.TextBoxes.Clear() メソッドを使用することができます。以下の例では、Word文書から最初のテキストボックスを削除する方法を示しています。
- Document のインスタンスを作成します。
- Document.LoadFromFile() メソッドを使って Word 文書を読み込みます。
- Document.TextBoxes.RemoveAt(int index) メソッドを使用して、最初のテキストボックスを削除します。
- Document.SaveToFile() メソッドを使用して、ドキュメントを別のファイルに保存します。
C#
using Spire.Doc;
namespace Removetextbox
{
class Program
{
static void Main(string[] args)
{
//Documentのインスタンスを作成する
Document Doc = new Document();
//Word文書を読み込む
Doc.LoadFromFile("テキストボックスの追加.docx");
//最初のテキストボックスを削除する
Doc.TextBoxes.RemoveAt(0);
//すべてのテキストボックスを削除する
//Doc.TextBoxes.Clear();
//ドキュメントを保存する
Doc.SaveToFile("テキストボックスの削除.docx", FileFormat.Docx);
}
}
}
VB.NET
Imports Spire.Doc
Namespace Removetextbox
Class Program
Shared Sub Main(ByVal args() As String)
'Documentのインスタンスを作成する
Dim Doc As Document = New Document()
'Word文書を読み込む
Doc.LoadFromFile("テキストボックスの追加.docx")
'最初のテキストボックスを削除する
Doc.TextBoxes.RemoveAt(0)
'すべてのテキストボックスを削除する
'Doc.TextBoxes.Clear();
'ドキュメントを保存する
Doc.SaveToFile("テキストボックスの削除.docx", FileFormat.Docx)
End Sub
End Class
End Namespace
Spire.Doc for .NETは、その他にも多くのWord文書処理機能を備えています。詳しくは、Spire.Doc for .NETプログラムガイドをご覧ください。