Destinations.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.Drawing;
  11. using GrapeCity.Documents.Pdf.Annotations;
  12. using GrapeCity.Documents.Pdf.Actions;
  13. using DsPdfWeb.Demos.Common;
  14.  
  15. namespace DsPdfWeb.Demos.Basics
  16. {
  17. // Demonstrates how to create destinations and associate them with
  18. // outline nodes or links in the document body.
  19. public class Destinations
  20. {
  21. public int CreatePDF(Stream stream)
  22. {
  23. var doc = new GcPdfDocument();
  24.  
  25. var page0 = doc.NewPage();
  26. var page1 = doc.NewPage();
  27. var page2 = doc.NewPage();
  28.  
  29. var mainNote = Util.AddNote(
  30. "This is page 1.\n\n\n" +
  31. "Demo of various destination types.\n\n" +
  32. "Destinations attached to nodes in the document's outline tree:\n" +
  33. " - Page 1: goes to this page, fits whole page\n" +
  34. " - Page 2: goes to page 2, fits whole page\n" +
  35. " -- Note 2, zoom 200%: goes to note 2 on page 2, zooms to 200%\n" +
  36. " -- Zoom 100%: zooms to whole page 2\n" +
  37. " -- Back to page 1: goes back to this page\n" +
  38. " - Page 3: goes to page 3, fits whole page\n" +
  39. " -- Zoom to note on page 3: zooms to whole note on page 3\n" +
  40. " -- Back to page 1: goes back to this page\n" +
  41. " - Named destinations: keyed by names in NamedDestinations dictionary:\n" +
  42. " -- page1\n" +
  43. " -- page2\n" +
  44. " -- page3\n\n" +
  45. "Destinations associated with areas in the document body:",
  46. page0);
  47. Util.AddNote(
  48. "This is page 2.",
  49. page1);
  50. var noteOnPage2 = Util.AddNote(
  51. "Note 2 on page 2.",
  52. page1, new RectangleF(300, 400, 200, 300));
  53. Util.AddNote(
  54. "This is page 3",
  55. page2);
  56. var noteOnPage3 = Util.AddNote(
  57. "This is a somewhat longer\n(even though not really long)\nnote on page 3.",
  58. page2, new RectangleF(200, 440, 200, 300));
  59.  
  60. // Destinations in the outline tree:
  61.  
  62. // DestinationFit: fits whole page:
  63. var on1 = new OutlineNode("Page 1", new DestinationFit(page0));
  64. doc.Outlines.Add(on1);
  65.  
  66. var on2 = new OutlineNode("Page 2", new DestinationFit(page1));
  67. doc.Outlines.Add(on2);
  68. // DestinationXYZ: allows specifying top/left coordinates and the zoom level (1 is 100%):
  69. on2.Children.Add(new OutlineNode("Note 2, zoom 200%", new DestinationXYZ(page1, noteOnPage2.X, noteOnPage2.Y, 2)));
  70. on2.Children.Add(new OutlineNode("Zoom 100%", new DestinationXYZ(page1, null, null, 1)));
  71. // Add a link back to page 1:
  72. on2.Children.Add(new OutlineNode("Back to page 1", new DestinationFit(page0)));
  73.  
  74. var on3 = new OutlineNode("Page 3", new DestinationFit(page2));
  75. doc.Outlines.Add(on3);
  76. // DestinationFitR: fits a rectangle on page:
  77. on3.Children.Add(new OutlineNode("Zoom to note on page 3", new DestinationFitR(2, noteOnPage3)));
  78. // Add links back to page 1 & 2:
  79. on3.Children.Add(new OutlineNode("Go to page 2", new DestinationFit(page1)));
  80. on3.Children.Add(new OutlineNode("Go to page 1", new DestinationFit(page0)));
  81.  
  82. // Destinations in the document body (reusing destinations from the outlines):
  83. // Go to page 2:
  84. var rc = Util.AddNote("Go to page 2", page0, new RectangleF(72, mainNote.Bottom + 18, 200, 72));
  85. page0.Annotations.Add(new LinkAnnotation(rc, on2.Dest));
  86. // Go to page 3:
  87. rc = Util.AddNote("Go to page 3", page0, new RectangleF(72, rc.Bottom + 18, 200, 72));
  88. page0.Annotations.Add(new LinkAnnotation(rc, on3.Dest));
  89.  
  90. // Destinations can also be named and added to the document's NamedDestinations dictionary.
  91. doc.NamedDestinations.Add("page1", new DestinationFit(page0));
  92. doc.NamedDestinations.Add("page2", new DestinationFit(page1));
  93. doc.NamedDestinations.Add("page3", new DestinationFit(page2));
  94. // Then they can be referenced using DestinationRef class, here we add them to the outline:
  95. var onNamed = new OutlineNode("Named destinations", (DestinationBase)null);
  96. doc.Outlines.Add(onNamed);
  97. onNamed.Children.Add(new OutlineNode("Named: page1", new DestinationRef("page1")));
  98. onNamed.Children.Add(new OutlineNode("Named: page2", new DestinationRef("page2")));
  99. onNamed.Children.Add(new OutlineNode("Named: page3", new DestinationRef("page3")));
  100.  
  101. // Done:
  102. doc.Save(stream);
  103. return doc.Pages.Count;
  104. }
  105. }
  106. }
  107.