You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PDFGoTo.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.pdf;
  19. import java.awt.geom.Point2D;
  20. /**
  21. * class representing a /GoTo object.
  22. * This can either have a Goto to a page reference and location
  23. * or to a specified PDF reference string.
  24. */
  25. public class PDFGoTo extends PDFAction {
  26. /**
  27. * the pageReference
  28. */
  29. private PDFReference pageReference;
  30. private String destination;
  31. private float xPosition;
  32. private float yPosition;
  33. /**
  34. * create a /GoTo object.
  35. *
  36. * @param pageReference the pageReference represented by this object
  37. */
  38. public PDFGoTo(String pageReference) {
  39. super();
  40. if (pageReference != null) {
  41. setPageReference(new PDFReference(pageReference));
  42. }
  43. }
  44. /**
  45. * create a /GoTo object.
  46. *
  47. * @param pageReference the PDF reference to the target page
  48. * @param position the target area's on-page coordinates in points
  49. */
  50. public PDFGoTo(String pageReference, Point2D position) {
  51. /* generic creation of object */
  52. this(pageReference);
  53. setPosition(position);
  54. }
  55. /**
  56. * Sets page reference after object has been created
  57. *
  58. * @param pageReference the new page reference to use
  59. */
  60. public void setPageReference(PDFReference pageReference) {
  61. this.pageReference = pageReference;
  62. }
  63. /**
  64. * Sets the target (X,Y) position
  65. *
  66. * @param position the target's on-page coordinates in points
  67. */
  68. public void setPosition(Point2D position) {
  69. this.xPosition = (float) position.getX();
  70. this.yPosition = (float) position.getY();
  71. }
  72. /**
  73. * Sets the x Position to jump to
  74. *
  75. * @param xPosition x position
  76. */
  77. public void setXPosition(float xPosition) {
  78. this.xPosition = xPosition;
  79. }
  80. /**
  81. * Sets the Y position to jump to
  82. *
  83. * @param yPosition y position
  84. */
  85. public void setYPosition(float yPosition) {
  86. this.yPosition = yPosition;
  87. }
  88. /**
  89. * Set the destination string for this Goto.
  90. *
  91. * @param dest the PDF destination string
  92. */
  93. public void setDestination(String dest) {
  94. destination = dest;
  95. }
  96. /**
  97. * Get the PDF reference for the GoTo action.
  98. *
  99. * @return the PDF reference for the action
  100. */
  101. public String getAction() {
  102. return referencePDF();
  103. }
  104. /**
  105. * {@inheritDoc}
  106. */
  107. public String toPDFString() {
  108. String dest;
  109. if (destination == null) {
  110. dest = "/D [" + this.pageReference + " /XYZ " + xPosition
  111. + " " + yPosition + " null]\n";
  112. } else {
  113. dest = "/D [" + this.pageReference + " " + destination + "]\n";
  114. }
  115. return "<< /Type /Action\n/S /GoTo\n" + dest + ">>";
  116. }
  117. /*
  118. * example
  119. * 29 0 obj
  120. * <<
  121. * /S /GoTo
  122. * /D [23 0 R /FitH 600]
  123. * >>
  124. * endobj
  125. */
  126. /** {@inheritDoc} */
  127. protected boolean contentEquals(PDFObject obj) {
  128. if (this == obj) {
  129. return true;
  130. }
  131. if (obj == null || !(obj instanceof PDFGoTo)) {
  132. return false;
  133. }
  134. PDFGoTo gt = (PDFGoTo)obj;
  135. if (gt.pageReference == null) {
  136. if (pageReference != null) {
  137. return false;
  138. }
  139. } else {
  140. if (!gt.pageReference.equals(pageReference)) {
  141. return false;
  142. }
  143. }
  144. if (destination == null) {
  145. if (!(gt.destination == null && gt.xPosition == xPosition
  146. && gt.yPosition == yPosition)) {
  147. return false;
  148. }
  149. } else {
  150. if (!destination.equals(gt.destination)) {
  151. return false;
  152. }
  153. }
  154. return true;
  155. }
  156. }