Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TableOfContentSample.cs 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /***************************************************************************************
  2. DocX – DocX is the community edition of Xceed Words for .NET
  3. Copyright (C) 2009-2017 Xceed Software Inc.
  4. This program is provided to you under the terms of the Microsoft Public
  5. License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
  6. For more features and fast professional support,
  7. pick up Xceed Words for .NET at https://xceed.com/xceed-words-for-net/
  8. *************************************************************************************/
  9. using System;
  10. using System.IO;
  11. namespace Xceed.Words.NET.Examples
  12. {
  13. public class TableOfContentSample
  14. {
  15. #region Private Members
  16. private const string TableOfContentSampleOutputDirectory = Program.SampleDirectory + @"TableOfContent\Output\";
  17. #endregion
  18. #region Constructors
  19. static TableOfContentSample()
  20. {
  21. if( !Directory.Exists( TableOfContentSample.TableOfContentSampleOutputDirectory ) )
  22. {
  23. Directory.CreateDirectory( TableOfContentSample.TableOfContentSampleOutputDirectory );
  24. }
  25. }
  26. #endregion
  27. #region Public Methods
  28. /// <summary>
  29. /// Add a Table of content to a document.
  30. /// </summary>
  31. public static void InsertTableOfContent()
  32. {
  33. Console.WriteLine( "\tInsertTableOfContent()" );
  34. // Creates a document
  35. using( DocX document = DocX.Create( TableOfContentSample.TableOfContentSampleOutputDirectory + @"InsertTableOfContent.docx" ) )
  36. {
  37. // Add a title
  38. document.InsertParagraph( "Insert Table of content" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;
  39. // Insert a table of content with a page break.
  40. document.InsertTableOfContents( "Teams", TableOfContentsSwitches.O | TableOfContentsSwitches.U | TableOfContentsSwitches.Z | TableOfContentsSwitches.H );
  41. document.InsertSectionPageBreak();
  42. // Create a paragraph and fill it in method AddTeams().
  43. var p = document.InsertParagraph();
  44. var rosters = TableOfContentSample.AddTeams( p );
  45. document.InsertParagraph( rosters );
  46. document.Save();
  47. Console.WriteLine( "\tCreated: InsertTableOfContent.docx\n" );
  48. }
  49. }
  50. /// <summary>
  51. /// Add a Table of content to a document by inserting it just before a reference paragraph.
  52. /// </summary>
  53. public static void InsertTableOfContentWithReference()
  54. {
  55. Console.WriteLine( "\tInsertTableOfContentWithReference()" );
  56. // Create a document.
  57. using( DocX document = DocX.Create( TableOfContentSample.TableOfContentSampleOutputDirectory + @"InsertTableOfContentWithReference.docx" ) )
  58. {
  59. // Add a title.
  60. document.InsertParagraph( "Insert Table of content with reference" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;
  61. // Add an intro paragraph.
  62. var intro = document.InsertParagraph( "This page will show the team rosters of the American League East Division." );
  63. intro.SpacingAfter( 150d );
  64. // Create a paragraph and fill it in method AddTeams().
  65. var p = document.InsertParagraph();
  66. var rosters = TableOfContentSample.AddTeams( p );
  67. document.InsertParagraph( rosters );
  68. // Insert a table of content with a page break just before the paragraph p.
  69. document.InsertTableOfContents( p,
  70. "Teams",
  71. TableOfContentsSwitches.O | TableOfContentsSwitches.U | TableOfContentsSwitches.Z | TableOfContentsSwitches.H,
  72. "Heading4" );
  73. document.Save();
  74. Console.WriteLine( "\tCreated: InsertTableOfContentWithReference.docx\n" );
  75. }
  76. }
  77. #endregion
  78. #region Private Methods
  79. private static Paragraph AddTeams( Paragraph paragraph )
  80. {
  81. // Add a title paragraph.
  82. var title = paragraph.InsertParagraphAfterSelf( "Team Rosters" ).Bold().FontSize( 20 ).SpacingAfter( 50d );
  83. title.Alignment = Alignment.center;
  84. // Add the content paragraphs and set a style for the Table of Content to recognize them.
  85. var p = title.InsertParagraphAfterSelf( "Boston Red Sox" ).Bold().FontSize( 15 ).SpacingAfter( 25d );
  86. p.StyleName = "Heading1";
  87. var p1 = p.InsertParagraphAfterSelf( "Tom Smith, P" )
  88. .AppendLine( "Mike Fitzgerald, C" )
  89. .AppendLine( "Tom Clancy, 1B" )
  90. .AppendLine( "Kevin Garnet, OF" ).SpacingAfter( 300d );
  91. var p2 = p1.InsertParagraphAfterSelf( "Tampa Rays" ).Bold().FontSize( 15 ).SpacingAfter( 25d );
  92. p2.StyleName = "Heading1";
  93. var p3 = p2.InsertParagraphAfterSelf( "Josh Hernandez, P" )
  94. .AppendLine( "Jacob Trouba, C" )
  95. .AppendLine( "Jesus Sanchez, 1B" )
  96. .AppendLine( "Jose Ria, OF" ).SpacingAfter( 300d );
  97. var p4 = p3.InsertParagraphAfterSelf( "New York Yankees" ).Bold().FontSize( 15 ).SpacingAfter( 25d );
  98. p4.StyleName = "Heading1";
  99. var p5 = p4.InsertParagraphAfterSelf( "Derek Jones, P" )
  100. .AppendLine( "Jose Riva, C" )
  101. .AppendLine( "Bryan Smith, 1B" )
  102. .AppendLine( "Carl Shattern, OF" ).SpacingAfter( 300d );
  103. var p6 = p5.InsertParagraphAfterSelf( "Baltimore Orioles" ).Bold().FontSize( 15 ).SpacingAfter( 25d );
  104. p6.StyleName = "Heading1";
  105. var p7 = p6.InsertParagraphAfterSelf( "Simon Delgar, P" )
  106. .AppendLine( "Johnny Helpan, C" )
  107. .AppendLine( "Miguel Danregados, 1B" )
  108. .AppendLine( "Joe West, OF" ).SpacingAfter( 300d );
  109. var p8 = p7.InsertParagraphAfterSelf( "Toronto Blue Jays" ).Bold().FontSize( 15 ).SpacingAfter( 25d );
  110. p8.StyleName = "Heading1";
  111. var p9 = p8.InsertParagraphAfterSelf( "Samir Endoya, P" )
  112. .AppendLine( "Steve Martin, C" )
  113. .AppendLine( "Erik Young, 1B" )
  114. .AppendLine( "Steve Martinek, OF" );
  115. return paragraph;
  116. }
  117. #endregion
  118. }
  119. }