Link.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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-2015 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. /** Internally used classes */
  23. #require_once 'Zend/Pdf/Element.php';
  24. #require_once 'Zend/Pdf/Element/Array.php';
  25. #require_once 'Zend/Pdf/Element/Dictionary.php';
  26. #require_once 'Zend/Pdf/Element/Name.php';
  27. #require_once 'Zend/Pdf/Element/Numeric.php';
  28. /** Zend_Pdf_Annotation */
  29. #require_once 'Zend/Pdf/Annotation.php';
  30. /**
  31. * A link annotation represents either a hypertext link to a destination elsewhere in
  32. * the document or an action to be performed.
  33. *
  34. * Only destinations are used now since only GoTo action can be created by user
  35. * in current implementation.
  36. *
  37. * @package Zend_Pdf
  38. * @subpackage Annotation
  39. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. */
  42. class Zend_Pdf_Annotation_Link extends Zend_Pdf_Annotation
  43. {
  44. /**
  45. * Annotation object constructor
  46. *
  47. * @throws Zend_Pdf_Exception
  48. */
  49. public function __construct(Zend_Pdf_Element $annotationDictionary)
  50. {
  51. if ($annotationDictionary->getType() != Zend_Pdf_Element::TYPE_DICTIONARY) {
  52. #require_once 'Zend/Pdf/Exception.php';
  53. throw new Zend_Pdf_Exception('Annotation dictionary resource has to be a dictionary.');
  54. }
  55. if ($annotationDictionary->Subtype === null ||
  56. $annotationDictionary->Subtype->getType() != Zend_Pdf_Element::TYPE_NAME ||
  57. $annotationDictionary->Subtype->value != 'Link') {
  58. #require_once 'Zend/Pdf/Exception.php';
  59. throw new Zend_Pdf_Exception('Subtype => Link entry is requires');
  60. }
  61. parent::__construct($annotationDictionary);
  62. }
  63. /**
  64. * Create link annotation object
  65. *
  66. * @param float $x1
  67. * @param float $y1
  68. * @param float $x2
  69. * @param float $y2
  70. * @param Zend_Pdf_Target|string $target
  71. * @return Zend_Pdf_Annotation_Link
  72. * @throws Zend_Pdf_Exception
  73. */
  74. public static function create($x1, $y1, $x2, $y2, $target)
  75. {
  76. if (is_string($target)) {
  77. #require_once 'Zend/Pdf/Destination/Named.php';
  78. $target = Zend_Pdf_Destination_Named::create($target);
  79. }
  80. if (!$target instanceof Zend_Pdf_Target) {
  81. #require_once 'Zend/Pdf/Exception.php';
  82. throw new Zend_Pdf_Exception('$target parameter must be a Zend_Pdf_Target object or a string.');
  83. }
  84. $annotationDictionary = new Zend_Pdf_Element_Dictionary();
  85. $annotationDictionary->Type = new Zend_Pdf_Element_Name('Annot');
  86. $annotationDictionary->Subtype = new Zend_Pdf_Element_Name('Link');
  87. $rectangle = new Zend_Pdf_Element_Array();
  88. $rectangle->items[] = new Zend_Pdf_Element_Numeric($x1);
  89. $rectangle->items[] = new Zend_Pdf_Element_Numeric($y1);
  90. $rectangle->items[] = new Zend_Pdf_Element_Numeric($x2);
  91. $rectangle->items[] = new Zend_Pdf_Element_Numeric($y2);
  92. $annotationDictionary->Rect = $rectangle;
  93. if ($target instanceof Zend_Pdf_Destination) {
  94. $annotationDictionary->Dest = $target->getResource();
  95. } else {
  96. $annotationDictionary->A = $target->getResource();
  97. }
  98. return new Zend_Pdf_Annotation_Link($annotationDictionary);
  99. }
  100. /**
  101. * Set link annotation destination
  102. *
  103. * @param Zend_Pdf_Target|string $target
  104. * @return Zend_Pdf_Annotation_Link
  105. */
  106. public function setDestination($target)
  107. {
  108. if (is_string($target)) {
  109. #require_once 'Zend/Pdf/Destination/Named.php';
  110. $destination = Zend_Pdf_Destination_Named::create($target);
  111. }
  112. if (!$target instanceof Zend_Pdf_Target) {
  113. #require_once 'Zend/Pdf/Exception.php';
  114. throw new Zend_Pdf_Exception('$target parameter must be a Zend_Pdf_Target object or a string.');
  115. }
  116. $this->_annotationDictionary->touch();
  117. $this->_annotationDictionary->Dest = $destination->getResource();
  118. if ($target instanceof Zend_Pdf_Destination) {
  119. $this->_annotationDictionary->Dest = $target->getResource();
  120. $this->_annotationDictionary->A = null;
  121. } else {
  122. $this->_annotationDictionary->Dest = null;
  123. $this->_annotationDictionary->A = $target->getResource();
  124. }
  125. return $this;
  126. }
  127. /**
  128. * Get link annotation destination
  129. *
  130. * @return Zend_Pdf_Target|null
  131. */
  132. public function getDestination()
  133. {
  134. if ($this->_annotationDictionary->Dest === null &&
  135. $this->_annotationDictionary->A === null) {
  136. return null;
  137. }
  138. if ($this->_annotationDictionary->Dest !== null) {
  139. #require_once 'Zend/Pdf/Destination.php';
  140. return Zend_Pdf_Destination::load($this->_annotationDictionary->Dest);
  141. } else {
  142. #require_once 'Zend/Pdf/Action.php';
  143. return Zend_Pdf_Action::load($this->_annotationDictionary->A);
  144. }
  145. }
  146. }