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.

PDFLink.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. // Java
  20. import java.awt.geom.Rectangle2D;
  21. import java.util.Set;
  22. /**
  23. * class representing an /Annot object of /Subtype /Link
  24. */
  25. public class PDFLink extends PDFObject {
  26. /**
  27. * Used to represent an external link.
  28. */
  29. public static final int EXTERNAL = 0;
  30. /**
  31. * Used to represent an internal link.
  32. */
  33. public static final int INTERNAL = 1;
  34. private float ulx;
  35. private float uly;
  36. private float brx;
  37. private float bry;
  38. private String color;
  39. private PDFAction action;
  40. private Integer structParent;
  41. /**
  42. * create objects associated with a link annotation (GoToR)
  43. *
  44. * @param r the rectangle of the link hotspot in absolute coordinates
  45. */
  46. public PDFLink(Rectangle2D r) {
  47. /* generic creation of PDF object */
  48. super();
  49. this.ulx = (float)r.getX();
  50. this.uly = (float)r.getY();
  51. this.brx = (float)(r.getX() + r.getWidth());
  52. this.bry = (float)(r.getY() + r.getHeight());
  53. this.color = "0 0 0"; // just for now
  54. }
  55. /**
  56. * Set the pdf action for this link.
  57. * @param action the pdf action that is activated for this link
  58. */
  59. public void setAction(PDFAction action) {
  60. this.action = action;
  61. }
  62. /**
  63. * Sets the value of the StructParent entry for this link.
  64. *
  65. * @param structParent key in the structure parent tree
  66. */
  67. public void setStructParent(int structParent) {
  68. this.structParent = structParent;
  69. }
  70. /**
  71. * {@inheritDoc}
  72. */
  73. public String toPDFString() {
  74. getDocumentSafely().getProfile().verifyAnnotAllowed();
  75. String fFlag = "";
  76. if (getDocumentSafely().getProfile().getPDFAMode().isEnabled()) {
  77. int f = 0;
  78. f |= 1 << (3 - 1); //Print, bit 3
  79. f |= 1 << (4 - 1); //NoZoom, bit 4
  80. f |= 1 << (5 - 1); //NoRotate, bit 5
  81. fFlag = "/F " + f;
  82. }
  83. String s = "<< /Type /Annot\n" + "/Subtype /Link\n" + "/Rect [ "
  84. + (ulx) + " " + (uly) + " "
  85. + (brx) + " " + (bry) + " ]\n" + "/C [ "
  86. + this.color + " ]\n" + "/Border [ 0 0 0 ]\n" + "/A "
  87. + this.action.getAction() + "\n" + "/H /I\n"
  88. + (this.structParent != null
  89. ? "/StructParent " + this.structParent.toString() + "\n" : "")
  90. + fFlag + "\n>>";
  91. return s;
  92. }
  93. /*
  94. * example
  95. * 19 0 obj
  96. * <<
  97. * /Type /Annot
  98. * /Subtype /Link
  99. * /Rect [ 176.032 678.48412 228.73579 692.356 ]
  100. * /C [ 0.86491 0.03421 0.02591 ]
  101. * /Border [ 0 0 1 ]
  102. * /A 28 0 R
  103. * /H /I
  104. * >>
  105. * endobj
  106. */
  107. /** {@inheritDoc} */
  108. protected boolean contentEquals(PDFObject obj) {
  109. if (this == obj) {
  110. return true;
  111. }
  112. if (obj == null || !(obj instanceof PDFLink)) {
  113. return false;
  114. }
  115. PDFLink link = (PDFLink)obj;
  116. if (!((link.ulx == ulx) && (link.uly == uly)
  117. && (link.brx == brx) && (link.bry == bry))) {
  118. return false;
  119. }
  120. if (!(link.color.equals(color)
  121. && link.action.getAction().equals(action.getAction()))) {
  122. return false;
  123. }
  124. return true;
  125. }
  126. @Override
  127. public void getChildren(Set<PDFObject> children) {
  128. super.getChildren(children);
  129. if (action.hasObjectNumber()) {
  130. children.add(action);
  131. }
  132. action.getChildren(children);
  133. }
  134. }