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.

BookmarkSample.cs 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.Linq;
  12. namespace Xceed.Words.NET.Examples
  13. {
  14. public class BookmarkSample
  15. {
  16. #region Private Members
  17. private const string BookmarkSampleResourcesDirectory = Program.SampleDirectory + @"Bookmark\Resources\";
  18. private const string BookmarkSampleOutputDirectory = Program.SampleDirectory + @"Bookmark\Output\";
  19. #endregion
  20. #region Constructors
  21. static BookmarkSample()
  22. {
  23. if( !Directory.Exists( BookmarkSample.BookmarkSampleOutputDirectory ) )
  24. {
  25. Directory.CreateDirectory( BookmarkSample.BookmarkSampleOutputDirectory );
  26. }
  27. }
  28. #endregion
  29. #region Public Methods
  30. /// <summary>
  31. /// Insert a bookmark in a document and a paragraph(and replace the displayed bookmark).
  32. /// </summary>
  33. public static void InsertBookmarks()
  34. {
  35. Console.WriteLine( "\tInsertBookmarks()" );
  36. // Create a document
  37. using( DocX document = DocX.Create( BookmarkSample.BookmarkSampleOutputDirectory + @"InsertBookmarks.docx" ) )
  38. {
  39. // Add a title
  40. document.InsertParagraph( "Insert Bookmarks" ).FontSize( 15d ).SpacingAfter( 40d ).Alignment = Alignment.center;
  41. // Insert a bookmark in the document.
  42. document.InsertBookmark( "Bookmark1" );
  43. // Add a paragraph
  44. var p = document.InsertParagraph( "This document contains a bookmark named \"" );
  45. p.Append( document.Bookmarks.First().Name );
  46. p.Append( "\" just before this line." );
  47. p.SpacingAfter( 50d );
  48. var _bookmarkName = "Bookmark2";
  49. var _displayedBookmarkName = "special";
  50. // Add another paragraph.
  51. var p2 = document.InsertParagraph( "This paragraph contains a " );
  52. // Add a bookmark into the paragraph.
  53. p2.AppendBookmark( _bookmarkName );
  54. p2.Append( " bookmark named \"" );
  55. p2.Append( document.Bookmarks.Last().Name );
  56. p2.Append( "\" but displayed as \"" + _displayedBookmarkName + "\"." );
  57. // Set a string to be displayed as the Bookmark in the second paragraph.
  58. p2.InsertAtBookmark( _displayedBookmarkName, _bookmarkName );
  59. document.Save();
  60. Console.WriteLine( "\tCreated: InsertBookmarks.docx\n" );
  61. }
  62. }
  63. /// <summary>
  64. /// Load a document with bookmarks and replace the bookmark's text.
  65. /// </summary>
  66. public static void ReplaceText()
  67. {
  68. Console.WriteLine( "\tReplaceBookmarkText()" );
  69. // Load a document
  70. using( DocX document = DocX.Load( BookmarkSample.BookmarkSampleResourcesDirectory + @"DocumentWithBookmarks.docx" ) )
  71. {
  72. // Get the regular bookmark from the document and replace its Text.
  73. var regularBookmark = document.Bookmarks[ "regBookmark" ];
  74. if( regularBookmark != null )
  75. {
  76. regularBookmark.SetText( "Regular Bookmark has been changed" );
  77. }
  78. // Get the formatted bookmark from the document and replace its Text.
  79. var formattedBookmark = document.Bookmarks[ "formattedBookmark" ];
  80. if( formattedBookmark != null )
  81. {
  82. formattedBookmark.SetText( "Formatted Bookmark has been changed" );
  83. }
  84. document.SaveAs( BookmarkSample.BookmarkSampleOutputDirectory + @"ReplaceBookmarkText.docx" );
  85. Console.WriteLine( "\tCreated: ReplaceBookmarkText.docx\n" );
  86. }
  87. }
  88. #endregion
  89. }
  90. }