PictureWrap.cs
- //
- // This code is part of Document Solutions for Word demos.
- // Copyright (c) MESCIUS inc. All rights reserved.
- //
- using System;
- using System.IO;
- using System.Drawing;
- using GrapeCity.Documents.Word;
- using GrapeCity.Documents.Imaging;
-
- namespace DsWordWeb.Demos
- {
- // This sample adds a paragraph of text, splits it in the middle,
- // and inserts a picture into the split position.
- // This sequence is repeated several times with the same text and picture,
- // but with different picture wrapping styles and alignments.
- public class PictureWrap
- {
- public GcWordDocument CreateDocx()
- {
- GcWordDocument doc = new GcWordDocument();
- doc.Styles.DefaultParagraphFormat.Alignment = ParagraphAlignment.Both;
-
- // Load picture data:
- var picBytes = File.ReadAllBytes(Path.Combine("Resources", "Images", "road.jpg"));
- // Create a GcBitmap so that we can find out the native picture size:
- var image = new GcBitmap(picBytes);
- // Scale picture to a smallish size:
- var width = doc.Body.Sections.Last.PageSetup.ClientWidth / 6;
- var height = image.Height * (width / image.Width);
-
- // The section:
- var sec = doc.Body.Sections.First;
- // The paragraphs collection:
- var pars = sec.GetRange().Paragraphs;
- // Generate a sample string (used in all sample paragraphs):
- var str = Util.LoremIpsumPar(10, 10, 10, 10);
- // Header:
- pars.Add("Different picture wrapping styles and alignments.", doc.Styles[BuiltInStyleId.Heading1]);
-
- // Function to insert a sized picture into the middle of a paragraph:
- Func<string, Picture> addPicture = (caption_) => {
- pars.Add(caption_, doc.Styles[BuiltInStyleId.Heading2]);
- var par_ = pars.Add(str);
- var text_ = par_.GetRange().Texts.First.Split(str.Length / 2);
- var run_ = text_.ParentRun.Split(text_, InsertLocation.Before);
- run_ = run_.Previous;
- var pic_ = run_.GetRange().Pictures.Add(picBytes, "image/jpeg");
- pic_.Size.Width.Value = width;
- pic_.Size.Height.Value = height;
- return pic_;
- };
-
- // Add picture using different wrapping styles and alignments:
- var pic = addPicture("Wrapping style 'In line with text':");
-
- pic = addPicture("Wrapping style 'Square', centered:");
- pic.WrapFormat.Type = WrapType.Square;
- pic.Position.Horizontal.Type = ShapePositionType.Alignment;
- pic.Position.Horizontal.Alignment = ShapeHorizontalRelativeAlignment.Center;
-
- pic = addPicture("Wrapping style 'Tight', centered:");
- pic.WrapFormat.Type = WrapType.Tight;
- pic.Position.Horizontal.Type = ShapePositionType.Alignment;
- pic.Position.Horizontal.Alignment = ShapeHorizontalRelativeAlignment.Center;
-
- // Add page break:
- sec.GetRange().Runs.Last.GetRange().Texts.AddBreak(BreakType.Page);
-
- pic = addPicture("Wrapping style 'Through', left aligned:");
- pic.WrapFormat.Type = WrapType.Through;
- pic.Position.Horizontal.Type = ShapePositionType.Alignment;
- pic.Position.Horizontal.Alignment = ShapeHorizontalRelativeAlignment.Left;
-
- pic = addPicture("Wrapping style 'Top and Bottom', right aligned:");
- pic.WrapFormat.Type = WrapType.TopBottom;
- pic.Position.Horizontal.Type = ShapePositionType.Alignment;
- pic.Position.Horizontal.Alignment = ShapeHorizontalRelativeAlignment.Right;
-
- pic = addPicture("Wrapping style 'Behind text', left aligned:");
- pic.WrapFormat.BehindText = true;
- pic.WrapFormat.Type = WrapType.None;
- pic.Position.Horizontal.Type = ShapePositionType.Alignment;
- pic.Position.Horizontal.Alignment = ShapeHorizontalRelativeAlignment.Left;
-
- // Add page break:
- sec.GetRange().Runs.Last.GetRange().Texts.AddBreak(BreakType.Page);
-
- pic = addPicture("Wrapping style 'In front of text', right aligned:");
- pic.WrapFormat.BehindText = false;
- pic.WrapFormat.Type = WrapType.None;
- pic.Position.Horizontal.Type = ShapePositionType.Alignment;
- pic.Position.Horizontal.Alignment = ShapeHorizontalRelativeAlignment.Right;
-
- pars.Add("The End.");
-
- // Done:
- return doc;
- }
- }
- }
-