Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ParagraphSample.cs 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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.Collections.Generic;
  11. using System.Drawing;
  12. using System.IO;
  13. using System.Text.RegularExpressions;
  14. namespace Xceed.Words.NET.Examples
  15. {
  16. public class ParagraphSample
  17. {
  18. #region Private Members
  19. private static Dictionary<string, string> _replacePatterns = new Dictionary<string, string>()
  20. {
  21. { "COST", "$13.95" },
  22. };
  23. private const string ParagraphSampleOutputDirectory = Program.SampleDirectory + @"Paragraph\Output\";
  24. #endregion
  25. #region Constructors
  26. static ParagraphSample()
  27. {
  28. if( !Directory.Exists( ParagraphSample.ParagraphSampleOutputDirectory ) )
  29. {
  30. Directory.CreateDirectory( ParagraphSample.ParagraphSampleOutputDirectory );
  31. }
  32. }
  33. #endregion
  34. #region Public Methods
  35. /// <summary>
  36. /// Create a document with formatted paragraphs.
  37. /// </summary>
  38. public static void SimpleFormattedParagraphs()
  39. {
  40. Console.WriteLine( "\tSimpleFormattedParagraphs()" );
  41. // Create a new document.
  42. using( DocX document = DocX.Create( ParagraphSample.ParagraphSampleOutputDirectory + @"SimpleFormattedParagraphs.docx" ) )
  43. {
  44. // Add a title
  45. document.InsertParagraph( "Formatted paragraphs" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;
  46. // Insert a Paragraph into this document.
  47. var p = document.InsertParagraph();
  48. // Append some text and add formatting.
  49. p.Append( "This is a simple formatted red bold paragraph" )
  50. .Font( new Font( "Arial" ) )
  51. .FontSize( 25 )
  52. .Color( Color.Red )
  53. .Bold()
  54. .Append( " containing a blue italic text." ).Font( new Font( "Times New Roman" ) ).Color( Color.Blue ).Italic()
  55. .SpacingAfter( 40 );
  56. // Insert another Paragraph into this document.
  57. var p2 = document.InsertParagraph();
  58. // Append some text and add formatting.
  59. p2.Append( "This is a formatted paragraph using spacing," )
  60. .Font( new Font( "Courier New" ) )
  61. .FontSize( 10 )
  62. .Italic()
  63. .Spacing( 5 )
  64. .Append( "highlight" ).Highlight( Highlight.yellow ).UnderlineColor( Color.Blue ).CapsStyle( CapsStyle.caps )
  65. .Append( " and strike through." ).StrikeThrough( StrikeThrough.strike );
  66. // Save this document to disk.
  67. document.Save();
  68. Console.WriteLine( "\tCreated: SimpleFormattedParagraphs.docx\n" );
  69. }
  70. }
  71. /// <summary>
  72. /// Create a document and add a paragraph with all its lines on a single page.
  73. /// </summary>
  74. public static void ForceParagraphOnSinglePage()
  75. {
  76. Console.WriteLine( "\tForceParagraphOnSinglePage()" );
  77. // Create a new document.
  78. using( DocX document = DocX.Create( ParagraphSample.ParagraphSampleOutputDirectory + @"ForceParagraphOnSinglePage.docx" ) )
  79. {
  80. // Add a title
  81. document.InsertParagraph( "Prevent paragraph split" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;
  82. // Create a Paragraph that will appear on 1st page.
  83. var p = document.InsertParagraph( "This is a paragraph on first page.\nLine2\nLine3\nLine4\nLine5\nLine6\nLine7\nLine8\nLine9\nLine10\nLine11\nLine12\nLine13\nLine14\nLine15\nLine16\nLine17\nLine18\nLine19\nLine20\nLine21\nLine22\nLine23\nLine24\nLine25\n" );
  84. p.FontSize(15).SpacingAfter( 30 );
  85. // Create a Paragraph where all its lines will appear on a same page.
  86. var p2 = document.InsertParagraph( "This is a paragraph where all its lines are on the same page. The paragraph does not split on 2 pages.\nLine2\nLine3\nLine4\nLine5\nLine6\nLine7\nLine8\nLine9\nLine10" );
  87. p2.SpacingAfter( 30 );
  88. // Indicate that all the paragraph's lines will be on the same page
  89. p2.KeepLinesTogether();
  90. // Create a Paragraph that will appear on 2nd page.
  91. var p3 = document.InsertParagraph( "This is a paragraph on second page.\nLine2\nLine3\nLine4\nLine5\nLine6\nLine7\nLine8\nLine9\nLine10" );
  92. // Save this document to disk.
  93. document.Save();
  94. Console.WriteLine( "\tCreated: ForceParagraphOnSinglePage.docx\n" );
  95. }
  96. }
  97. /// <summary>
  98. /// Create a document and add a paragraph with all its lines on the same page as the next paragraph.
  99. /// </summary>
  100. public static void ForceMultiParagraphsOnSinglePage()
  101. {
  102. Console.WriteLine( "\tForceMultiParagraphsOnSinglePage()" );
  103. // Create a new document.
  104. using( DocX document = DocX.Create( ParagraphSample.ParagraphSampleOutputDirectory + @"ForceMultiParagraphsOnSinglePage.docx" ) )
  105. {
  106. // Add a title.
  107. document.InsertParagraph( "Keeps Paragraphs on same page" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;
  108. // Create a Paragraph that will appear on 1st page.
  109. var p = document.InsertParagraph( "This is a paragraph on first page.\nLine2\nLine3\nLine4\nLine5\nLine6\nLine7\nLine8\nLine9\nLine10\nLine11\nLine12\nLine13\nLine14\nLine15\nLine16\nLine17\nLine18\nLine19\nLine20\nLine21\nLine22\n" );
  110. p.FontSize( 15 ).SpacingAfter( 30 );
  111. // Create a Paragraph where all its lines will appear on a same page as the next paragraph.
  112. var p2 = document.InsertParagraph( "This is a paragraph where all its lines are on the same page as the next paragraph.\nLine2\nLine3\nLine4\nLine5\nLine6\nLine7\nLine8\nLine9\nLine10" );
  113. p2.SpacingAfter( 30 );
  114. // Indicate that this paragraph will be on the same page as the next paragraph.
  115. p2.KeepWithNextParagraph();
  116. // Create a Paragraph that will appear on 2nd page.
  117. var p3 = document.InsertParagraph( "This is a paragraph on second page.\nLine2\nLine3\nLine4\nLine5\nLine6\nLine7\nLine8\nLine9\nLine10" );
  118. // Indicate that all this paragraph's lines will be on the same page.
  119. p3.KeepLinesTogether();
  120. // Save this document to disk.
  121. document.Save();
  122. Console.WriteLine( "\tCreated: ForceMultiParagraphsOnSinglePage.docx\n" );
  123. }
  124. }
  125. /// <summary>
  126. /// Create a document and insert, remove and replace texts.
  127. /// </summary>
  128. public static void TextActions()
  129. {
  130. Console.WriteLine( "\tTextActions()" );
  131. // Create a new document.
  132. using( DocX document = DocX.Create( ParagraphSample.ParagraphSampleOutputDirectory + @"TextActions.docx" ) )
  133. {
  134. // Add a title
  135. document.InsertParagraph( "Insert, remove and replace text" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;
  136. // Create a paragraph and insert text.
  137. var p1 = document.InsertParagraph( "In this paragraph we insert a comma, a colon and " );
  138. // Add a comma at index 17.
  139. p1.InsertText( 17, "," );
  140. // Add a colon at index of character 'd'.
  141. p1.InsertText( p1.Text.IndexOf( "d" ) + 1, ": " );
  142. // Add "a name" at the end of p1.Text.
  143. p1.InsertText( p1.Text.Length, "a name." );
  144. p1.SpacingAfter( 30 );
  145. // Create a paragraph and insert text.
  146. var p2 = document.InsertParagraph( "In this paragraph, we remove a mistaken word and a comma." );
  147. // Remove the word "mistaken".
  148. p2.RemoveText( 31, 9 );
  149. // Remove the comma sign.
  150. p2.RemoveText( p2.Text.IndexOf( "," ), 1 );
  151. p2.SpacingAfter( 30 );
  152. // Create a paragraph and insert text.
  153. var p3 = document.InsertParagraph( "In this paragraph, we replace an complex word with an easier one and spaces with hyphens." );
  154. // Replace the "complex" word with "easy" word.
  155. p3.ReplaceText( "complex", "easy" );
  156. // Replace the spaces with tabs
  157. p3.ReplaceText( " ", "--" );
  158. p3.SpacingAfter( 30 );
  159. // Create a paragraph and insert text.
  160. var p4 = document.InsertParagraph( "In this paragraph, we replace a word by using a handler: <COST>." );
  161. // Replace "<COST>" with "$13.95" using an handler
  162. p4.ReplaceText( "<(.*?)>", ReplaceTextHandler, false, RegexOptions.IgnoreCase, null, new Formatting() );
  163. // Save this document to disk.
  164. document.Save();
  165. Console.WriteLine( "\tCreated: TextActions.docx\n" );
  166. }
  167. }
  168. /// <summary>
  169. /// Set different Heading type for a Paragraph.
  170. /// </summary>
  171. public static void Heading()
  172. {
  173. Console.WriteLine( "\tHeading()" );
  174. // Create a document.
  175. using( DocX document = DocX.Create( ParagraphSample.ParagraphSampleOutputDirectory + @"Heading.docx" ) )
  176. {
  177. // Add a title.
  178. document.InsertParagraph( "Heading types" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;
  179. var headingTypes = Enum.GetValues( typeof( HeadingType ) );
  180. foreach( HeadingType heading in headingTypes )
  181. {
  182. // Set a text containing the current Heading type.
  183. var text = string.Format( "This Paragraph is using \"{0}\" heading type.", heading.EnumDescription() );
  184. // Add a paragraph.
  185. var p = document.InsertParagraph().AppendLine( text );
  186. // Set the paragraph's heading type.
  187. p.Heading( heading );
  188. }
  189. document.Save();
  190. Console.WriteLine( "\tCreated: Heading.docx\n" );
  191. }
  192. }
  193. #endregion
  194. #region Private Methods
  195. private static string ReplaceTextHandler( string findStr )
  196. {
  197. if( _replacePatterns.ContainsKey( findStr ) )
  198. {
  199. return _replacePatterns[ findStr ];
  200. }
  201. return findStr;
  202. }
  203. #endregion
  204. }
  205. }