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