Word for UWP
手順 2:テキストの追加
クイックスタート > 手順 2:テキストの追加

Visual Studio プロジェクトでコードビューを表示したまま、btnText_Click イベントに次のコードを追加します。

Dim word As New C1WordDocument()

' 横方向を使用して効果を高めます
word.Landscape = True

' テキストを測定および表示します
Dim text = "こんにちは!! これはサンプルテキストです。"
Dim font = New Font("Segoe UI Light", 20, RtfFontStyle.Italic)

' 段落を追加します
word.AddParagraph(text, font, Colors.BlueViolet, RtfHorizontalAlignment.Justify)

Dim picker As New FileSavePicker()
picker.FileTypeChoices.Add("Word Open XML Format (.docx)", New List(Of String)() From { _
        ".docx" _
})
picker.FileTypeChoices.Add("RTF Format (.rtf)", New List(Of String)() From { _
        ".rtf" _
})

picker.DefaultFileExtension = ".docx"
picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary
Dim file As StorageFile = Await picker.PickSaveFileAsync()
If file IsNot Nothing Then
        Await word.SaveAsync(file, If(file.Name.ToLower().EndsWith(".docx"), FileFormat.OpenXml, FileFormat.Rtf))

        Dim dlg As New MessageDialog("ファイルを保存しました")
        Await dlg.ShowAsync()
End If
C1WordDocument word = new C1WordDocument();

// 横方向を使用して効果を高めます
word.Landscape = true;

// テキストを測定および表示します 
var text = "こんにちは!! これはサンプルテキストです。";
var font = new Font("Segoe UI Light", 20, RtfFontStyle.Italic);

// 段落を追加します
word.AddParagraph(text, font, Colors.BlueViolet, RtfHorizontalAlignment.Justify);

FileSavePicker picker = new FileSavePicker();
picker.FileTypeChoices.Add("Word Open XML Format (.docx)", new List < string > () {
  ".docx"
});
picker.FileTypeChoices.Add("RTF Format (.rtf)", new List < string > () {
  ".rtf"
});

picker.DefaultFileExtension = ".docx";
picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
StorageFile file = await picker.PickSaveFileAsync();
if (file != null) {
  await word.SaveAsync(file, file.Name.ToLower().EndsWith(".docx") ? FileFormat.OpenXml : FileFormat.Rtf);

  MessageDialog dlg = new MessageDialog("ファイルを保存しました");
  await dlg.ShowAsync();
}

上記のコードでは、Word ドキュメントが横長モードで作成され、AddParagraph メソッドを使用してテキストが追加されます。作成したドキュメントは、選択した場所に保存できます。目的の場所を選択し、ドキュメントの名前を書き込むこともできます。ドキュメントは、その名前で保存されます。