Word for UWP
引用文の追加
Word for UWP の操作 > 基礎レベルの操作 > 引用文の追加

Word for UWP を使用して、別のファイルからの引用文をドキュメントに追加できます。

次のコードでは、WordUtils という名前のクラスを使用します。このクラスは、システムの次の場所にある製品サンプル内に置かれています。
Documents\ComponentOne Samples\UWP\WordSample
これらのクラスを上記の場所からアプリケーションで使用できます。

次のコードを使用して、テキストファイルからの引用文をドキュメントに追加します。

' ページ四角形を計算します(マージンを差し引いて)
Dim rcPage As Rect = WordUtils.PageRectangle(word)
Dim rc As Rect = rcPage

' 出力パラメータを初期化します
Dim hdrFont As New Font("Arial", 14, RtfFontStyle.Bold)
Dim titleFont As New Font("Arial", 24, RtfFontStyle.Bold)
Dim txtFont As New Font("Times New Roman", 10, RtfFontStyle.Italic)

' タイトルを追加します
rc = WordUtils.RenderParagraph(word, word.Info.Title, titleFont, rcPage, rc)
' ドキュメントを構築します
For Each s As String In GetQuotes()
        Dim authorQuote As String() = s.Split(ControlChars.Tab)

        ' ヘッダーをレンダリングします(作成者)
        Dim author = authorQuote(0)
        rc.Y += 20
        rc = WordUtils.RenderParagraph(word, author, hdrFont, rcPage, rc, True)

        ' 本文をレンダリングします(引用文)
        Dim text As String = authorQuote(1)
        rc.X = rcPage.X + 36
        ' << 本文を 1/2 インチインデントします
        rc.Width = rcPage.Width - 40
        rc = WordUtils.RenderParagraph(word, text, txtFont, rcPage, rc)
        rc.X = rcPage.X
        ' << インデントを元に戻します
        rc.Width = rcPage.Width ' << 各引用文の後に 12pt のスペースを追加します
        rc.Y += 12
Next
Private Shared Function GetQuotes() As List
        Dim list = New List()

Using sr = New StreamReader(GetType(BasicTextPage).GetTypeInfo()
        .Assembly.GetManifestResourceStream("WordSamples.Resources.quotes.txt"))
                Dim quotes = sr.ReadToEnd()
                For Each quote As String In quotes.Split("*"C)
                        Dim pos As Integer = quote.IndexOf(vbCr & vbLf)
                        If pos > -1 Then
                                Dim q = String.Format("{0}" & vbTab & "{1}", quote.Substring(0, pos), quote.Substring(pos + 2).Trim())
                                list.Add(q)
                        End If
                Next
        End Using

        Return list
End Function
// ページ四角形を計算します(マージンを差し引いて)
Rect rcPage = WordUtils.PageRectangle(word);
Rect rc = rcPage;

// 出力パラメータを初期化します
Font hdrFont = new Font("Arial", 14, RtfFontStyle.Bold);
Font titleFont = new Font("Arial", 24, RtfFontStyle.Bold);
Font txtFont = new Font("Times New Roman", 10, RtfFontStyle.Italic);

// タイトルを追加します
rc = WordUtils.RenderParagraph(word, word.Info.Title, titleFont, rcPage, rc);

// ドキュメントを構築します
foreach(string s in GetQuotes()) {
  string[] authorQuote = s.Split('\t');

  // ヘッダーをレンダリングします(作成者)
  var author = authorQuote[0];
  rc.Y += 20;
  rc = WordUtils.RenderParagraph(word, author, hdrFont, rcPage, rc, true);

  // 本文をレンダリングします(引用文)
  string text = authorQuote[1];
  rc.X = rcPage.X + 36; // << 本文を 1/2 インチインデントします
  rc.Width = rcPage.Width - 40;
  rc = WordUtils.RenderParagraph(word, text, txtFont, rcPage, rc);
  rc.X = rcPage.X; // << インデントを元に戻します
  rc.Width = rcPage.Width;
  rc.Y += 12; // << 各引用文の後に 12pt のスペースを追加します
}

static List GetQuotes() {
  var list = new List();

using(var sr = new StreamReader(typeof(BasicTextPage).GetTypeInfo()
        .Assembly.GetManifestResourceStream("WordSamples.Resources.quotes.txt"))) {
    var quotes = sr.ReadToEnd();
    foreach(string quote in quotes.Split('*')) {
      int pos = quote.IndexOf("\r\n");
      if (pos > -1) {
        var q = string.Format("{0}\t{1}", quote.Substring(0, pos), quote.Substring(pos + 2).Trim());
        list.Add(q);
      }
    }
  }

  return list;
}

上記のコードは、テキストファイルから引用文を読み込んでドキュメントに書き出します。まずタイトルをドキュメントに追加し、次にヘッダーと本文をレンダリングしてから、ドキュメントにテキストを書き込みます。

上記のコードの出力は、次の図のようになります。