Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

DocumentSample.cs 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.IO;
  12. using System.Text.RegularExpressions;
  13. namespace Xceed.Words.NET.Examples
  14. {
  15. public class DocumentSample
  16. {
  17. #region Private Members
  18. private static Dictionary<string, string> _replacePatterns = new Dictionary<string, string>()
  19. {
  20. { "OPPONENT", "Pittsburgh Penguins" },
  21. { "GAME_TIME", "19h30" },
  22. { "GAME_NUMBER", "161" },
  23. { "DATE", "October 18 2016" },
  24. };
  25. private const string DocumentSampleResourcesDirectory = Program.SampleDirectory + @"Document\Resources\";
  26. private const string DocumentSampleOutputDirectory = Program.SampleDirectory + @"Document\Output\";
  27. #endregion
  28. #region Constructors
  29. static DocumentSample()
  30. {
  31. if( !Directory.Exists( DocumentSample.DocumentSampleOutputDirectory ) )
  32. {
  33. Directory.CreateDirectory( DocumentSample.DocumentSampleOutputDirectory );
  34. }
  35. }
  36. #endregion
  37. #region Public Methods
  38. /// <summary>
  39. /// Load a document and replace texts following a replace pattern.
  40. /// </summary>
  41. public static void ReplaceText()
  42. {
  43. Console.WriteLine( "\tReplaceText()" );
  44. // Load a document.
  45. using( DocX document = DocX.Load( DocumentSample.DocumentSampleResourcesDirectory + @"ReplaceText.docx" ) )
  46. {
  47. // Check if all the replace patterns are used in the loaded document.
  48. if( document.FindUniqueByPattern( @"<[\w \=]{4,}>", RegexOptions.IgnoreCase ).Count == _replacePatterns.Count )
  49. {
  50. // Do the replacement
  51. for( int i = 0; i < _replacePatterns.Count; ++i )
  52. {
  53. document.ReplaceText( "<(.*?)>", DocumentSample.ReplaceFunc, false, RegexOptions.IgnoreCase, null, new Formatting() );
  54. }
  55. // Save this document to disk.
  56. document.SaveAs( DocumentSample.DocumentSampleOutputDirectory + @"ReplacedText.docx" );
  57. Console.WriteLine( "\tCreated: ReplacedText.docx\n" );
  58. }
  59. }
  60. }
  61. /// <summary>
  62. /// Add custom properties to a document.
  63. /// </summary>
  64. public static void AddCustomProperties()
  65. {
  66. Console.WriteLine( "\tAddCustomProperties()" );
  67. // Create a new document.
  68. using( DocX document = DocX.Create( DocumentSample.DocumentSampleOutputDirectory + @"AddCustomProperties.docx" ) )
  69. {
  70. // Add a title
  71. document.InsertParagraph( "Adding Custom Properties to a document" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;
  72. //Add custom properties to document.
  73. document.AddCustomProperty( new CustomProperty( "CompanyName", "Xceed Software inc." ) );
  74. document.AddCustomProperty( new CustomProperty( "Product", "Xceed Words for .NET" ) );
  75. document.AddCustomProperty( new CustomProperty( "Address", "10 Boul. de Mortagne" ) );
  76. document.AddCustomProperty( new CustomProperty( "Date", DateTime.Now ) );
  77. // Add a paragraph displaying the number of custom properties.
  78. var p = document.InsertParagraph( "This document contains " ).Append( document.CustomProperties.Count.ToString() ).Append(" Custom Properties :");
  79. p.SpacingAfter( 30 );
  80. // Display each propertie's name and value.
  81. foreach( var prop in document.CustomProperties )
  82. {
  83. document.InsertParagraph( prop.Value.Name ).Append( " = " ).Append( prop.Value.Value.ToString() ).AppendLine();
  84. }
  85. // Save this document to disk.
  86. document.Save();
  87. Console.WriteLine( "\tCreated: AddCustomProperties.docx\n" );
  88. }
  89. }
  90. /// <summary>
  91. /// Add a template to a document.
  92. /// </summary>
  93. public static void ApplyTemplate()
  94. {
  95. Console.WriteLine( "\tApplyTemplate()" );
  96. // Create a new document.
  97. using( DocX document = DocX.Create( DocumentSample.DocumentSampleOutputDirectory + @"ApplyTemplate.docx" ) )
  98. {
  99. // The path to a template document,
  100. var templatePath = DocumentSample.DocumentSampleResourcesDirectory + @"Template.docx";
  101. // Apply a template to the document based on a path.
  102. document.ApplyTemplate( templatePath );
  103. // Add a paragraph at the end of the template.
  104. document.InsertParagraph( "This paragraph is not part of the template." ).FontSize( 15d ).UnderlineStyle(UnderlineStyle.singleLine).SpacingBefore(50d);
  105. // Save this document to disk.
  106. document.Save();
  107. Console.WriteLine( "\tCreated: ApplyTemplate.docx\n" );
  108. }
  109. }
  110. /// <summary>
  111. /// Insert a document at the end of another document.
  112. /// </summary>
  113. public static void AppendDocument()
  114. {
  115. Console.WriteLine( "\tAppendDocument()" );
  116. // Load the first document.
  117. using( DocX document1 = DocX.Load( DocumentSample.DocumentSampleResourcesDirectory + @"First.docx" ) )
  118. {
  119. // Load the second document.
  120. using( DocX document2 = DocX.Load( DocumentSample.DocumentSampleResourcesDirectory + @"Second.docx" ) )
  121. {
  122. // Insert a document at the end of another document.
  123. // When true, document is added at the end. When false, document is added at beginning.
  124. document1.InsertDocument( document2, true );
  125. // Save this document to disk.
  126. document1.SaveAs( DocumentSample.DocumentSampleOutputDirectory + @"AppendDocument.docx" );
  127. Console.WriteLine( "\tCreated: AppendDocument.docx\n" );
  128. }
  129. }
  130. }
  131. #endregion
  132. #region Private Methods
  133. private static string ReplaceFunc( string findStr )
  134. {
  135. if( _replacePatterns.ContainsKey( findStr ) )
  136. {
  137. return _replacePatterns[ findStr ];
  138. }
  139. return findStr;
  140. }
  141. #endregion
  142. }
  143. }