Word for UWP の操作 > 上級レベルの操作 > TOC の追加 |
Word for UWP では、テキストと見出しを含む Word ドキュメントに目次(TOC)を追加できます。これらの見出しを使用して TOC を作成します。
次のコードでは、WordUtils という名前のクラスを使用します。このクラスは、システムの次の場所にある製品サンプル内に置かれています。
Documents\ComponentOne Samples\UWP\WordSample
これらのクラスを上記の場所からアプリケーションで使用できます。
次のコードは、テキストと見出しを含む Word ドキュメントの TOC を作成します。
Dim _doc As New C1WordDocument() ' タイトルを追加します Dim titleFont As New Font("Tahoma", 24, RtfFontStyle.Bold) Dim rcPage As Rect = WordUtils.PageRectangle(doc) Dim rc As Rect = WordUtils.RenderParagraph(doc, doc.Info.Title, titleFont, rcPage, rcPage, False) rc.Y += 12 ' 意味がないドキュメントを作成します Dim bkmk = New List() Dim headerFont As New Font("Arial", 14, RtfFontStyle.Bold) Dim bodyFont As New Font("Times New Roman", 11) For i As Integer = 0 To 29 ' i 番目の見出しを作成します(リンクターゲットおよびアウトラインエントリとして) Dim header As String = String.Format("{0}. {1}", i + 1, BuildRandomTitle()) rc = WordUtils.RenderParagraph(doc, header, headerFont, rcPage, rc, True, _ True) ' 後で TOC を構築するためにブックマークを保存します Dim pageNumber As Integer = doc.CurrentPage() + 1 bkmk.Add(New String() {pageNumber.ToString(), header}) ' テキストを作成します rc.X += 36 rc.Width -= 36 For j As Integer = 0 To 3 + (_rnd.[Next](20) - 1) Dim text As String = BuildRandomParagraph() rc = WordUtils.RenderParagraph(doc, text, bodyFont, rcPage, rc) rc.Y += 6 Next rc.X -= 36 rc.Width += 36 rc.Y += 20 Next ' TOC を開始します doc.PageBreak() ' 新しいページで TOC を開始します Dim tocPage As Integer = doc.CurrentPage() ' ページ索引を保存します(後で TOC から移動するため) rc = WordUtils.RenderParagraph(doc, Strings.TableOfContentsDocumentTitle, titleFont, rcPage, rcPage, True) rc.Y += 12 rc.X += 30 rc.Width -= 40 ' TOC をレンダリングします Dim dottedPen As New Pen(Colors.Gray, 1.5F) dottedPen.DashStyle = DashStyle.Dot Dim sfRight As New StringFormat() sfRight.Alignment = HorizontalAlignment.Right rc.Height = bodyFont.Size * 1.2 For Each entry As String() In bkmk ' ブックマーク情報を取得します Dim page As String = entry(0) Dim header As String = entry(1) ' 見出し名とページ番号をレンダリングします doc.DrawString(header, bodyFont, Colors.Black, rc) doc.DrawString(page, bodyFont, Colors.Black, rc, sfRight) ' エントリにローカルハイパーリンクを追加します doc.AddLink(Strings.PoundSign + header) ' 次のエントリに移動します rc = WordUtils.Offset(rc, 0, rc.Height) If rc.Bottom > rcPage.Bottom Then doc.PageBreak() rc.Y = rcPage.Y End If Next
C1WordDocument _doc = new C1WordDocument(); // タイトルを追加します Font titleFont = new Font("Tahoma", 24, RtfFontStyle.Bold); Rect rcPage = WordUtils.PageRectangle(doc); Rect rc = WordUtils.RenderParagraph(doc, doc.Info.Title, titleFont, rcPage, rcPage, false); rc.Y += 12; // 意味がないドキュメントを作成します var bkmk = new List(); Font headerFont = new Font("Arial", 14, RtfFontStyle.Bold); Font bodyFont = new Font("Times New Roman", 11); for (int i = 0; i < 30; i++) { // i 番目の見出しを作成します(リンクターゲットおよびアウトラインエントリとして) string header = string.Format("{0}. {1}", i + 1, BuildRandomTitle()); rc = WordUtils.RenderParagraph(doc, header, headerFont, rcPage, rc, true, true); // 後で TOC を構築するためにブックマークを保存します int pageNumber = doc.CurrentPage() + 1; bkmk.Add(new string[] { pageNumber.ToString(), header }); // テキストを作成します rc.X += 36; rc.Width -= 36; for (int j = 0; j < 3 + _rnd.Next(20); j++) { string text = BuildRandomParagraph(); rc = WordUtils.RenderParagraph(doc, text, bodyFont, rcPage, rc); rc.Y += 6; } rc.X -= 36; rc.Width += 36; rc.Y += 20; } // TOC を開始します doc.PageBreak(); // 新しいページで TOC を開始します int tocPage = doc.CurrentPage(); // ページ索引を保存します(後で TOC から移動するため) rc = WordUtils.RenderParagraph(doc, Strings.TableOfContentsDocumentTitle, titleFont, rcPage, rcPage, true); rc.Y += 12; rc.X += 30; rc.Width -= 40; // TOC をレンダリングします Pen dottedPen = new Pen(Colors.Gray, 1.5 f); dottedPen.DashStyle = DashStyle.Dot; StringFormat sfRight = new StringFormat(); sfRight.Alignment = HorizontalAlignment.Right; rc.Height = bodyFont.Size * 1.2; foreach(string[] entry in bkmk) { // ブックマーク情報を取得します string page = entry[0]; string header = entry[1]; // 見出し名とページ番号をレンダリングします doc.DrawString(header, bodyFont, Colors.Black, rc); doc.DrawString(page, bodyFont, Colors.Black, rc, sfRight); // エントリにローカルハイパーリンクを追加します doc.AddLink(Strings.PoundSign + header); // 次のエントリに移動します rc = WordUtils.Offset(rc, 0, rc.Height); if (rc.Bottom > rcPage.Bottom) { doc.PageBreak(); rc.Y = rcPage.Y; } }
上記のコードは、ドキュメント内のテキストの見出しにブックマークを追加します。次に、これらのブックマークを使用して TOC を生成します。
上記のコードの出力は、次の図のようになります。