Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ProtectionSample.cs 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.Drawing;
  11. using System.IO;
  12. namespace Xceed.Words.NET.Examples
  13. {
  14. public class ProtectionSample
  15. {
  16. #region Private Members
  17. private const string ProtectionSampleOutputDirectory = Program.SampleDirectory + @"Protection\Output\";
  18. #endregion
  19. #region Constructors
  20. static ProtectionSample()
  21. {
  22. if( !Directory.Exists( ProtectionSample.ProtectionSampleOutputDirectory ) )
  23. {
  24. Directory.CreateDirectory( ProtectionSample.ProtectionSampleOutputDirectory );
  25. }
  26. }
  27. #endregion
  28. #region Public Methods
  29. /// <summary>
  30. /// Create a read only document that can be edited by entering a valid password.
  31. /// </summary>
  32. public static void AddPasswordProtection()
  33. {
  34. Console.WriteLine( "\tAddPasswordProtection()" );
  35. // Create a new document.
  36. using( DocX document = DocX.Create( ProtectionSample.ProtectionSampleOutputDirectory + @"AddPasswordProtection.docx" ) )
  37. {
  38. // Add a title
  39. document.InsertParagraph( "Document protection using password" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;
  40. // Insert a Paragraph into this document.
  41. var p = document.InsertParagraph();
  42. // Append some text and add formatting.
  43. p.Append( "This document is protected and can only be edited by stopping its protection with a valid password(\"xceed\")." )
  44. .Font( new Font( "Arial" ) )
  45. .FontSize( 25 )
  46. .Color( Color.Blue )
  47. .Bold();
  48. // Set the document as read only and add a password to unlock it.
  49. document.AddPasswordProtection( EditRestrictions.readOnly, "xceed" );
  50. // Save this document to disk.
  51. document.Save();
  52. Console.WriteLine( "\tCreated: AddPasswordProtection.docx\n" );
  53. }
  54. }
  55. /// <summary>
  56. /// Create a read only document that can be edited by stopping the protection.
  57. /// </summary>
  58. public static void AddProtection()
  59. {
  60. Console.WriteLine( "\tAddProtection()" );
  61. // Create a new document.
  62. using( DocX document = DocX.Create( ProtectionSample.ProtectionSampleOutputDirectory + @"AddProtection.docx" ) )
  63. {
  64. // Add a title.
  65. document.InsertParagraph( "Document protection not using password" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;
  66. // Insert a Paragraph into this document.
  67. var p = document.InsertParagraph();
  68. // Append some text and add formatting.
  69. p.Append( "This document is protected and can only be edited by stopping its protection." )
  70. .Font( new Font( "Arial" ) )
  71. .FontSize( 25 )
  72. .Color( Color.Red )
  73. .Bold();
  74. document.AddProtection( EditRestrictions.readOnly );
  75. // Save this document to disk.
  76. document.Save();
  77. Console.WriteLine( "\tCreated: AddProtection.docx\n" );
  78. }
  79. }
  80. #endregion
  81. }
  82. }