PictureWrap.vb
  1. ''
  2. '' This code is part of Document Solutions for Word demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Linq
  7. Imports GrapeCity.Documents.Word
  8. Imports GrapeCity.Documents.Imaging
  9.  
  10. '' This sample adds a paragraph of text, spllits it in the middle,
  11. '' And inserts a picture into the split position.
  12. '' This sequence Is repeated several times with the same text And picture,
  13. '' but with different picture wrapping styles And alignments.
  14. Public Class PictureWrap
  15. Function CreateDocx() As GcWordDocument
  16. Dim doc = New GcWordDocument()
  17. doc.Styles.DefaultParagraphFormat.Alignment = ParagraphAlignment.Both
  18.  
  19. '' Load picture data:
  20. Dim picBytes = File.ReadAllBytes(Path.Combine("Resources", "Images", "road.jpg"))
  21. '' Create a GcBitmap so that we can find out the native picture size:
  22. Dim image = New GcBitmap(picBytes)
  23. '' Scale picture to a smallish size:
  24. Dim width = doc.Body.Sections.Last.PageSetup.ClientWidth / 6
  25. Dim height = image.Height * (width / image.Width)
  26.  
  27. '' The section
  28. Dim sec = doc.Body.Sections.First
  29. '' The paragraphs collection:
  30. Dim pars = sec.GetRange().Paragraphs
  31. '' Generate a sample string (used in all sample paragraphs):
  32. Dim str = LoremIpsumPar(10, 10, 10, 10)
  33. '' Header:
  34. pars.Add("Different picture wrapping styles and alignments.", doc.Styles(BuiltInStyleId.Heading1))
  35.  
  36. '' Function to insert a sized picture into the middle of a paragraph
  37. Dim addPicture As Func(Of String, Picture) =
  38. Function(caption_)
  39. pars.Add(caption_, doc.Styles(BuiltInStyleId.Heading2))
  40. Dim par_ = pars.Add(str)
  41. Dim text_ = par_.GetRange().Texts.First.Split(str.Length / 2)
  42. Dim run_ = text_.ParentRun.Split(text_, InsertLocation.Before)
  43. run_ = run_.Previous
  44. Dim pic_ = run_.GetRange().Pictures.Add(picBytes, "image/jpeg")
  45. pic_.Size.Width.Value = width
  46. pic_.Size.Height.Value = height
  47. Return pic_
  48. End Function
  49.  
  50. '' Add picture using different wrapping styles And alignments:
  51. Dim pic = addPicture("Wrapping style 'In line with text':")
  52.  
  53. pic = addPicture("Wrapping style 'Square', centered:")
  54. pic.WrapFormat.Type = WrapType.Square
  55. pic.Position.Horizontal.Type = ShapePositionType.Alignment
  56. pic.Position.Horizontal.Alignment = ShapeHorizontalRelativeAlignment.Center
  57.  
  58. pic = addPicture("Wrapping style 'Tight', centered:")
  59. pic.WrapFormat.Type = WrapType.Tight
  60. pic.Position.Horizontal.Type = ShapePositionType.Alignment
  61. pic.Position.Horizontal.Alignment = ShapeHorizontalRelativeAlignment.Center
  62.  
  63. '' Add page break
  64. sec.GetRange().Runs.Last.GetRange().Texts.AddBreak(BreakType.Page)
  65.  
  66. pic = addPicture("Wrapping style 'Through', left aligned:")
  67. pic.WrapFormat.Type = WrapType.Through
  68. pic.Position.Horizontal.Type = ShapePositionType.Alignment
  69. pic.Position.Horizontal.Alignment = ShapeHorizontalRelativeAlignment.Left
  70.  
  71. pic = addPicture("Wrapping style 'Top and Bottom', right aligned:")
  72. pic.WrapFormat.Type = WrapType.TopBottom
  73. pic.Position.Horizontal.Type = ShapePositionType.Alignment
  74. pic.Position.Horizontal.Alignment = ShapeHorizontalRelativeAlignment.Right
  75.  
  76. pic = addPicture("Wrapping style 'Behind text', left aligned:")
  77. pic.WrapFormat.BehindText = True
  78. pic.WrapFormat.Type = WrapType.None
  79. pic.Position.Horizontal.Type = ShapePositionType.Alignment
  80. pic.Position.Horizontal.Alignment = ShapeHorizontalRelativeAlignment.Left
  81.  
  82. '' Add page break
  83. sec.GetRange().Runs.Last.GetRange().Texts.AddBreak(BreakType.Page)
  84.  
  85. pic = addPicture("Wrapping style 'In front of text', right aligned:")
  86. pic.WrapFormat.BehindText = False
  87. pic.WrapFormat.Type = WrapType.None
  88. pic.Position.Horizontal.Type = ShapePositionType.Alignment
  89. pic.Position.Horizontal.Alignment = ShapeHorizontalRelativeAlignment.Right
  90.  
  91. pars.Add("The End.")
  92.  
  93. '' Done
  94. Return doc
  95. End Function
  96. End Class
  97.