ParagraphFormatting.cs
  1. //
  2. // This code is part of Document Solutions for PDF 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.Pdf;
  9.  
  10. namespace DsPdfWeb.Demos.Basics
  11. {
  12. // This sample demonstrates the most basic paragraph formatting options:
  13. // - first line indent;
  14. // - line spacing.
  15. public class ParagraphFormatting
  16. {
  17. public int CreatePDF(Stream stream)
  18. {
  19. Func<string> makePara = () => Common.Util.LoremIpsum(1, 5, 10, 15, 30);
  20.  
  21. var doc = new GcPdfDocument();
  22. var g = doc.NewPage().Graphics;
  23. // Using Graphics.CreateTextLayout() ensures that TextLayout's resolution
  24. // is set to the same value as that of the graphics (which is 72 dpi by default):
  25. var tl = g.CreateTextLayout();
  26. // Default font:
  27. tl.DefaultFormat.Font = StandardFonts.Times;
  28. tl.DefaultFormat.FontSize = 12;
  29. // Set TextLayout to the whole page:
  30. tl.MaxWidth = doc.PageSize.Width;
  31. tl.MaxHeight = doc.PageSize.Height;
  32. // ...and have it manage the page margins (1" all around):
  33. tl.MarginAll = tl.Resolution;
  34. // First line offset 1/2":
  35. tl.FirstLineIndent = 72 / 2;
  36. // 1.5 line spacing:
  37. tl.LineSpacingScaleFactor = 1.5f;
  38. //
  39. tl.Append(makePara());
  40. tl.PerformLayout(true);
  41. // Render text at (0,0) (margins are added by TextLayout):
  42. g.DrawTextLayout(tl, PointF.Empty);
  43. // Done:
  44. doc.Save(stream);
  45. return doc.Pages.Count;
  46. }
  47. }
  48. }
  49.