フォントの操作

フォントとは、特定のサイズ、色、デザインを持つ文字のセットです。 Aspose.Words では、Fonts 名前空間と Font クラスを使用してフォントを操作できます。

フォントの書式設定

現在のフォントの書式設定は、Font プロパティによって返される Font オブジェクトによって表されます。 Font クラスには、Microsoft Word で使用可能なフォント プロパティを複製した、さまざまなフォント プロパティが含まれています。

次のコード例は、フォントの書式設定を設定する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Set font formatting properties
Font font = builder.Font;
font.Bold = true;
font.Color = System.Drawing.Color.DarkBlue;
font.Italic = true;
font.Name = "Arial";
font.Size = 24;
font.Spacing = 5;
font.Underline = Underline.Double;
// Output formatted text
builder.Writeln("I'm a very nice formatted string.");
dataDir = dataDir + "DocumentBuilderSetFontFormatting_out.doc";
doc.Save(dataDir);

フォントの塗りつぶしプロパティを使用して、テキストの塗りつぶし書式を設定することもできます。これにより、たとえば前景色やテキスト塗りつぶしの透明度を変更できるようになります。

フォントの行間隔の取得

フォントの行間隔は、テキストの連続する 2 行のベースライン間の垂直方向の距離です。したがって、行間隔には、行間の空白スペースと文字自体の高さが含まれます。

次の例に示すように、この値を取得するために LineSpacing プロパティが Font クラスに導入されました。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocument();
// Initialize document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Font.Name = "Calibri";
builder.Writeln("qText");
// Obtain line spacing.
Font font = builder.Document.FirstSection.Body.FirstParagraph.Runs[0].Font;
Console.WriteLine($"lineSpacing = {font.LineSpacing}");

フォント強調マーク

一部の東アジア言語では、強調を示すために特別な強調記号を使用します。 Font クラスは、フォーマット時に適用される EmphasisMark 列挙値を取得または設定するための EmphasisMark プロパティを提供します。

次のコード例は、EphasisMark プロパティを設定する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document document = new Document();
DocumentBuilder builder = new DocumentBuilder(document);
builder.Font.EmphasisMark = EmphasisMark.UnderSolidCircle;
builder.Write("Emphasis text");
builder.Writeln();
builder.Font.ClearFormatting();
builder.Write("Simple text");
document.Save(dataDir + "FontEmphasisMark_out.doc");