Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ParallelSample.cs 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.Threading.Tasks;
  12. using System.Linq;
  13. namespace Xceed.Words.NET.Examples
  14. {
  15. public class ParallelSample
  16. {
  17. #region Private Members
  18. private const string ParallelSampleResourcesDirectory = Program.SampleDirectory + @"Parallel\Resources\";
  19. private const string ParallelSampleOutputDirectory = Program.SampleDirectory + @"Parallel\Output\";
  20. #endregion
  21. #region Constructors
  22. static ParallelSample()
  23. {
  24. if( !Directory.Exists( ParallelSample.ParallelSampleOutputDirectory ) )
  25. {
  26. Directory.CreateDirectory( ParallelSample.ParallelSampleOutputDirectory );
  27. }
  28. }
  29. #endregion
  30. #region Public Methods
  31. /// <summary>
  32. /// For each of the documents in the folder 'Parallel\Resources\',
  33. /// Replace the string "Apple" with the string "Potato" and replace the "Apple" image by a "Potato" image.
  34. /// Do this in Parrallel accross many CPU cores.
  35. /// </summary>
  36. public static void DoParallelActions()
  37. {
  38. Console.WriteLine( "\tDoParallelActions()" );
  39. // Get the docx files from the Resources directory.
  40. var inputDir = new DirectoryInfo( ParallelSample.ParallelSampleResourcesDirectory );
  41. var inputFiles = inputDir.GetFiles( "*.docx" );
  42. // Loop through each document and do actions on them.
  43. Parallel.ForEach( inputFiles, f => ParallelSample.Action( f ) );
  44. }
  45. private static void Action( FileInfo file )
  46. {
  47. // Load the document.
  48. using( DocX document = DocX.Load( file.FullName ) )
  49. {
  50. // Replace texts in this document.
  51. document.ReplaceText( "Apples", "Potatoes" );
  52. document.ReplaceText( "An Apple", "A Potato" );
  53. // create the new image
  54. var newImage = document.AddImage( ParallelSample.ParallelSampleResourcesDirectory + @"potato.jpg" );
  55. // Look in each paragraph and remove its first image to replace it with the new one.
  56. foreach( var p in document.Paragraphs )
  57. {
  58. var oldPicture = p.Pictures.FirstOrDefault();
  59. if( oldPicture != null )
  60. {
  61. oldPicture.Remove();
  62. p.AppendPicture( newImage.CreatePicture( 150, 150 ) );
  63. }
  64. }
  65. document.SaveAs( ParallelSample.ParallelSampleOutputDirectory + "Output" + file.Name );
  66. Console.WriteLine( "\tCreated: Output" + file.Name + ".docx\n" );
  67. }
  68. }
  69. #endregion
  70. }
  71. }