バージョン

書式設定を Word 文書に適用

このトピックは、Infragistics® Word ライブラリを使用して Word 文書を書式設定する方法を示します。以下のコードは、forward-only の WordDocumentWriter オブジェクトを使用します。このトピックで検討されるさまざまな書式設定機能は、サイズ、方向などのフォント、段落書式およびページ属性です。

Note
注:

InfragisticsWPF4.Documents.IO.v24.1.dll アセンブリへの参照が必要とされます。

フォントの指定

Font クラスは、文字のビジュアル属性または文字の範囲をカスタマイズする方法を提供します。

C# の場合:

using Infragistics.Documents.Word;
// Create a font, which can be reused in content creation
Infragistics.Documents.Word.Font font = docWriter.CreateFont();
font.Name = "Consolas";
font.Bold = true;
font.Size = .23f;
font.Underline = Underline.Double;
font.UnderlineColor = Colors.Blue;
font.Effects.Capitalization = Capitalization.CapsOn;

Visual Basic の場合:

Imports Infragistics.Documents.Word
' Create a font, which we can reuse in content creation
Dim font As Infragistics.Documents.Word.Font = docWriter.CreateFont()
font.Name = "Consolas"
font.Bold = True
font.Size = 0.23F
font.Underline = Underline.[Double]
font.UnderlineColor = Colors.Blue
font.Effects.Capitalization = Capitalization.CapsOn

段落プロパティ

ParagraphProperties クラスは段落の書式設定を制御する方法を提供します。

C# の場合:

using Infragistics.Documents.Word;
// Paragraph Properties
// Create a new instance of the WordDocumentWriter class using the
// static 'Create' method.
WordDocumentWriter docWriter = WordDocumentWriter.Create(@"C:\TestWordDoc.docx");
ParagraphProperties paraformat = docWriter.CreateParagraphProperties();
// Set right alignment for this paragraph
paraformat.Alignment = ParagraphAlignment.Right;
// Set 0.5 inch left indent
paraformat.LeftIndent = 0.5f;

Visual Basic の場合:

Imports Infragistics.Documents.Word
' Paragraph Properties
' Create a new instance of the WordDocumentWriter class using the
' static 'Create' method.
Dim docWriter As WordDocumentWriter = WordDocumentWriter.Create("C:\TestWordDoc.docx")
Dim paraformat As ParagraphProperties = docWriter.CreateParagraphProperties()
' Set right alignment for this paragraph
paraformat.Alignment = ParagraphAlignment.Right
' Set 0.5 inch left indent
paraformat.LeftIndent = 0.5F

セクション プロパティ

SectionProperties クラスは、サイズ、余白、方向などのページ属性を制御する方法を提供します。SectionProperties インスタンスを渡す DefineSection メソッドは、EndParagraph メソッドの後で呼び出さなければなりません。

C# の場合:

using Infragistics.Documents.Word;
// Create a new instance of the WordDocumentWriter class using the
// static 'Create' method.
WordDocumentWriter docWriter = WordDocumentWriter.Create(@"C:\TestWordDoc.docx");
// Use inches as the unit of measure
docWriter.Unit = UnitOfMeasurement.Inch;
SectionProperties secProperties = docWriter.CreateSectionProperties();
secProperties.PageSize = new Size(7, 5);
secProperties.PageOrientation = PageOrientation.Landscape;
// Applies the section properties(PageSize and Orientation) for the above added paragraphs
docWriter.DefineSection(secProperties);

Visual Basic の場合:

Imports Infragistics.Documents.Word
' Create a new instance of the WordDocumentWriter class using the
' static 'Create' method.
WordDocumentWriter docWriter = WordDocumentWriter.Create("C:\TestWordDoc.docx");
' Use inches as the unit of measure
docWriter.Unit = UnitOfMeasurement.Inch
Dim secProperties As SectionProperties = docWriter.CreateSectionProperties()
secProperties.PageSize = New Size(7, 5)
secProperties.PageOrientation = PageOrientation.Landscape
' Applies the section properties(PageSize and Orientation) for the above added paragraphs
docWriter.DefineSection(secProperties)

以下は上記の書式設定すべてを含む完全なコードです。

C# の場合:

