InkAnnotPaths.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.Collections.Generic;
  8. using System.Drawing;
  9. using GrapeCity.Documents.Pdf;
  10. using GrapeCity.Documents.Text;
  11. using GrapeCity.Documents.Pdf.Annotations;
  12.  
  13. namespace DsPdfWeb.Demos.Basics
  14. {
  15. // Shows how to add an ink annotation to a PDF document,
  16. // and how to render its content using the InkAnnotation.Paths property.
  17. public class InkAnnotPaths
  18. {
  19. public int CreatePDF(Stream stream)
  20. {
  21. var doc = new GcPdfDocument();
  22. var page = doc.NewPage();
  23. // User name for the annotation's author:
  24. var user1 = "Jaime Smith";
  25.  
  26. var rc = Common.Util.AddNote(
  27. "This sample creates an ink annotation and shows how to use the InkAnnotation.Paths property " +
  28. "to render the annotation's content. The content is specified by discrete points that should be " +
  29. "connected when a PDF viewer renders them. The points can be connected either by straight or " +
  30. "curved lines depending on the viewer implementation.",
  31. page);
  32.  
  33. var inkAnnot = new InkAnnotation()
  34. {
  35. UserName = user1,
  36. Rect = new RectangleF(rc.Left, rc.Bottom + 20, 72 * 5, 72 * 2),
  37. LineWidth = 2,
  38. Color = Color.DarkBlue,
  39. Contents = "This is an ink annotation drawn via InkAnnotation.Paths."
  40. };
  41. float x0 = 80, x = 80, y = rc.Bottom + 24, h = 18, dx = 2, dy = 4, dx2 = 4, w = 10, xoff = 15;
  42.  
  43. // Scribble 'ink annotation' text:
  44.  
  45. // i
  46. var paths = new List<PointF[]>();
  47. paths.Add(new[] { new PointF(x + w/2, y), new PointF(x + w/2, y + h), new PointF(x + w, y + h*.7f) });
  48. paths.Add(new[] { new PointF(x + w/2 - dx, y - h/3 + dy), new PointF(x + w/2 + dx, y - h/3) });
  49. // n
  50. x += xoff;
  51. paths.Add(new[] { new PointF(x, y), new PointF(x, y + h), new PointF(x, y + h - dy), new PointF(x + w*0.7f, y),
  52. new PointF(x + w - dx/2, y + h*.6f), new PointF(x + w, y + h), new PointF(x + w + dx2, y + h*.7f) });
  53. // k
  54. x += xoff;
  55. paths.Add(new[] { new PointF(x, y - h/3), new PointF(x, y + h) });
  56. paths.Add(new[] { new PointF(x + w, y), new PointF(x + dx, y + h/2 - dy), new PointF(x, y + h/2),
  57. new PointF(x + dx2, y + h/2 + dy), new PointF(x + w, y + h), new PointF(x + w + dx2, y + h*.7f) });
  58.  
  59. // a
  60. x += xoff * 2;
  61. paths.Add(new[] { new PointF(x + w, y + dy), new PointF(x + w/2, y), new PointF(x, y + h/2), new PointF(x + w/2, y + h),
  62. new PointF(x + w, y + dy), new PointF(x + w, y), new PointF(x + w, y + h), new PointF(x + w + dx2, y + h*.7f) });
  63. // n
  64. x += xoff;
  65. paths.Add(new[] { new PointF(x, y), new PointF(x, y + h), new PointF(x, y + h - dy), new PointF(x + w*0.7f, y),
  66. new PointF(x + w - dx/2, y + h*.6f), new PointF(x + w, y + h), new PointF(x + w + dx2, y + h*.7f) });
  67. // n
  68. x += xoff;
  69. paths.Add(new[] { new PointF(x, y), new PointF(x, y + h), new PointF(x, y + h - dy), new PointF(x + w*0.7f, y),
  70. new PointF(x + w - dx/2, y + h*.6f), new PointF(x + w, y + h), new PointF(x + w + dx2, y + h*.7f) });
  71. // o
  72. x += xoff;
  73. paths.Add(new[] { new PointF(x + w/2, y), new PointF(x + w/2 - dx, y), new PointF(x, y + h/2), new PointF(x + w/2, y + h),
  74. new PointF(x + w, y + h/2), new PointF(x + w/2 + dx, y), new PointF(x + w/2, y) });
  75. // t
  76. x += xoff;
  77. paths.Add(new[] { new PointF(x + w/2, y - h/3), new PointF(x + w/2, y + h), new PointF(x + w, y + h*.7f) });
  78. paths.Add(new[] { new PointF(x, y), new PointF(x + w, y) });
  79. // a
  80. x += xoff;
  81. paths.Add(new[] { new PointF(x + w, y + dy), new PointF(x + w/2, y), new PointF(x, y + h/2), new PointF(x + w/2, y + h),
  82. new PointF(x + w, y + dy), new PointF(x + w, y), new PointF(x + w, y + h), new PointF(x + w + dx2, y + h*.7f) });
  83. // t
  84. x += xoff;
  85. paths.Add(new[] { new PointF(x + w/2, y - h/3), new PointF(x + w/2, y + h), new PointF(x + w, y + h*.7f) });
  86. paths.Add(new[] { new PointF(x, y), new PointF(x + w, y) });
  87. // i
  88. x += xoff;
  89. paths.Add(new[] { new PointF(x + w/2, y), new PointF(x + w/2, y + h), new PointF(x + w, y + h*.7f) });
  90. paths.Add(new[] { new PointF(x + w/2 - dx, y - h/3 + dy), new PointF(x + w/2 + dx, y - h/3) });
  91. // o
  92. x += xoff;
  93. paths.Add(new[] { new PointF(x + w/2, y), new PointF(x + w/2 - dx, y), new PointF(x, y + h/2),
  94. new PointF(x + w/2, y + h), new PointF(x + w, y + h/2), new PointF(x + w/2 + dx, y), new PointF(x + w/2, y) });
  95. // n
  96. x += xoff;
  97. paths.Add(new[] { new PointF(x, y), new PointF(x, y + h), new PointF(x, y + h - dy), new PointF(x + w*0.7f, y),
  98. new PointF(x + w - dx/2, y + h*.6f), new PointF(x + w, y + h), new PointF(x + w + dx2, y + h*.7f),
  99. new PointF(x + w*3, y + h*.4f), new PointF(x + w + dx2, y + h + dy*2), new PointF(x0, y + h + dy)} );
  100. inkAnnot.Paths = paths;
  101.  
  102. page.Annotations.Add(inkAnnot);
  103.  
  104. // Done:
  105. doc.Save(stream);
  106. return doc.Pages.Count;
  107. }
  108. }
  109. }
  110.