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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. namespace Xceed.Words.NET.Examples
  12. {
  13. public class MarginSample
  14. {
  15. #region Private Members
  16. private const string MarginSampleOutputDirectory = Program.SampleDirectory + @"Margin\Output\";
  17. #endregion
  18. #region Constructors
  19. static MarginSample()
  20. {
  21. if( !Directory.Exists( MarginSample.MarginSampleOutputDirectory ) )
  22. {
  23. Directory.CreateDirectory( MarginSample.MarginSampleOutputDirectory );
  24. }
  25. }
  26. #endregion
  27. #region Public Methods
  28. /// <summary>
  29. /// Modify the direction of text in a paragraph or document.
  30. /// </summary>
  31. public static void SetDirection()
  32. {
  33. Console.WriteLine( "\tSetDirection()" );
  34. // Create a document.
  35. using( DocX document = DocX.Create( MarginSample.MarginSampleOutputDirectory + @"SetDirection.docx" ) )
  36. {
  37. // Add a title.
  38. document.InsertParagraph( "Modify direction of paragraphs" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;
  39. // Add first paragraph.
  40. var p = document.InsertParagraph("This is the first paragraph.");
  41. p.SpacingAfter( 30 );
  42. // Add second paragraph.
  43. var p2 = document.InsertParagraph( "This is the second paragraph." );
  44. p2.SpacingAfter( 30 );
  45. // Make this Paragraph flow right to left. Default is left to right.
  46. p2.Direction = Direction.RightToLeft;
  47. // Add third paragraph.
  48. var p3 = document.InsertParagraph( "This is the third paragraph." );
  49. p3.SpacingAfter( 30 );
  50. // Add fourth paragraph.
  51. var p4 = document.InsertParagraph( "This is the fourth paragraph." );
  52. p4.SpacingAfter( 30 );
  53. // To modify the direction of each paragraph in a document, just set the direction on the document.
  54. document.SetDirection( Direction.RightToLeft );
  55. document.Save();
  56. Console.WriteLine( "\tCreated: SetDirection.docx\n" );
  57. }
  58. }
  59. /// <summary>
  60. /// Add indentations on paragraphs.
  61. /// </summary>
  62. public static void Indentation()
  63. {
  64. Console.WriteLine( "\tIndentation()" );
  65. // Create a document.
  66. using( DocX document = DocX.Create( MarginSample.MarginSampleOutputDirectory + @"Indentation.docx" ) )
  67. {
  68. // Add a title.
  69. document.InsertParagraph( "Paragraph indentation" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;
  70. // Set a smaller page width.
  71. document.PageWidth = 250f;
  72. // Add the first paragraph.
  73. var p = document.InsertParagraph( "This is the first paragraph. It doesn't contain any indentation." );
  74. p.SpacingAfter( 30 );
  75. // Add the second paragraph.
  76. var p2 = document.InsertParagraph( "This is the second paragraph. It contains an indentation on the first line." );
  77. // Indent only the first line of the Paragraph.
  78. p2.IndentationFirstLine = 1.0f;
  79. p2.SpacingAfter( 30 );
  80. // Add the third paragraph.
  81. var p3 = document.InsertParagraph( "This is the third paragraph. It contains an indentation on all the lines except the first one." );
  82. // Indent all the lines of the Paragraph, except the first.
  83. p3.IndentationHanging = 1.0f;
  84. document.Save();
  85. Console.WriteLine( "\tCreated: Indentation.docx\n" );
  86. }
  87. }
  88. /// <summary>
  89. /// Add margins for a document.
  90. /// </summary>
  91. public static void Margins()
  92. {
  93. Console.WriteLine( "\tMargins()" );
  94. // Create a document.
  95. using( DocX document = DocX.Create( MarginSample.MarginSampleOutputDirectory + @"Margins.docx" ) )
  96. {
  97. // Add a title.
  98. document.InsertParagraph( "Document margins" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;
  99. // Set the page width to be smaller.
  100. document.PageWidth = 350f;
  101. // Set the document margins.
  102. document.MarginLeft = 85f;
  103. document.MarginRight = 85f;
  104. document.MarginTop = 0f;
  105. document.MarginBottom = 50f;
  106. // Add a paragraph. It will be affected by the document margins.
  107. var p = document.InsertParagraph("This is a paragraph from a document with a left margin of 85, a right margin of 85, a top margin of 0 and a bottom margin of 50.");
  108. document.Save();
  109. Console.WriteLine( "\tCreated: Margins.docx\n" );
  110. }
  111. }
  112. #endregion
  113. }
  114. }