Columns.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.  
  10. namespace DsWordWeb.Demos
  11. {
  12. // Shows how to create a document with multi-column layout.
  13. public class Columns
  14. {
  15. public GcWordDocument CreateDocx()
  16. {
  17. const float In = 72;
  18. GcWordDocument doc = new GcWordDocument();
  19.  
  20. var sec1 = doc.Body.Sections.First;
  21.  
  22. sec1.PageSetup.TextColumns.Add();
  23.  
  24. var pars1 = sec1.GetRange().Paragraphs;
  25. pars1.Add("Section 1, Paragraph 1: " + Util.LoremIpsumPar());
  26. pars1.Add("Section 1, Paragraph 2: " + Util.LoremIpsumPar());
  27. pars1.Add("Section 1, Paragraph 3: " + Util.LoremIpsumPar());
  28.  
  29. var sec2 = doc.Body.Paragraphs.Last.AddSectionBreak();
  30. sec2.PageSetup.Margin.Left = sec2.PageSetup.Margin.Right =
  31. sec2.PageSetup.Margin.Top = sec2.PageSetup.Margin.Bottom = In / 2;
  32.  
  33. // For the 2nd section, change page orientation:
  34. sec2.PageSetup.Size.Orientation = PageOrientation.Landscape;
  35.  
  36. // Add 3 manually laid out columns:
  37. // - First is 2" wide
  38. // - 2nd and 3rd equally sharing the remaining space
  39. float pw = sec2.PageSetup.ClientWidth;
  40. float space = sec2.PageSetup.TextColumns[0].SpaceAfter;
  41. float c0w = In * 2;
  42. float cw = (pw - c0w - space * 2) / 2;
  43. sec2.PageSetup.TextColumns[0].Width = In * 2;
  44. sec2.PageSetup.TextColumns.Add(cw);
  45. sec2.PageSetup.TextColumns.Add(cw);
  46.  
  47. var pars2 = sec2.GetRange().Paragraphs;
  48. pars2.Add("Section 2, Paragraph 1 followed by a column break.");
  49. pars2.Last.GetRange().Runs.Last.GetRange().Texts.AddBreak(BreakType.Column);
  50.  
  51. pars2.Add("Section 2, Paragraph 2: " + Util.LoremIpsumPar());
  52. pars2.Add("Section 2, Paragraph 3: " + Util.LoremIpsumPar());
  53.  
  54. // Done:
  55. return doc;
  56. }
  57. }
  58. }
  59.