チュートリアル
簡単にライブラリーを使用するためのチュートリアルコレクション
行間は段落の行間のスペースで、段落の間隔はドキュメント内の段落の前後のスペースです。MS Word では、デフォルトの間隔設定に満足できない場合、手動で間隔を調整することができます。この記事では、Spire.Doc for .NET を使用して、プログラムで Word ドキュメントの行間と段落の間隔を設定する方法について説明します。
まず、Spire.Doc for .NET パッケージに含まれている DLL ファイルを .NET プロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。
PM> Install-Package Spire.Doc
行間や段落の間隔を緩くすると文章が読みやすくなり、行間や段落の間隔を狭くするとドキュメントに多くの文章を収めることができます。以下は、Word ドキュメントの行間と段落の間隔を調整する手順です。
using Spire.Doc;
using Spire.Doc.Documents;
namespace SetSpacing
{
class Program
{
static void Main(string[] args)
{
//Documentのインスタンスを作成する
Document document = new Document();
//セクションを追加する
Section section = document.AddSection();
//段落を追加する
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("Spire.Doc for .NET クラスライブラリ");
paragraph.GetStyle().CharacterFormat.FontName = "BIZ UDGothic";
//別の段落を追加する
Paragraph paragraph1 = section.AddParagraph();
paragraph1.AppendText("Spire.Doc for .NET は、開発者が .NET " +
"(Target .NET Framework、.NET Core、.NET Standard、.NET 5.0、.NET 6.0、 Xamarin & Mono Android) " +
"プラットホームから Word の文書ファイルを迅速かつ高品質で" +
"作成・編集・変換・参照・印刷するために設計された専門的な Word.NET ライブラリです。");
//段落の前に間隔を設定する
paragraph1.Format.BeforeSpacing = 30;
//段落の後に間隔を設定する
paragraph1.Format.AfterSpacing = 30;
//別の段落を追加する
Paragraph paragraph2 = section.AddParagraph();
paragraph2.AppendText("Spire.Doc for .NET は独立した Word.NET コンポーネントなので、" +
"Microsoft Office や Microsoft Word を実行環境にインストールする必要はありません。" +
"また、任意の開発者の .NET アプリケーションに Microsoft Word 文書作成機能を組み入れることができます。");
//段落の行間を設定する
paragraph2.Format.LineSpacing = 25;
//ドキュメントを保存する
document.SaveToFile("間隔の設定.docx", FileFormat.Docx);
}
}
}
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace SetSpacing
Class Program
Shared Sub Main(ByVal args() As String)
'Documentのインスタンスを作成する
Dim document As Document = New Document()
'セクションを追加する
Dim section As Section = document.AddSection()
'段落を追加する
Dim paragraph As Paragraph = section.AddParagraph()
paragraph.AppendText("Spire.Doc for .NET クラスライブラリ")
paragraph.GetStyle().CharacterFormat.FontName = "BIZ UDGothic"
'別の段落を追加する
Dim paragraph1 As Paragraph = section.AddParagraph()
paragraph1.AppendText("Spire.Doc for .NET は、開発者が .NET " +
"(Target .NET Framework、.NET Core、.NET Standard、.NET 5.0、.NET 6.0、 Xamarin & Mono Android) " +
"プラットホームから Word の文書ファイルを迅速かつ高品質で" +
"作成・編集・変換・参照・印刷するために設計された専門的な Word.NET ライブラリです。")
'段落の前に間隔を設定する
paragraph1.Format.BeforeSpacing = 30
'段落の後に間隔を設定する
paragraph1.Format.AfterSpacing = 30
'別の段落を追加する
Dim paragraph2 As Paragraph = section.AddParagraph()
paragraph2.AppendText("Spire.Doc for .NET は独立した Word.NET コンポーネントなので、" +
"Microsoft Office や Microsoft Word を実行環境にインストールする必要はありません。" +
"また、任意の開発者の .NET アプリケーションに Microsoft Word 文書作成機能を組み入れることができます。")
'段落の行間を設定する
paragraph2.Format.LineSpacing = 25
'ドキュメントを保存する
document.SaveToFile("間隔の設定.docx", FileFormat.Docx)
End Sub
End Class
End Namespace
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
Word ドキュメントにおいて、インデントとは、段落本文とページ余白の距離を調整するために使用される段落の書式です。左インデント、右インデント、最初の行のインデント、ぶら下げインデントがあります。左インデントと右インデントは段落のすべての行に適用できますが、最初の行のインデントは段落の最初の行にしか適用できません。ぶら下げインデントについては、段落の最初の行を除くすべての行に適用することができます。この記事では、Spire.Doc for .NET を使用して、プログラムで Word ドキュメントに段落インデントを設定する方法を紹介します。
まず、Spire.Doc for .NET パッケージに含まれている DLL ファイルを .NET プロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。
PM> Install-Package Spire.Doc
以下の表は、Wordドキュメントで段落の異なるインデントを設定するために使用される、主要なクラスとプロパティの一部を示しています。
クラス/プロパティ | 説明 |
ParagraphFormat クラス | 段落の書式を表します。 |
ParagraphFormat.LeftIndent プロパティ | 段落の左インデントを表す値を返送または設定します。 |
ParagraphFormat.RightIndent プロパティ | 段落の右インデントを表す値を返送または設定します。 |
ParagraphFormat.FirstLineIndent プロパティ | 最初の行またはぶら下げインデントの値を取得または設定します。正の値は最初の行のインデントを表し、負の値はぶら下げインデントを表します。 |
詳しい手順は以下の通りです。
using Spire.Doc;
using Spire.Doc.Documents;
namespace WordIndent
{
class Program
{
static void Main(string[] args)
{
//Documentのインスタンスを作成する
Document doc = new Document();
//Wordドキュメントを読み込む
doc.LoadFromFile("C:/サンプル 1.docx");
//最初の段落を取得し、左インデントを設定する
Paragraph para1 = doc.Sections[0].Paragraphs[1];
para1.Format.LeftIndent = 30;
//2段落目を取得し、右インデントを設定する
Paragraph para2 = doc.Sections[0].Paragraphs[2];
para2.Format.RightIndent = 30;
//3段落目を取得し、最初の行のインデントを設定する
Paragraph para3 = doc.Sections[0].Paragraphs[3];
para3.Format.FirstLineIndent = 30;
//4段落目を取得し、ぶら下げインデントを設定する
Paragraph para4 = doc.Sections[0].Paragraphs[4];
para4.Format.FirstLineIndent = -30;
//ドキュメントを保存する
doc.SaveToFile("インデントの設定.docx", FileFormat.Docx2010);
}
}
}
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace WordIndent
Class Program
Shared Sub Main(ByVal args() As String)
'Documentのインスタンスを作成する
Dim doc As Document = New Document()
'Wordドキュメントを読み込む
doc.LoadFromFile("C:/サンプル 1.docx")
'最初の段落を取得し、左インデントを設定する
Dim para1 As Paragraph = doc.Sections(0).Paragraphs(1)
para1.Format.LeftIndent = 30
'2段落目を取得し、右インデントを設定する
Dim para2 As Paragraph = doc.Sections(0).Paragraphs(2)
para2.Format.RightIndent = 30
'3段落目を取得し、最初の行のインデントを設定する
Dim para3 As Paragraph = doc.Sections(0).Paragraphs(3)
para3.Format.FirstLineIndent = 30
'4段落目を取得し、ぶら下げインデントを設定する
Dim para4 As Paragraph = doc.Sections(0).Paragraphs(4)
para4.Format.FirstLineIndent = -30
'ドキュメントを保存する
doc.SaveToFile("インデントの設定.docx", FileFormat.Docx2010)
End Sub
End Class
End Namespace
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
Web から Word ドキュメントにコンテンツをコピーすると、段落間に空白が多くなり、長い文章に見えると同時に、文章の可読性が低下する可能性があります。この記事では、Spire.Doc for .NET を使用して C# および VB.NET でプログラムによって既存の Word ドキュメント内の空の行/空白の段落を削除する方法を示します。
まず、Spire.Doc for.NET パッケージに含まれている DLL ファイルを .NET プロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。
PM> Install-Package Spire.Doc
詳細な手順は次のとおりです。
using Spire.Doc;
using Spire.Doc.Documents;
using System;
namespace RemoveEmptyLines
{
class Program
{
static void Main(string[] args)
{
//Documentインスタンスを作成する
Document doc = new Document();
//Word ドキュメントをロードする
doc.LoadFromFile(@"input.docx");
//ドキュメント内のすべての段落をループする
foreach (Section section in doc.Sections)
{
for (int i = 0; i < section.Body.ChildObjects.Count; i++)
{
if (section.Body.ChildObjects[i].DocumentObjectType == DocumentObjectType.Paragraph)
{
//段落が空白の段落であるかどうかを決定する
if (String.IsNullOrEmpty((section.Body.ChildObjects[i] as Paragraph).Text.Trim()))
{
//空白の段落を削除する
section.Body.ChildObjects.Remove(section.Body.ChildObjects[i]);
i--;
}
}
}
}
//結果ドキュメントの保存する
doc.SaveToFile("RemoveEmptyLines.docx", FileFormat.Docx2013);
}
}
}
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace RemoveEmptyLines
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Documentインスタンスを作成する
Dim doc As Document = New Document()
'Word ドキュメントをロードする
doc.LoadFromFile("input.docx")
'ドキュメント内のすべての段落をループする
For Each section As Section In doc.Sections
For i As Integer = 0 To section.Body.ChildObjects.Count - 1
If section.Body.ChildObjects(i).DocumentObjectType Is DocumentObjectType.Paragraph Then
'段落が空白の段落であるかどうかを決定する
If String.IsNullOrEmpty(TryCast(section.Body.ChildObjects(i), Paragraph).Text.Trim()) Then
'空白の段落を削除する
section.Body.ChildObjects.Remove(section.Body.ChildObjects(i))
i -= 1
End If
End If
Next
Next
'結果ドキュメントの保存する
doc.SaveToFile("RemoveEmptyLines.docx", FileFormat.Docx2013)
End Sub
End Class
End Namespace
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
Word ドキュメントを処理する際には、ドキュメント内の段落を削除する必要があることがよくあります。たとえば、冗長性の高い段落が多数含まれるコンテンツを Web 上からドキュメントにコピーした後、有用なコンテンツだけを残すために余分な段落を削除する必要があります。Spire.Doc for .NET は、他のソフトウェアを使用することなく、コードを介してこれらの段落をすばやく削除するのに役立ちます。この記事では、Spire.Doc for .NET を使用して、C# および VB.NET で Word ドキュメントの段落を削除する方法を示します。
まず、Spire.Doc for.NET パッケージに含まれている DLL ファイルを .NET プロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。
PM> Install-Package Spire.Doc
Spire.Doc for .NET は、Word ドキュメントから指定した段落を削除するための ParagraphCollection クラスの RemoveAt() メソッドを提供しています。詳細な手順は次のとおりです。
using System;
using Spire.Doc;
namespace RemoveParagraphs
{
internal class Program
{
static void Main(string[] args)
{
//Documentクラスのオブジェクトを作成する
Document document = new Document();
//Wordドキュメントをロードする
document.LoadFromFile("Sample.docx");
//ドキュメントの最初のセクションを取得する
Section section = document.Sections[0];
//セクションの4段目を削除する
section.Paragraphs.RemoveAt(3);
//ドキュメントを保存する
document.SaveToFile("RemoveParagraphs.docx", FileFormat.Docx2013);
}
}
}
Imports System
Imports Spire.Doc
Module Program
Sub Main(args As String())
'Documentクラスのオブジェクトを作成する
Dim document As Document = New Document
'Wordドキュメントをロードする
document.LoadFromFile("Sample.docx")
'ドキュメントの最初のセクションを取得する
Dim section As Section = document.Sections(0)
'セクションの4段目を削除する
section.Paragraphs.RemoveAt(3)
'ドキュメントを保存する
document.SaveToFile("RemoveParagraphs.docx", FileFormat.Docx2013)
End Sub
End Module
Spire.Doc for .NET は、Word ドキュメント内のすべての段落を削除するための ParagraphCollection クラスの Clear() メソッドを提供しています。詳細な手順は次のとおりです。
using System;
using Spire.Doc;
namespace RemoveAllParagraphs
{
internal class Program
{
static void Main(string[] args)
{
//Documentクラスのオブジェクトを作成する
Document document = new Document();
//Wordドキュメントをロードする
document.LoadFromFile("Sample.docx");
//すべてのセクションをループする
foreach (Section section in document.Sections)
{
//各セクションのすべての段落を削除する
section.Paragraphs.Clear();
}
//ドキュメントを保存する
document.SaveToFile("RemoveAllParagraphs.docx", FileFormat.Docx2013);
}
}
}
Imports System
Imports Spire.Doc
Module Program
Sub Main(args As String())
'Documentクラスのオブジェクトを作成する
Dim document As Document = New Document()
'Wordドキュメントをロードする
document.LoadFromFile("Sample.docx")
'すべてのセクションをループする
For Each section As Section In document.Sections
'各セクションのすべての段落を削除する
section.Paragraphs.Clear()
Next
'ドキュメントを保存する
document.SaveToFile("RemoveAllParagraphs.docx", FileFormat.Docx2013)
End Sub
End Module
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
テキストの配置は、段落全体のテキストの外観を決定する段落フォーマット属性です。Microsoft Word で使用できるテキストの配置には、左揃え、中央揃え、右揃え、および両端揃えの4種類があります。この記事では、Spire.Doc for .NET を使用して、Word 文書の段落にさまざまなテキストの配置をプログラムで設定する方法をご紹介します。
まず、Spire.Doc for .NET パッケージに含まれている DLL ファイルを .NET プロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。
PM> Install-Package Spire.Doc
詳細な手順は次のとおりです。
using Spire.Doc;
using Spire.Doc.Documents;
namespace AlignText
{
class Program
{
static void Main(string[] args)
{
//ドキュメントインスタンスを作成する
Document doc = new Document();
//サンプルのWord文書を読み込む
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx");
//最初のセクションを取得する
Section section = doc.Sections[0];
//最初の段落を取得して、中央揃えにする
Paragraph p = section.Paragraphs[0];
p.Format.HorizontalAlignment = HorizontalAlignment.Center;
//2番目の段落を取得し、左揃えにする
Paragraph p1 = section.Paragraphs[1];
p1.Format.HorizontalAlignment = HorizontalAlignment.Left;
//3番目の段落を取得し、右揃えにする
Paragraph p2 = section.Paragraphs[2];
p2.Format.HorizontalAlignment = HorizontalAlignment.Right;
//4番目の段落を取得し、両端揃えにする
Paragraph p3 = section.Paragraphs[3];
p3.Format.HorizontalAlignment = HorizontalAlignment.Justify;
//ドキュメントを保存する
doc.SaveToFile("WordAlignment.docx", FileFormat.Docx);
}
}
}
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace AlignText
Class Program
Shared Sub Main(ByVal args() As String)
'ドキュメントインスタンスを作成する
Document doc = New Document()
'サンプルのWord文書を読み込む
doc.LoadFromFile("C:\Users\Administrator\Desktop\sample.docx")
'最初のセクションを取得する
Dim section As Section = doc.Sections(0)
'最初の段落を取得して、中央揃えにする
Dim p As Paragraph = section.Paragraphs(0)
p.Format.HorizontalAlignment = HorizontalAlignment.Center
'2番目の段落を取得し、左揃えにする
Dim p1 As Paragraph = section.Paragraphs(1)
p1.Format.HorizontalAlignment = HorizontalAlignment.Left
'3番目の段落を取得し、右揃えにする
Dim p2 As Paragraph = section.Paragraphs(2)
p2.Format.HorizontalAlignment = HorizontalAlignment.Right
'4番目の段落を取得し、両端揃えにする
Dim p3 As Paragraph = section.Paragraphs(3)
p3.Format.HorizontalAlignment = HorizontalAlignment.Justify
'ドキュメントを保存する
doc.SaveToFile("WordAlignment.docx", FileFormat.Docx)
End Sub
End Class
End Namespace
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。