TabsAlignment.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 System.Collections.Generic;
  9. using GrapeCity.Documents.Pdf;
  10. using GrapeCity.Documents.Text;
  11. using GrapeCity.Documents.Drawing;
  12.  
  13. namespace DsPdfWeb.Demos.Basics
  14. {
  15. // This sample demonstrates how to use TextLayout.TabStops to render columns
  16. // of floating point numbers aligned in different ways:
  17. // - aligned on the decimal point via TabStopAlignment.Separator;
  18. // - left-aligned on the tab position using TabStopAlignment.Leading;
  19. // - centered around the tab position using TabStopAlignment.Center;
  20. // - right-aligned on the tab position using TabStopAlignment.Trailing.
  21. public class TabsAlignment
  22. {
  23. public int CreatePDF(Stream stream)
  24. {
  25. // Create and set up the document:
  26. var doc = new GcPdfDocument();
  27. var page = doc.NewPage();
  28. var g = page.Graphics;
  29. // Create and set up a TextLayout object to print the text:
  30. var tl = g.CreateTextLayout();
  31. tl.MaxWidth = page.Size.Width;
  32. tl.MaxHeight = page.Size.Height;
  33. tl.MarginLeft = tl.MarginRight = tl.MarginTop = tl.MarginBottom = 36;
  34. tl.DefaultFormat.Font = StandardFonts.Times;
  35. tl.DefaultFormat.FontSize = 10;
  36. tl.DefaultFormat.BackColor = Color.FromArgb(217, 217, 217);
  37. // Add tab stops with different alignment types
  38. // (the first tab's ctor creates a TabStopAlignment.Separator TabStop):
  39. tl.TabStops = new List<TabStop>()
  40. {
  41. new TabStop(72, '.'),
  42. new TabStop(72 * 2.5f, TabStopAlignment.Leading),
  43. new TabStop(72 * 5, TabStopAlignment.Center),
  44. new TabStop(72 * 7.5f, TabStopAlignment.Trailing),
  45. };
  46. // Render sample text:
  47. tl.Append($"TabStopAlignment:\r\n\tSeparator '.'\tLeading\tCenter\tTrailing\r\n");
  48. double v0 = 1;
  49. double q = (1 + Math.Sqrt(5)) / 2;
  50. for (int i = 1; i < 50; ++i)
  51. {
  52. tl.Append($"\t{v0:R}\t{v0:R}\t{v0:R}\t{v0:R}\r\n");
  53. v0 *= q;
  54. }
  55. tl.PerformLayout(true);
  56. // Draw the text and images:
  57. g.DrawTextLayout(tl, PointF.Empty);
  58. // Done:
  59. doc.Save(stream);
  60. return doc.Pages.Count;
  61. }
  62. }
  63. }
  64.