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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 String pageReference;
  30. private String destination = null;
  31. private float xPosition = 0;
  32. private float yPosition = 0;
  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. setPageReference(pageReference);
  41. }
  42. /**
  43. * create a /GoTo object.
  44. *
  45. * @param pageReference the PDF reference to the target page
  46. * @param position the target area's on-page coordinates in points
  47. */
  48. public PDFGoTo(String pageReference, Point2D position) {
  49. /* generic creation of object */
  50. this(pageReference);
  51. setPosition(position);
  52. }
  53. /**
  54. * Sets page reference after object has been created
  55. *
  56. * @param pageReference the new page reference to use
  57. */
  58. public void setPageReference(String pageReference) {
  59. this.pageReference = pageReference;
  60. }
  61. /**
  62. * Sets the target (X,Y) position
  63. *
  64. * @param position the target's on-page coordinates in points
  65. */
  66. public void setPosition(Point2D position) {
  67. this.xPosition = (float) position.getX();
  68. this.yPosition = (float) position.getY();
  69. }
  70. /**
  71. * Sets the x Position to jump to
  72. *
  73. * @param xPosition x position
  74. */
  75. public void setXPosition(float xPosition) {
  76. this.xPosition = xPosition;
  77. }
  78. /**
  79. * Sets the Y position to jump to
  80. *
  81. * @param yPosition y position
  82. */
  83. public void setYPosition(float yPosition) {
  84. this.yPosition = yPosition;
  85. }
  86. /**
  87. * Set the destination string for this Goto.
  88. *
  89. * @param dest the PDF destination string
  90. */
  91. public void setDestination(String dest) {
  92. destination = dest;
  93. }
  94. /**
  95. * Get the PDF reference for the GoTo action.
  96. *
  97. * @return the PDF reference for the action
  98. */
  99. public String getAction() {
  100. return referencePDF();
  101. }
  102. /**
  103. * {@inheritDoc}
  104. */
  105. public String toPDFString() {
  106. String dest;
  107. if (destination == null) {
  108. dest = "/D [" + this.pageReference + " /XYZ " + xPosition
  109. + " " + yPosition + " null]\n";
  110. } else {
  111. dest = "/D [" + this.pageReference + " " + destination + "]\n";
  112. }
  113. return "<< /Type /Action\n/S /GoTo\n" + dest + ">>";
  114. }
  115. /*
  116. * example
  117. * 29 0 obj
  118. * <<
  119. * /S /GoTo
  120. * /D [23 0 R /FitH 600]
  121. * >>
  122. * endobj
  123. */
  124. /** {@inheritDoc} */
  125. protected boolean contentEquals(PDFObject obj) {
  126. if (this == obj) {
  127. return true;
  128. }
  129. if (obj == null || !(obj instanceof PDFGoTo)) {
  130. return false;
  131. }
  132. PDFGoTo gt = (PDFGoTo)obj;
  133. if (gt.pageReference == null) {
  134. if (pageReference != null) {
  135. return false;
  136. }
  137. } else {
  138. if (!gt.pageReference.equals(pageReference)) {
  139. return false;
  140. }
  141. }
  142. if (destination == null) {
  143. if (!(gt.destination == null && gt.xPosition == xPosition
  144. && gt.yPosition == yPosition)) {
  145. return false;
  146. }
  147. } else {
  148. if (!destination.equals(gt.destination)) {
  149. return false;
  150. }
  151. }
  152. return true;
  153. }
  154. }