You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ParagraphSample.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. .SpacingAfter( 40 );
  67. // Insert another Paragraph into this document.
  68. var p3 = document.InsertParagraph();
  69. // Append some text with 2 TabStopPositions.
  70. p3.InsertTabStopPosition( Alignment.center, 216f, TabStopPositionLeader.dot )
  71. .InsertTabStopPosition( Alignment.right, 432f, TabStopPositionLeader.dot )
  72. .Append( "Text with TabStopPositions on Left\tMiddle\tand Right" )
  73. .SpacingAfter( 40 );
  74. // Save this document to disk.
  75. document.Save();
  76. Console.WriteLine( "\tCreated: SimpleFormattedParagraphs.docx\n" );
  77. }
  78. }
  79. /// <summary>
  80. /// Create a document and add a paragraph with all its lines on a single page.
  81. /// </summary>
  82. public static void ForceParagraphOnSinglePage()
  83. {
  84. Console.WriteLine( "\tForceParagraphOnSinglePage()" );
  85. // Create a new document.
  86. using( DocX document = DocX.Create( ParagraphSample.ParagraphSampleOutputDirectory + @"ForceParagraphOnSinglePage.docx" ) )
  87. {
  88. // Add a title
  89. document.InsertParagraph( "Prevent paragraph split" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;
  90. // Create a Paragraph that will appear on 1st page.
  91. 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" );
  92. p.FontSize(15).SpacingAfter( 30 );
  93. // Create a Paragraph where all its lines will appear on a same page.
  94. 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" );
  95. p2.SpacingAfter( 30 );
  96. // Indicate that all the paragraph's lines will be on the same page
  97. p2.KeepLinesTogether();
  98. // Create a Paragraph that will appear on 2nd page.
  99. var p3 = document.InsertParagraph( "This is a paragraph on second page.\nLine2\nLine3\nLine4\nLine5\nLine6\nLine7\nLine8\nLine9\nLine10" );
  100. // Save this document to disk.
  101. document.Save();
  102. Console.WriteLine( "\tCreated: ForceParagraphOnSinglePage.docx\n" );
  103. }
  104. }
  105. /// <summary>
  106. /// Create a document and add a paragraph with all its lines on the same page as the next paragraph.
  107. /// </summary>
  108. public static void ForceMultiParagraphsOnSinglePage()
  109. {
  110. Console.WriteLine( "\tForceMultiParagraphsOnSinglePage()" );
  111. // Create a new document.
  112. using( DocX document = DocX.Create( ParagraphSample.ParagraphSampleOutputDirectory + @"ForceMultiParagraphsOnSinglePage.docx" ) )
  113. {
  114. // Add a title.
  115. document.InsertParagraph( "Keeps Paragraphs on same page" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;
  116. // Create a Paragraph that will appear on 1st page.
  117. 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" );
  118. p.FontSize( 15 ).SpacingAfter( 30 );
  119. // Create a Paragraph where all its lines will appear on a same page as the next paragraph.
  120. 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" );
  121. p2.SpacingAfter( 30 );
  122. // Indicate that this paragraph will be on the same page as the next paragraph.
  123. p2.KeepWithNextParagraph();
  124. // Create a Paragraph that will appear on 2nd page.
  125. var p3 = document.InsertParagraph( "This is a paragraph on second page.\nLine2\nLine3\nLine4\nLine5\nLine6\nLine7\nLine8\nLine9\nLine10" );
  126. // Indicate that all this paragraph's lines will be on the same page.
  127. p3.KeepLinesTogether();
  128. // Save this document to disk.
  129. document.Save();
  130. Console.WriteLine( "\tCreated: ForceMultiParagraphsOnSinglePage.docx\n" );
  131. }
  132. }
  133. /// <summary>
  134. /// Create a document and insert, remove and replace texts.
  135. /// </summary>
  136. public static void TextActions()
  137. {
  138. Console.WriteLine( "\tTextActions()" );
  139. // Create a new document.
  140. using( DocX document = DocX.Create( ParagraphSample.ParagraphSampleOutputDirectory + @"TextActions.docx" ) )
  141. {
  142. // Add a title
  143. document.InsertParagraph( "Insert, remove and replace text" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;
  144. // Create a paragraph and insert text.
  145. var p1 = document.InsertParagraph( "In this paragraph we insert a comma, a colon and " );
  146. // Add a comma at index 17.
  147. p1.InsertText( 17, "," );
  148. // Add a colon at index of character 'd'.
  149. p1.InsertText( p1.Text.IndexOf( "d" ) + 1, ": " );
  150. // Add "a name" at the end of p1.Text.
  151. p1.InsertText( p1.Text.Length, "a name." );
  152. p1.SpacingAfter( 30 );
  153. // Create a paragraph and insert text.
  154. var p2 = document.InsertParagraph( "In this paragraph, we remove a mistaken word and a comma." );
  155. // Remove the word "mistaken".
  156. p2.RemoveText( 31, 9 );
  157. // Remove the comma sign.
  158. p2.RemoveText( p2.Text.IndexOf( "," ), 1 );
  159. p2.SpacingAfter( 30 );
  160. // Create a paragraph and insert text.
  161. var p3 = document.InsertParagraph( "In this paragraph, we replace an complex word with an easier one and spaces with hyphens." );
  162. // Replace the "complex" word with "easy" word.
  163. p3.ReplaceText( "complex", "easy" );
  164. // Replace the spaces with tabs
  165. p3.ReplaceText( " ", "--" );
  166. p3.SpacingAfter( 30 );
  167. // Create a paragraph and insert text.
  168. var p4 = document.InsertParagraph( "In this paragraph, we replace a word by using a handler: <COST>." );
  169. // Replace "<COST>" with "$13.95" using an handler
  170. p4.ReplaceText( "<(.*?)>", ReplaceTextHandler, false, RegexOptions.IgnoreCase, null, new Formatting() );
  171. p4.SpacingAfter( 30 );
  172. // Insert another Paragraph into this document.
  173. var p5 = document.InsertParagraph();
  174. // Append some text with track changes
  175. p5.Append( "This is a paragraph where tracking of modifications is used." );
  176. p5.ReplaceText( "modifications", "changes", true );
  177. // Save this document to disk.
  178. document.Save();
  179. Console.WriteLine( "\tCreated: TextActions.docx\n" );
  180. }
  181. }
  182. /// <summary>
  183. /// Set different Heading type for a Paragraph.
  184. /// </summary>
  185. public static void Heading()
  186. {
  187. Console.WriteLine( "\tHeading()" );
  188. // Create a document.
  189. using( DocX document = DocX.Create( ParagraphSample.ParagraphSampleOutputDirectory + @"Heading.docx" ) )
  190. {
  191. // Add a title.
  192. document.InsertParagraph( "Heading types" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;
  193. var headingTypes = Enum.GetValues( typeof( HeadingType ) );
  194. foreach( HeadingType heading in headingTypes )
  195. {
  196. // Set a text containing the current Heading type.
  197. var text = string.Format( "This Paragraph is using \"{0}\" heading type.", heading.EnumDescription() );
  198. // Add a paragraph.
  199. var p = document.InsertParagraph().AppendLine( text );
  200. // Set the paragraph's heading type.
  201. p.Heading( heading );
  202. }
  203. document.Save();
  204. Console.WriteLine( "\tCreated: Heading.docx\n" );
  205. }
  206. }
  207. #endregion
  208. #region Private Methods
  209. private static string ReplaceTextHandler( string findStr )
  210. {
  211. if( _replacePatterns.ContainsKey( findStr ) )
  212. {
  213. return _replacePatterns[ findStr ];
  214. }
  215. return findStr;
  216. }
  217. #endregion
  218. }
  219. }