using Infragistics.Documents.Word;
// Create a new instance of the WordDocumentWriter class using the
// static 'Create' method.
// This instance must be closed once content is written into Word.
WordDocumentWriter docWriter = WordDocumentWriter.Create(@"C:\TestWordDoc.docx");
// Use inches as the unit of measure
docWriter.Unit = UnitOfMeasurement.Inch;
// Create a font, which we can use in content creation
Infragistics.Documents.Word.Font font = docWriter.CreateFont();
// Start the document...note that each call to StartDocument must
// be balanced with a corresponding call to EndDocument.
docWriter.StartDocument();
// Start a paragraph
docWriter.StartParagraph();
// Add a text run for the title, bolded and a little bigger
font.Name = "Consolas";
font.Bold = true;
font.Size = .23f;
font.Underline = Underline.Double;
font.UnderlineColor = Colors.Blue;
font.Effects.Capitalization = Capitalization.CapsOn;
docWriter.AddTextRun("Paragraphs and Topic Sentences", font);
// End the paragraph
docWriter.EndParagraph();
// Paragraph Properties
ParagraphProperties paraformat = docWriter.CreateParagraphProperties();
paraformat.Alignment = ParagraphAlignment.Right;
// Start another Paragraph
// and apply the ParagraphProperties Object
docWriter.StartParagraph(paraformat);
docWriter.AddNewLine();
// Reset font, and apply different font settings for this paragraph.
font.Reset();
font.Italic = true;
font.ForeColor = Colors.Blue;
font.Effects.TextEffect = FontTextEffect.EngravingOn;
docWriter.AddTextRun("A paragraph is a series of sentences that are organized and coherent, and are all related to a single topic. Almost every piece of writing you do that is longer than a few sentences should be organized into paragraphs. This is because paragraphs show a reader where the subdivisions of an essay begin and end, and thus help the reader see the organization of the essay and grasp its main points.", font);
// End the paragraph
docWriter.EndParagraph();
// Add an Empty paragraph
docWriter.AddEmptyParagraph();
// Start the last paragraph with left indent of 0.5 inch
paraformat.Reset();
paraformat.LeftIndent = 0.5f;
docWriter.StartParagraph(paraformat);
font.Reset();
font.ForeColor = Colors.Red;
docWriter.AddTextRun("This page is defined by the SectionProperties object. The size of the page is set to 7x5 inches and the Orientation is set to Landscape.", font);
docWriter.EndParagraph();
// Set page attributes
SectionProperties secProperties = docWriter.CreateSectionProperties();
secProperties.PageSize = new Size(7, 5);
secProperties.PageOrientation = PageOrientation.Landscape;
// Applies the section properties(PageSize and Orientation) for the above added paragraphs
docWriter.DefineSection(secProperties);
// End the Document
docWriter.EndDocument();
// Close the writer
docWriter.Close();

Visual Basic の場合:

Imports Infragistics.Documents.Word
' Create a new instance of the WordDocumentWriter class using the
' static 'Create' method.
' This instance must be closed once content is written into Word.
Dim docWriter As WordDocumentWriter = WordDocumentWriter.Create("C:\TestWordDoc.docx")
' Use inches as the unit of measure
docWriter.Unit = UnitOfMeasurement.Inch
' Create a font, which we can reuse in content creation
Dim font As Infragistics.Documents.Word.Font = docWriter.CreateFont()
' Start the document...note that each call to StartDocument must
' be balanced with a corresponding call to EndDocument.
docWriter.StartDocument()
' Start a paragraph
docWriter.StartParagraph()
' Add a text run for the title, bolded and a little bigger
font.Name = "Consolas"
font.Bold = True
font.Size = 0.23F
font.Underline = Underline.[Double]
font.UnderlineColor = Colors.Blue
font.Effects.Capitalization = Capitalization.CapsOn
docWriter.AddTextRun("Paragraphs and Topic Sentences", font)
' End the paragraph
docWriter.EndParagraph()
' Paragraph Properties
Dim paraformat As ParagraphProperties = docWriter.CreateParagraphProperties()
paraformat.Alignment = ParagraphAlignment.Right
' Start another Paragraph
' and apply the ParagraphProperties Object
docWriter.StartParagraph(paraformat)
docWriter.AddNewLine()
' Reset font, and apply different font settings for this paragraph.
font.Reset()
font.Italic = True
font.ForeColor = Colors.Blue
font.Effects.TextEffect = FontTextEffect.EngravingOn
docWriter.AddTextRun("A paragraph is a series of sentences that are organized and coherent, and are all related to a single topic. Almost every piece of writing you do that is longer than a few sentences should be organized into paragraphs. This is because paragraphs show a reader where the subdivisions of an essay begin and end, and thus help the reader see the organization of the essay and grasp its main points.", font)
' End the paragraph
docWriter.EndParagraph()
' Set page attributes
Dim secProperties As SectionProperties = docWriter.CreateSectionProperties()
secProperties.PageSize = New Size(7, 5)
secProperties.PageOrientation = PageOrientation.Landscape
' Applies the section properties(Page Size and Orientation) for the above added paragraphs
docWriter.DefineSection(secProperties)
' Add an Empty paragraph
docWriter.AddEmptyParagraph()
' Start the last paragraph with left indent of 0.5 inch
paraformat.Reset()
paraformat.LeftIndent = 0.5F
docWriter.StartParagraph(paraformat)
font.Reset()
font.ForeColor = Colors.Red
docWriter.AddTextRun("The above page is defined by the Section Properties object. The size of the page is set to 7x5 inches and the Orientation is set to Landscape.", font)
docWriter.EndParagraph()
' End the Document
docWriter.EndDocument()
' Close the writer
docWriter.Close()

以下のコードスニペットでは、MS Word が上記すべてのコードの結果を描画する方法を紹介します:

Word Apply Formatting 01.png