您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ParagraphSample.cs 11KB

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