チュートリアル
簡単にライブラリーを使用するためのチュートリアルコレクション
文字書式は、個々の単語や語句の外観を変更するために使用されます。書式設定されたテキストは、読者の注意を文書の特定のセクションに向けさせ、重要な情報を強調することができます。Word ドキュメントで使用できる文字書式の形式は、かなり多くあります。この記事では、Spire.Doc for .NET を使用して、Word ドキュメント内のテキストにさまざまな種類の書式を適用する方法について説明します。
まず、Spire.Doc for .NET パッケージに含まれている DLL ファイルを .NET プロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。
PM> Install-Package Spire.Doc
テキストに書式を適用するには、テキストを TextRange として取得し、CharacterFormat プロパティを通して TextRange 内の文字に書式を適用する必要があります。以下はその詳細な手順です。
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace ApplyFormattingToCharacters
{
class Program
{
static void Main(string[] args)
{
//Documentのオブジェクトを作成する
Document document = new Document();
//セクションを追加する
Section sec = document.AddSection();
//段落を追加し、スタイルを設定します
Paragraph paragraph = sec.AddParagraph();
ParagraphStyle defaultFont = new ParagraphStyle(document);
defaultFont.CharacterFormat.FontName = "Yu Gothic";
document.Styles.Add(defaultFont);
paragraph.ApplyStyle(defaultFont.Name);
paragraph.AppendText("\n\nここでは、さまざまな文字スタイルを持つ段落を紹介します。\nこれは");
//段落にテキストを追加し、TextRangeのオブジェクトを返す
TextRange tr = paragraph.AppendText("取り消し線付きテキスト");
//TextRangeのオブジェクトで取り消し線付きの文字書式を設定する
tr.CharacterFormat.IsStrikeout = true;
//テキスト背景に網かけを設定する
paragraph.AppendText("。\nこれは");
tr = paragraph.AppendText("網かけ付きテキストです");
tr.CharacterFormat.IsShadow = true;
//フォントサイズを設定する
paragraph.AppendText("。\nこれは");
tr = paragraph.AppendText("大きいフォントのテキストです");
tr.CharacterFormat.FontSize = 20;
//フォント名を設定する
paragraph.AppendText("。\nこれは");
tr = paragraph.AppendText("明朝体の文字です");
tr.CharacterFormat.FontName = "Yu Mincho";
//フォントの色を設定する
paragraph.AppendText("。\nこれは");
tr = paragraph.AppendText("赤色で表示されるテキストです");
tr.CharacterFormat.TextColor = Color.Red;
//テキストに太字や斜体の書式を適用する
paragraph.AppendText("。\nこれは");
tr = paragraph.AppendText("太字や斜体のテキストです");
tr.CharacterFormat.Bold = true;
tr.CharacterFormat.Italic = true;
//テキストに下線書式を適用する
paragraph.AppendText("。\nこれは");
tr = paragraph.AppendText("下線付きのテキストです");
tr.CharacterFormat.UnderlineStyle = UnderlineStyle.Single;
//テキストに背景色を適用する
paragraph.AppendText("。\nこれは");
tr = paragraph.AppendText("ハイライトされているテキストです");
tr.CharacterFormat.HighlightColor = Color.Green;
//テキストに囲み線を適用する
paragraph.AppendText("。\nこれは");
tr = paragraph.AppendText("囲み線付きのテキストです");
tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.Single;
tr.CharacterFormat.Border.Color = Color.Black;
//テキストに強調マークを付ける
paragraph.AppendText("。\nこれは");
tr = paragraph.AppendText("傍点付きのテキストです");
tr.CharacterFormat.EmphasisMark = Emphasis.DotBelow;
//テキストに上付き文字を適用する
paragraph.AppendText("。\nこれは数学の公式です: a");
tr = paragraph.AppendText("2");
tr.CharacterFormat.SubSuperScript = SubSuperScript.SuperScript;
paragraph.AppendText(" + b");
tr = paragraph.AppendText("2");
tr.CharacterFormat.SubSuperScript = SubSuperScript.SuperScript;
paragraph.AppendText(" = c");
tr = paragraph.AppendText("2");
tr.CharacterFormat.SubSuperScript = SubSuperScript.SuperScript;
paragraph.AppendText(".");
//ドキュメントを保存する
document.SaveToFile("文字書式の設定.docx", FileFormat.Docx);
}
}
}
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System.Drawing
Namespace ApplyFormattingToCharacters
Class Program
Shared Sub Main(ByVal args() As String)
'Documentのオブジェクトを作成する
Dim document As Document = New Document()
'セクションを追加する
Dim sec As Section = document.AddSection()
'段落を追加し、スタイルを設定します
Dim paragraph As Paragraph = sec.AddParagraph()
Dim defaultFont As ParagraphStyle = New ParagraphStyle(document)
defaultFont.CharacterFormat.FontName = "Yu Gothic"
document.Styles.Add(defaultFont)
paragraph.ApplyStyle(defaultFont.Name)
paragraph.AppendText("\n\nここでは、さまざまな文字スタイルを持つ段落を紹介します。\nこれは")
'段落にテキストを追加し、TextRangeのオブジェクトを返す
Dim tr As TextRange = paragraph.AppendText("取り消し線付きテキスト")
'TextRangeのオブジェクトで取り消し線付きの文字書式を設定する
tr.CharacterFormat.IsStrikeout = True
'テキスト背景に網かけを設定する
paragraph.AppendText("。\nこれは")
tr = paragraph.AppendText("網かけ付きテキストです")
tr.CharacterFormat.IsShadow = True
'フォントサイズを設定する
paragraph.AppendText("。\nこれは")
tr = paragraph.AppendText("大きいフォントのテキストです")
tr.CharacterFormat.FontSize = 20
'フォント名を設定する
paragraph.AppendText("。\nこれは")
tr = paragraph.AppendText("明朝体の文字です")
tr.CharacterFormat.FontName = "Yu Mincho"
'フォントの色を設定する
paragraph.AppendText("。\nこれは")
tr = paragraph.AppendText("赤色で表示されるテキストです")
tr.CharacterFormat.TextColor = Color.Red
'テキストに太字や斜体の書式を適用する
paragraph.AppendText("。\nこれは")
tr = paragraph.AppendText("太字や斜体のテキストです")
tr.CharacterFormat.Bold = True
tr.CharacterFormat.Italic = True
'テキストに下線書式を適用する
paragraph.AppendText("。\nこれは")
tr = paragraph.AppendText("下線付きのテキストです")
tr.CharacterFormat.UnderlineStyle = UnderlineStyle.Single
'テキストに背景色を適用する
paragraph.AppendText("。\nこれは")
tr = paragraph.AppendText("ハイライトされているテキストです")
tr.CharacterFormat.HighlightColor = Color.Green
'テキストに囲み線を適用する
paragraph.AppendText("。\nこれは")
tr = paragraph.AppendText("囲み線付きのテキストです")
tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.Single
tr.CharacterFormat.Border.Color = Color.Black
'テキストに強調マークを付ける
paragraph.AppendText("。\nこれは")
tr = paragraph.AppendText("傍点付きのテキストです")
tr.CharacterFormat.EmphasisMark = Emphasis.DotBelow
'テキストに上付き文字を適用する
paragraph.AppendText("。\nこれは数学の公式です: a")
tr = paragraph.AppendText("2")
tr.CharacterFormat.SubSuperScript = SubSuperScript.SuperScript
paragraph.AppendText(" + b")
tr = paragraph.AppendText("2")
tr.CharacterFormat.SubSuperScript = SubSuperScript.SuperScript
paragraph.AppendText(" = c")
tr = paragraph.AppendText("2")
tr.CharacterFormat.SubSuperScript = SubSuperScript.SuperScript
paragraph.AppendText(".")
'ドキュメントを保存する
document.SaveToFile("文字書式の設定.docx", FileFormat.Docx)
End Sub
End Class
End Namespace
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
Word ドキュメントから内容を抽出して、別の用途に使用したい場合があります。例えば、Word ドキュメントからテキストや画像を抽出し、それらを再配列して新しいドキュメントを作成することができます。これは、Spire.Doc for .NET を使用することによって簡単に達成することができます。このツールは、Word ドキュメント内のあらゆる種類の要素を取得し、それらをエクスポート、再配列、または変更することを可能にします。この記事では、Spire.Doc for .NET を使用して Word 文書からテキストと画像を抽出する方法について説明します。
まず、Spire.Doc for .NET パッケージに含まれている DLL ファイルを .NET プロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。
PM> Install-Package Spire.Doc
テキストを抽出するには、Paragraph.Text プロパティを使って段落内のすべてのテキストを取得し、StringBuilder.AppendLine() メソッドを使って StringBuilder クラスのインスタンスにそれを入れます。最後に、テキストを TXT ファイルに書き出すことができます。
詳しい手順は以下の通りです。
using Spire.Doc;
using Spire.Doc.Documents;
using System.Text;
using System.IO;
namespace ExtractTextfromWord
{
class ExtractText
{
static void Main(string[] args)
{
//Documentクラスのインスタンスを作成する
Document document = new Document();
//Word文書の読み込み
document.LoadFromFile("C:/洞窟の芸術.docx");
//StringBuilderクラスのインスタンスを作成する
StringBuilder sb = new StringBuilder();
//Word文書からテキストを抽出し、StringBuilderクラスのインスタンスに保存する
foreach (Section section in document.Sections)
{
foreach (Paragraph paragraph in section.Paragraphs)
{
sb.AppendLine(paragraph.Text);
}
}
//新たにtxtファイルを作成し、抽出したテキストを保存する
File.WriteAllText("テキストの抽出.txt", sb.ToString());
}
}
}
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System.Text
Imports System.IO
Namespace ExtractTextfromWord
Class ExtractText
Shared Sub Main(ByVal args() As String)
'Documentクラスのインスタンスを作成する
Dim document As Document = New Document()
'Word文書の読み込み
document.LoadFromFile("C:/洞窟の芸術.docx")
'StringBuilderクラスのインスタンスを作成する
Dim sb As StringBuilder = New StringBuilder()
'Word文書からテキストを抽出し、StringBuilderクラスのインスタンスに保存する
Dim section As Section
For Each section In document.Sections
Dim paragraph As Paragraph
For Each paragraph In section.Paragraphs
sb.AppendLine(paragraph.Text)
Next
Next
'新たにtxtファイルを作成し、抽出したテキストを保存する
File.WriteAllText("テキストの抽出.txt", sb.ToString())
End Sub
End Class
End Namespace
Word ドキュメントから画像を抽出するには、各段落にあるオブジェクトを取得し、それが画像であるかどうかを検出します。そして、それらの画像オブジェクトを PNG ファイルとして保存します。
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace ExtractImage
{
class Program
{
static void Main(string[] args)
{
//Documentクラスのオブジェクトを作成する
Document document = new Document();
//Word文書の読み込み
document.LoadFromFile("C:/洞窟の芸術.docx");
int index = 0;
//文書の各セクションを取得する
foreach (Section section in document.Sections)
{
//セクションの各段落を取得する
foreach (Paragraph paragraph in section.Paragraphs)
{
//指定された段落の各文書オブジェクトを取得する
foreach (DocumentObject docObject in paragraph.ChildObjects)
{
//DocumentObjectTypeが画像であれば、ファイルに保存する
if (docObject.DocumentObjectType == DocumentObjectType.Picture)
{
DocPicture picture = docObject as DocPicture;
picture.Image.Save(string.Format("画像/画像{0}.png", index), System.Drawing.Imaging.ImageFormat.Png);
index++;
}
}
}
}
}
}
}
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Namespace ExtractImage
Class Program
Shared Sub Main(ByVal args() As String)
'Documentクラスのオブジェクトを作成する
Dim document As Document = New Document()
'Word文書の読み込み
document.LoadFromFile("C:/洞窟の芸術.docx")
Dim index As Integer = 0
'文書の各セクションを取得する
Dim section As Section
For Each section In document.Sections
'セクションの各段落を取得する
Dim paragraph As Paragraph
For Each paragraph In section.Paragraphs
'指定された段落の各文書オブジェクトを取得する
Dim docObject As DocumentObject
For Each docObject In paragraph.ChildObjects
'DocumentObjectTypeが画像であれば、ファイルに保存する
If docObject.DocumentObjectType = DocumentObjectType.Picture Then
Dim picture As DocPicture = docObject
picture.Image.Save(String.Format("画像/画像{0}.png", index), System.Drawing.Imaging.ImageFormat.Png)
index = index + 1
End If
Next
Next
Next
End Sub
End Class
End Namespace
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
長い Word ドキュメントから特定のテキスト内容を探すのは難しいものです。MS Word には検索機能があり、ドキュメント内の指定した内容を素早く探し出すことができます。 もちろん、読者が見落としたときのために、検索したテキストに強調表示をすることも可能です。 Spire.Doc for .NET を使用して、Word ドキュメント内のテキストを検索し、強調表示する方法について説明します。
まず、Spire.Doc for .NET パッケージに含まれている DLL ファイルを .NET プロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。
PM> Install-Package Spire.Doc
詳細な手順は以下の通りです。
using System;
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
namespace FindAndHighlightText
{
internal class Program
{
static void Main(string[] args)
{
//Document クラスのオブジェクトを作成する
Document document = new Document();
//Wordドキュメントを読み込む
document.LoadFromFile(@"C:\実例.docx");
//一致するテキストを検索する
TextSelection[] text = document.FindAllString("荘子", false, true);
//検出されたテキストをループし、強調表示する
foreach (TextSelection seletion in text)
{
seletion.GetAsOneRange().CharacterFormat.HighlightColor = Color.Yellow;
}
//ドキュメントを保存する
document.SaveToFile("テキストの検索と強調表示.docx", FileFormat.Docx);
}
}
}
Imports System
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace FindAndHighlightText
Friend Class Program
Shared Sub Main(ByVal args() As String)
'Document クラスのオブジェクトを作成する
Dim document As Document = New Document()
'Wordドキュメントを読み込む
document.LoadFromFile("C:\実例.docx")
'一致するテキストを検索する
Dim text() As TextSelection = document.FindAllString("荘子", False, True)
'検出されたテキストをループし、強調表示する
Dim seletion As TextSelection
For Each seletion In text
seletion.GetAsOneRange().CharacterFormat.HighlightColor = Color.Yellow
Next
'ドキュメントを保存する
document.SaveToFile("テキストの検索と強調表示.docx", FileFormat.Docx)
End Sub
End Class
End Namespace
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
強調マークは、Word文書で単語を強調し、目立たせるために使用されます。これは通常、強調された単語の上または下に、ドット或いは円を配置されたことです。ただし、単語を手動で選択して強調マークを付けるには、多くの作業をしなければならないので面倒です。幸い、Spire.Doc for .NET は、コードごとに強調マークを適用するためのはるかに簡単な方法を提供します。この記事では、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 applyemphasismark
{
class Program
{
static void Main(string[] args)
{
//ドキュメントインスタンスを作成する
Document document = new Document();
//ディスクからWord文書をロードする
document.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx");
//強調したいテキストを探す
TextSelection[] textSelections = document.FindAllString("Spire.Doc for .NET", false, true);
//見つかったテキストに強調マークを付ける
foreach (TextSelection selection in textSelections)
{
selection.GetAsOneRange().CharacterFormat.EmphasisMark = Emphasis.DotBelow;
}
//ドキュメントを別のWordファイルに保存する
string output = "ApllyEmphasisMark.docx";
document.SaveToFile(output, FileFormat.Docx);
}
}
}
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace applyemphasismark
Class Program
Shared Sub Main(ByVal args() As String)
'ドキュメントインスタンスを作成する
Dim document As Document = New Document()
'ディスクからWord文書をロードする
document.LoadFromFile("C:\Users\Administrator\Desktop\sample.docx")
'強調したいテキストを探す
Dim textSelections() As TextSelection = document.FindAllString("Spire.Doc for .NET",False,True)
'見つかったテキストに強調マークを付ける
Dim selection As TextSelection
For Each selection In textSelections
selection.GetAsOneRange().CharacterFormat.EmphasisMark = Emphasis.DotBelow
Next
'ドキュメントを別のWordファイルに保存する
Dim output As String = "ApllyEmphasisMark.docx"
document.SaveToFile(output, FileFormat.Docx)
End Sub
End Class
End Namespace
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。