Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

SectionSample.cs 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. using System.Linq;
  12. namespace Xceed.Words.NET.Examples
  13. {
  14. public class SectionSample
  15. {
  16. #region Private Members
  17. private const string SectionSampleOutputDirectory = Program.SampleDirectory + @"Section\Output\";
  18. #endregion
  19. #region Constructors
  20. static SectionSample()
  21. {
  22. if( !Directory.Exists( SectionSample.SectionSampleOutputDirectory ) )
  23. {
  24. Directory.CreateDirectory( SectionSample.SectionSampleOutputDirectory );
  25. }
  26. }
  27. #endregion
  28. #region Public Methods
  29. /// <summary>
  30. /// Create a document and insert Sections into it.
  31. /// </summary>
  32. public static void InsertSections()
  33. {
  34. Console.WriteLine( "\tInsertSections()" );
  35. // Create a document.
  36. using( DocX document = DocX.Create( SectionSample.SectionSampleOutputDirectory + @"InsertSections.docx" ) )
  37. {
  38. // Add a title
  39. document.InsertParagraph( "Inserting sections" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;
  40. // Add 2 paragraphs
  41. document.InsertParagraph( "This is the first paragraph." );
  42. document.InsertParagraph( "This is the second paragraph." );
  43. // Add a paragraph and a section break.
  44. document.InsertSection();
  45. // Add a new paragraph
  46. document.InsertParagraph( "This is the third paragraph, in a new section." );
  47. // Add a paragraph and a page break.
  48. document.InsertSectionPageBreak();
  49. document.InsertParagraph( "This is the fourth paragraph, in a new section." );
  50. var sections = document.GetSections();
  51. // Add a paragraph to display the result of sections.
  52. var p = document.InsertParagraph( "This document contains " ).Append( sections.Count.ToString() ).Append( " Sections.\n" );
  53. p.SpacingBefore( 40d );
  54. // Display the paragraphs count per section from this document.
  55. for( int i = 0; i < sections.Count; ++i )
  56. {
  57. var section = sections[ i ];
  58. var paragraphs = section.SectionParagraphs;
  59. var nonEmptyParagraphs = paragraphs.Where( x => !string.IsNullOrEmpty( x.Text ) );
  60. p.Append( "Section " ).Append( (i + 1).ToString() ).Append( " has " ).Append( nonEmptyParagraphs.Count().ToString() ).Append( " non-empty paragraphs.\n" );
  61. }
  62. document.Save();
  63. Console.WriteLine( "\tCreated: InsertSections.docx\n" );
  64. }
  65. }
  66. #endregion
  67. }
  68. }