Link.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Pdf
  17. * @subpackage Annotation
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id:
  21. */
  22. /** Zend_Pdf_Annotation */
  23. require_once 'Zend/Pdf/Annotation.php';
  24. /** Zend_Pdf_Destination */
  25. require_once 'Zend/Pdf/Destination.php';
  26. /**
  27. * A link annotation represents either a hypertext link to a destination elsewhere in
  28. * the document or an action to be performed.
  29. *
  30. * Only destinations are used now since only GoTo action can be created by user
  31. * in current implementation.
  32. *
  33. * @package Zend_Pdf
  34. * @subpackage Annotation
  35. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. */
  38. class Zend_Pdf_Annotation_Link extends Zend_Pdf_Annotation
  39. {
  40. /**
  41. * Annotation object constructor
  42. *
  43. * @throws Zend_Pdf_Exception
  44. */
  45. public function __construct(Zend_Pdf_Element $annotationDictionary)
  46. {
  47. if ($annotationDictionary->getType() != Zend_Pdf_Element::TYPE_DICTIONARY) {
  48. require_once 'Zend/Pdf/Exception.php';
  49. throw new Zend_Pdf_Exception('Annotation dictionary resource has to be a dictionary.');
  50. }
  51. if ($annotationDictionary->Subtype === null ||
  52. $annotationDictionary->Subtype->getType() != Zend_Pdf_Element::TYPE_NAME ||
  53. $annotationDictionary->Subtype->value != 'Link') {
  54. require_once 'Zend/Pdf/Exception.php';
  55. throw new Zend_Pdf_Exception('Subtype => Link entry is requires');
  56. }
  57. parent::__construct($annotationDictionary);
  58. }
  59. /**
  60. * Create link annotation object
  61. *
  62. * @param float $x1
  63. * @param float $y1
  64. * @param float $x2
  65. * @param float $y2
  66. * @param Zend_Pdf_Target|string $target
  67. * @return Zend_Pdf_Annotation_Link
  68. */
  69. public static function create($x1, $y1, $x2, $y2, $target)
  70. {
  71. if (is_string($target)) {
  72. require_once 'Zend/Pdf/Destination/Named.php';
  73. $destination = Zend_Pdf_Destination_Named::create($target);
  74. }
  75. if (!$target instanceof Zend_Pdf_Target) {
  76. require_once 'Zend/Pdf/Exception.php';
  77. throw new Zend_Pdf_Exception('$target parameter must be a Zend_Pdf_Target object or a string.');
  78. }
  79. $annotationDictionary = new Zend_Pdf_Element_Dictionary();
  80. $annotationDictionary->Type = new Zend_Pdf_Element_Name('Annot');
  81. $annotationDictionary->Subtype = new Zend_Pdf_Element_Name('Link');
  82. $rectangle = new Zend_Pdf_Element_Array();
  83. $rectangle->items[] = new Zend_Pdf_Element_Numeric($x1);
  84. $rectangle->items[] = new Zend_Pdf_Element_Numeric($y1);
  85. $rectangle->items[] = new Zend_Pdf_Element_Numeric($x2);
  86. $rectangle->items[] = new Zend_Pdf_Element_Numeric($y2);
  87. $annotationDictionary->Rect = $rectangle;
  88. if ($target instanceof Zend_Pdf_Destination) {
  89. $annotationDictionary->Dest = $target->getResource();
  90. } else {
  91. $annotationDictionary->A = $target->getResource();
  92. }
  93. return new Zend_Pdf_Annotation_Link($annotationDictionary);
  94. }
  95. /**
  96. * Set link annotation destination
  97. *
  98. * @param Zend_Pdf_Target|string $target
  99. * @return Zend_Pdf_Annotation_Link
  100. */
  101. public function setDestination($target)
  102. {
  103. if (is_string($target)) {
  104. require_once 'Zend/Pdf/Destination/Named.php';
  105. $destination = Zend_Pdf_Destination_Named::create($target);
  106. }
  107. if (!$target instanceof Zend_Pdf_Target) {
  108. require_once 'Zend/Pdf/Exception.php';
  109. throw new Zend_Pdf_Exception('$target parameter must be a Zend_Pdf_Target object or a string.');
  110. }
  111. $this->_annotationDictionary->touch();
  112. $this->_annotationDictionary->Dest = $destination->getResource();
  113. if ($target instanceof Zend_Pdf_Destination) {
  114. $this->_annotationDictionary->Dest = $target->getResource();
  115. $this->_annotationDictionary->A = null;
  116. } else {
  117. $this->_annotationDictionary->Dest = null;
  118. $this->_annotationDictionary->A = $target->getResource();
  119. }
  120. return $this;
  121. }
  122. /**
  123. * Get link annotation destination
  124. *
  125. * @return Zend_Pdf_Target|null
  126. */
  127. public function getDestination()
  128. {
  129. if ($this->_annotationDictionary->Dest === null &&
  130. $this->_annotationDictionary->A === null) {
  131. return null;
  132. }
  133. if ($this->_annotationDictionary->Dest !== null) {
  134. return Zend_Pdf_Destination::load($this->_annotationDictionary->Dest);
  135. } else {
  136. return Zend_Pdf_Action::load($this->_annotationDictionary->A);
  137. }
  138. }
  139. }