AnnotationTypes.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. using GrapeCity.Documents.Text;
  10. using GrapeCity.Documents.Pdf.Annotations;
  11.  
  12. namespace DsPdfWeb.Demos.Basics
  13. {
  14. // Shows how to add annotations to a PDF document.
  15. public class AnnotationTypes
  16. {
  17. public int CreatePDF(Stream stream)
  18. {
  19. var doc = new GcPdfDocument();
  20. var page = doc.NewPage();
  21. var g = page.Graphics;
  22. // User names for annotations' authors:
  23. var user1 = "Jaime Smith";
  24. var user2 = "Jane Donahue";
  25.  
  26. var noteWidth = 72 * 4;
  27. var gap = 8;
  28.  
  29. var rc = Common.Util.AddNote(
  30. "This sample demonstrates some types of annotations that can be created with DsPdf.\n" +
  31. "Note that some annotation types may not display in certain viewers (such as built-in browser viewers)." +
  32. "To see all annotations on this page, open it in Acrobat Reader or other full-featured PDF viewer.",
  33. page);
  34.  
  35. // Text annotation:
  36. var ip = new PointF(rc.X, rc.Bottom + gap);
  37. rc = Common.Util.AddNote(
  38. "A red text annotation is placed to the right of this note.",
  39. page, new RectangleF(ip.X, ip.Y, noteWidth, 100));
  40. var textAnnot = new TextAnnotation()
  41. {
  42. UserName = user1,
  43. Contents = "This is annotation 1, a red one.",
  44. Rect = new RectangleF(rc.Right, rc.Top, 72 * 2, 72),
  45. Color = Color.Red,
  46. };
  47. page.Annotations.Add(textAnnot);
  48. // A reply to previous annotation:
  49. var textAnnotReply = new TextAnnotation()
  50. {
  51. UserName = user2,
  52. Contents = "This is a reply to the first annotation.",
  53. Rect = new RectangleF(rc.Right, rc.Top, 72 * 2, 72),
  54. ReferenceAnnotation = textAnnot,
  55. ReferenceType = AnnotationReferenceType.Reply,
  56. };
  57. page.Annotations.Add(textAnnotReply);
  58.  
  59. // An initially open text annotation:
  60. ip = new PointF(rc.X, rc.Bottom + gap);
  61. rc = Common.Util.AddNote(
  62. "A green text annotation that is initially open is placed to the right of this note.",
  63. page, new RectangleF(ip.X, ip.Y, noteWidth, 100));
  64. var textAnnotOpen = new TextAnnotation()
  65. {
  66. Open = true,
  67. UserName = user1,
  68. Contents = "This is an initially open annotation (green).",
  69. Rect = new RectangleF(rc.Right, rc.Top, 72 * 2, 72),
  70. Color = Color.Green,
  71. };
  72. page.Annotations.Add(textAnnotOpen);
  73.  
  74. // A free text annotation (shows directly on page):
  75. ip = new PointF(rc.X, rc.Bottom + gap);
  76. rc = Common.Util.AddNote(
  77. "A blue free text annotation is placed below and to the right, with a callout going from it to this note.",
  78. page, new RectangleF(ip.X, ip.Y, noteWidth, 100));
  79. var freeAnnot = new FreeTextAnnotation()
  80. {
  81. Rect = new RectangleF(rc.Right + 18, rc.Bottom + 9, 72 * 2, 72),
  82. CalloutLine = new PointF[]
  83. {
  84. new PointF(rc.Left + rc.Width / 2, rc.Bottom),
  85. new PointF(rc.Left + rc.Width / 2, rc.Bottom + 9 + 36),
  86. new PointF(rc.Right + 18, rc.Bottom + 9 + 36),
  87. },
  88. LineWidth = 1,
  89. LineEndStyle = LineEndingStyle.OpenArrow,
  90. LineDashPattern = new float[] { 8, 4 },
  91. Contents = "This is a free text annotation with a callout line going to the note on the left.",
  92. Color = Color.LightSkyBlue,
  93. };
  94. page.Annotations.Add(freeAnnot);
  95.  
  96. // Another free text annotation, with some rich text inside:
  97. ip = new PointF(rc.X, freeAnnot.Rect.Bottom + gap);
  98. var freeRichAnnot = new FreeTextAnnotation()
  99. {
  100. Rect = new RectangleF(ip.X - 144, ip.Y, 72 * 5, 72),
  101. LineWidth = 1,
  102. Color = Color.LightSalmon,
  103. RichText =
  104. "<body><p>This is another <i>free text annotation</i>, with <b><i>Rich Text</i></b> content.</p>" +
  105. "<p>Even though a <b>free text</b> annotation displays text directly on a page, " +
  106. "as other annotations it can be placed outside the page's bounds.</p></body>"
  107. };
  108. page.Annotations.Add(freeRichAnnot);
  109.  
  110. // A square annotation around a note:
  111. ip = new PointF(rc.X, freeRichAnnot.Rect.Bottom + gap * 2);
  112. rc = Common.Util.AddNote(
  113. "A square annotation drawn with a 3pt wide orange line around this note has a rich text associated with it.",
  114. page, new RectangleF(ip.X, ip.Y, noteWidth, 100));
  115.  
  116. rc.Inflate(8, 8);
  117. var squareAnnot = new SquareAnnotation()
  118. {
  119. UserName = user2,
  120. Rect = rc,
  121. LineWidth = 3,
  122. Color = Color.Orange,
  123. RichText =
  124. "<body><p>This <b><i>rich text</i></b> is associated with the square annotation around a text note.</p></body>"
  125. };
  126. page.Annotations.Add(squareAnnot);
  127.  
  128. // Done:
  129. doc.Save(stream);
  130. return doc.Pages.Count;
  131. }
  132. }
  133. }
  134.