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.1KB

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