Destinations.vb
  1. ''
  2. '' This code is part of Document Solutions for PDF demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports System.Numerics
  8. Imports GrapeCity.Documents.Pdf
  9. Imports GrapeCity.Documents.Text
  10. Imports GrapeCity.Documents.Drawing
  11. Imports GrapeCity.Documents.Pdf.Annotations
  12. Imports GrapeCity.Documents.Pdf.Actions
  13.  
  14. '' Demonstrates how to create destinations and associate them with
  15. '' outline nodes or links in the document body.
  16. Public Class Destinations
  17. Function CreatePDF(ByVal stream As Stream) As Integer
  18. Dim doc = New GcPdfDocument()
  19.  
  20. Dim page0 = doc.NewPage()
  21. Dim page1 = doc.NewPage()
  22. Dim page2 = doc.NewPage()
  23.  
  24. Dim mainNote = Util.AddNote(
  25. "This is page 1." + vbLf + vbLf + vbLf +
  26. "Demo of various destination types." + vbLf + vbLf +
  27. "Destinations attached to nodes in the document's outline tree:" + vbLf +
  28. " - Page 1: goes to this page, fits whole page" + vbLf +
  29. " - Page 2: goes to page 2, fits whole page" + vbLf +
  30. " -- Note 2, zoom 200%: goes to note 2 on page 2, zooms to 200%" + vbLf +
  31. " -- Zoom 100%: zooms to whole page 2" + vbLf +
  32. " -- Back to page 1: goes back to this page" + vbLf +
  33. " - Page 3: goes to page 3, fits whole page" + vbLf +
  34. " -- Zoom to note on page 3: zooms to whole note on page 3" + vbLf +
  35. " -- Back to page 1: goes back to this page" + vbLf +
  36. " - Named destinations: keyed by names in NamedDestinations dictionary:" + vbLf +
  37. " -- page1" + vbLf +
  38. " -- page2" + vbLf +
  39. " -- page3" + vbLf + vbLf +
  40. "Destinations associated with areas in the document body:",
  41. page0)
  42. Util.AddNote(
  43. "This is page 2.",
  44. page1)
  45. Dim noteOnPage2 = Util.AddNote(
  46. "Note 2 on page 2.",
  47. page1, New RectangleF(300, 400, 200, 300))
  48. Util.AddNote(
  49. "This is page 3",
  50. page2)
  51. Dim noteOnPage3 = Util.AddNote(
  52. "This is a somewhat longer" + vbLf + "(even though not really long)" + vbLf + "note on page 3.",
  53. page2, New RectangleF(200, 440, 200, 300))
  54.  
  55. '' Destinations in the outline tree:
  56.  
  57. '' DestinationFit: fits whole page:
  58. Dim on1 = New OutlineNode("Page 1", New DestinationFit(page0))
  59. doc.Outlines.Add(on1)
  60.  
  61. Dim on2 = New OutlineNode("Page 2", New DestinationFit(page1))
  62. doc.Outlines.Add(on2)
  63. '' DestinationXYZ: allows specifying top/left coordinates and the zoom level (1 is 100%):
  64. on2.Children.Add(New OutlineNode("Note 2, zoom 200%", New DestinationXYZ(page1, noteOnPage2.X, noteOnPage2.Y, 2)))
  65. on2.Children.Add(New OutlineNode("Zoom 100%", New DestinationXYZ(page1, Nothing, Nothing, 1)))
  66. '' Add a link back to page 1:
  67. on2.Children.Add(New OutlineNode("Back to page 1", New DestinationFit(page0)))
  68.  
  69. Dim on3 = New OutlineNode("Page 3", New DestinationFit(page2))
  70. doc.Outlines.Add(on3)
  71. '' DestinationFitR: fits a rectangle on page:
  72. on3.Children.Add(New OutlineNode("Zoom to note on page 3", New DestinationFitR(page2, noteOnPage3)))
  73. '' Add links back to page 1 & 2:
  74. on3.Children.Add(New OutlineNode("Go to page 2", New DestinationFit(page1)))
  75. on3.Children.Add(New OutlineNode("Go to page 1", New DestinationFit(page0)))
  76.  
  77. '' Destinations in the document body (reusing destinations from the outlines):
  78. '' Go to page 2:
  79. Dim rc = Util.AddNote("Go to page 2", page0, New RectangleF(72, mainNote.Bottom + 18, 200, 72))
  80. page0.Annotations.Add(New LinkAnnotation(rc, on2.Dest))
  81. '' Go to page 3:
  82. rc = Util.AddNote("Go to page 3", page0, New RectangleF(72, rc.Bottom + 18, 200, 72))
  83. page0.Annotations.Add(New LinkAnnotation(rc, on3.Dest))
  84.  
  85. '' Destinations can also be named and added to the document's NamedDestinations dictionary.
  86. doc.NamedDestinations.Add("page1", New DestinationFit(page0))
  87. doc.NamedDestinations.Add("page2", New DestinationFit(page1))
  88. doc.NamedDestinations.Add("page3", New DestinationFit(page2))
  89. '' Then they can be referenced using DestinationRef class, here we add them to the outline:
  90. Dim onNamed = New OutlineNode("Named destinations", CType(Nothing, DestinationBase))
  91. doc.Outlines.Add(onNamed)
  92. onNamed.Children.Add(New OutlineNode("Named: page1", New DestinationRef("page1")))
  93. onNamed.Children.Add(New OutlineNode("Named: page2", New DestinationRef("page2")))
  94. onNamed.Children.Add(New OutlineNode("Named: page3", New DestinationRef("page3")))
  95. ''
  96. '' Done:
  97. doc.Save(stream)
  98. Return doc.Pages.Count
  99. End Function
  100. End Class
  101.