Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

HyperlinkSample.cs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. using System.Linq;
  13. namespace Xceed.Words.NET.Examples
  14. {
  15. public class HyperlinkSample
  16. {
  17. #region Private Members
  18. private const string HyperlinkSampleOutputDirectory = Program.SampleDirectory + @"Hyperlink\Output\";
  19. #endregion
  20. #region Constructors
  21. static HyperlinkSample()
  22. {
  23. if( !Directory.Exists( HyperlinkSample.HyperlinkSampleOutputDirectory ) )
  24. {
  25. Directory.CreateDirectory( HyperlinkSample.HyperlinkSampleOutputDirectory );
  26. }
  27. }
  28. #endregion
  29. #region Public Methods
  30. /// <summary>
  31. /// Insert/Add/Remove hyperlinks from paragraphs.
  32. /// </summary>
  33. public static void Hyperlinks()
  34. {
  35. Console.WriteLine( "\tHyperlinks()" );
  36. // Create a document
  37. using( DocX document = DocX.Create( HyperlinkSample.HyperlinkSampleOutputDirectory + @"Hyperlinks.docx" ) )
  38. {
  39. // Add a title
  40. document.InsertParagraph( "Insert/Remove Hyperlinks" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;
  41. // Add an Hyperlink into this document.
  42. var h = document.AddHyperlink( "google", new Uri( "http://www.google.com" ) );
  43. // Add a paragraph.
  44. var p = document.InsertParagraph( "The hyperlink has been inserted in this paragraph." );
  45. // insert an hyperlink at specific index in this paragraph.
  46. p.InsertHyperlink( h, 4 );
  47. p.SpacingAfter( 40d );
  48. // Get the first hyperlink in the document.
  49. var hyperlink = document.Hyperlinks.FirstOrDefault();
  50. if( hyperlink != null )
  51. {
  52. // Modify its text and Uri.
  53. hyperlink.Text = "xceed";
  54. hyperlink.Uri = new Uri( "http://www.xceed.com/" );
  55. }
  56. // Add an Hyperlink to this document.
  57. var h2 = document.AddHyperlink( "xceed", new Uri( "http://www.xceed.com/" ) );
  58. // Add a paragraph.
  59. var p2 = document.InsertParagraph( "A formatted hyperlink has been added at the end of this paragraph: " );
  60. // Append an hyperlink to a paragraph.
  61. p2.AppendHyperlink( h2 ).Color( Color.Blue ).UnderlineStyle( UnderlineStyle.singleLine );
  62. p2.Append( "." ).SpacingAfter( 40d );
  63. // Create a bookmark anchor.
  64. var bookmarkAnchor = "bookmarkAnchor";
  65. // Add an Hyperlink to this document pointing to a bookmark anchor.
  66. var h3 = document.AddHyperlink( "Special Data", bookmarkAnchor );
  67. // Add a paragraph.
  68. var p3 = document.InsertParagraph( "An hyperlink pointing to a bookmark of this Document has been added at the end of this paragraph: " );
  69. // Append an hyperlink to a paragraph.
  70. p3.AppendHyperlink( h3 ).Color( Color.Red ).UnderlineStyle( UnderlineStyle.singleLine );
  71. p3.Append( "." ).SpacingAfter( 40d );
  72. // Add an Hyperlink to this document.
  73. var h4 = document.AddHyperlink( "microsoft", new Uri( "http://www.microsoft.com" ) );
  74. // Add a paragraph
  75. var p4 = document.InsertParagraph( "The hyperlink from this paragraph has been removed. " );
  76. // Append an hyperlink to a paragraph.
  77. p4.AppendHyperlink( h4 ).Color( Color.Green ).UnderlineStyle( UnderlineStyle.singleLine ).Italic();
  78. // Remove the first hyperlink of paragraph 4.
  79. p4.RemoveHyperlink( 0 );
  80. // Add a paragraph.
  81. var p5 = document.InsertParagraph( "This is a paragraph containing a" );
  82. // Add a bookmark into the paragraph by setting its bookmark anchor.
  83. p5.AppendBookmark( bookmarkAnchor );
  84. p5.Append( " bookmark " );
  85. p5.Append( "referenced by a hyperlink defined in an earlier paragraph." );
  86. p5.SpacingBefore( 200d );
  87. document.Save();
  88. Console.WriteLine( "\tCreated: Hyperlinks.docx\n" );
  89. }
  90. }
  91. #endregion
  92. }
  93. }