Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*************************************************************************************
  2. DocX – DocX is the community edition of Xceed Words for .NET
  3. Copyright (C) 2009-2016 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.Packaging;
  11. using System.IO;
  12. namespace Xceed.Words.NET
  13. {
  14. /// <summary>
  15. /// Represents an Image embedded in a document.
  16. /// </summary>
  17. public class Image
  18. {
  19. #region Private Members
  20. /// <summary>
  21. /// A unique id which identifies this Image.
  22. /// </summary>
  23. private string _id;
  24. private DocX _document;
  25. #endregion
  26. #region Internal Members
  27. internal PackageRelationship _pr;
  28. #endregion
  29. #region Public Properties
  30. /// <summary>
  31. /// Returns the id of this Image.
  32. /// </summary>
  33. public string Id
  34. {
  35. get
  36. {
  37. return _id;
  38. }
  39. }
  40. ///<summary>
  41. /// Returns the name of the image file.
  42. ///</summary>
  43. public string FileName
  44. {
  45. get
  46. {
  47. return Path.GetFileName( _pr.TargetUri.ToString() );
  48. }
  49. }
  50. #endregion
  51. #region Constructors
  52. internal Image( DocX document, PackageRelationship pr )
  53. {
  54. _document = document;
  55. _pr = pr;
  56. _id = pr.Id;
  57. }
  58. #endregion
  59. #region Public Methods
  60. public Stream GetStream( FileMode mode, FileAccess access )
  61. {
  62. string temp = _pr.SourceUri.OriginalString;
  63. string start = temp.Remove( temp.LastIndexOf( '/' ) );
  64. string end = _pr.TargetUri.OriginalString;
  65. string full = end.Contains( start ) ? end : start + "/" + end;
  66. return ( new PackagePartStream( _document._package.GetPart( new Uri( full, UriKind.Relative ) ).GetStream( mode, access ) ) );
  67. }
  68. /// <summary>
  69. /// Add an image to a document, create a custom view of that image (picture) and then insert it into a Paragraph using append.
  70. /// </summary>
  71. /// <returns></returns>
  72. /// <example>
  73. /// Add an image to a document, create a custom view of that image (picture) and then insert it into a Paragraph using append.
  74. /// <code>
  75. /// using (DocX document = DocX.Create("Test.docx"))
  76. /// {
  77. /// // Add an image to the document.
  78. /// Image i = document.AddImage(@"Image.jpg");
  79. ///
  80. /// // Create a picture i.e. (A custom view of an image)
  81. /// Picture p = i.CreatePicture();
  82. /// p.FlipHorizontal = true;
  83. /// p.Rotation = 10;
  84. ///
  85. /// // Create a new Paragraph.
  86. /// Paragraph par = document.InsertParagraph();
  87. ///
  88. /// // Append content to the Paragraph.
  89. /// par.Append("Here is a cool picture")
  90. /// .AppendPicture(p)
  91. /// .Append(" don't you think so?");
  92. ///
  93. /// // Save all changes made to this document.
  94. /// document.Save();
  95. /// }
  96. /// </code>
  97. /// </example>
  98. ///
  99. public Picture CreatePicture()
  100. {
  101. return this.CreatePicture( -1, -1 );
  102. }
  103. /// <summary>
  104. /// Add an image to a document with specific height and width, create a custom view of that image (picture) and then insert it into a Paragraph using append.
  105. /// </summary>
  106. public Picture CreatePicture( int height, int width )
  107. {
  108. return Paragraph.CreatePicture( _document, _id, string.Empty, string.Empty, width, height );
  109. }
  110. #endregion
  111. }
  112. }