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.

PDFDocumentNavigationHandler.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.render.pdf;
  19. import java.awt.Rectangle;
  20. import java.awt.geom.Point2D;
  21. import java.awt.geom.Rectangle2D;
  22. import java.util.Iterator;
  23. import java.util.Map;
  24. import org.apache.fop.pdf.PDFAction;
  25. import org.apache.fop.pdf.PDFDocument;
  26. import org.apache.fop.pdf.PDFFactory;
  27. import org.apache.fop.pdf.PDFGoTo;
  28. import org.apache.fop.pdf.PDFLink;
  29. import org.apache.fop.pdf.PDFOutline;
  30. import org.apache.fop.render.intermediate.IFDocumentNavigationHandler;
  31. import org.apache.fop.render.intermediate.IFException;
  32. import org.apache.fop.render.intermediate.extensions.AbstractAction;
  33. import org.apache.fop.render.intermediate.extensions.Bookmark;
  34. import org.apache.fop.render.intermediate.extensions.BookmarkTree;
  35. import org.apache.fop.render.intermediate.extensions.GoToXYAction;
  36. import org.apache.fop.render.intermediate.extensions.Link;
  37. import org.apache.fop.render.intermediate.extensions.NamedDestination;
  38. import org.apache.fop.render.intermediate.extensions.URIAction;
  39. import org.apache.fop.render.pdf.PDFDocumentHandler.PageReference;
  40. /**
  41. * Implementation of the {@link IFDocumentNavigationHandler} interface for PDF output.
  42. */
  43. public class PDFDocumentNavigationHandler implements IFDocumentNavigationHandler {
  44. private final PDFDocumentHandler documentHandler;
  45. private final Map incompleteActions = new java.util.HashMap();
  46. private final Map completeActions = new java.util.HashMap();
  47. /**
  48. * Default constructor.
  49. * @param documentHandler the parent document handler
  50. */
  51. public PDFDocumentNavigationHandler(PDFDocumentHandler documentHandler) {
  52. super();
  53. this.documentHandler = documentHandler;
  54. }
  55. PDFDocument getPDFDoc() {
  56. return this.documentHandler.pdfDoc;
  57. }
  58. /** {@inheritDoc} */
  59. public void renderNamedDestination(NamedDestination destination) throws IFException {
  60. PDFAction action = getAction(destination.getAction());
  61. getPDFDoc().getFactory().makeDestination(
  62. destination.getName(), action.makeReference());
  63. }
  64. /** {@inheritDoc} */
  65. public void renderBookmarkTree(BookmarkTree tree) throws IFException {
  66. Iterator iter = tree.getBookmarks().iterator();
  67. while (iter.hasNext()) {
  68. Bookmark b = (Bookmark)iter.next();
  69. renderBookmark(b, null);
  70. }
  71. }
  72. private void renderBookmark(Bookmark bookmark, PDFOutline parent) {
  73. if (parent == null) {
  74. parent = getPDFDoc().getOutlineRoot();
  75. }
  76. PDFAction action = getAction(bookmark.getAction());
  77. String actionRef = (action != null ? action.makeReference().toString() : null);
  78. PDFOutline pdfOutline = getPDFDoc().getFactory().makeOutline(parent,
  79. bookmark.getTitle(), actionRef, bookmark.isShown());
  80. Iterator iter = bookmark.getChildBookmarks().iterator();
  81. while (iter.hasNext()) {
  82. Bookmark b = (Bookmark)iter.next();
  83. renderBookmark(b, pdfOutline);
  84. }
  85. }
  86. /** {@inheritDoc} */
  87. public void renderLink(Link link) throws IFException {
  88. Rectangle targetRect = link.getTargetRect();
  89. int pageHeight = documentHandler.currentPageRef.getPageDimension().height;
  90. Rectangle2D targetRect2D = new Rectangle2D.Double(
  91. targetRect.getMinX() / 1000.0,
  92. (pageHeight - targetRect.getMinY() - targetRect.getHeight()) / 1000.0,
  93. targetRect.getWidth() / 1000.0,
  94. targetRect.getHeight() / 1000.0);
  95. PDFAction pdfAction = getAction(link.getAction());
  96. //makeLink() currently needs a PDFAction and not a reference
  97. //TODO Revisit when PDFLink is converted to a PDFDictionary
  98. PDFLink pdfLink = getPDFDoc().getFactory().makeLink(
  99. targetRect2D, pdfAction);
  100. if (pdfLink != null) {
  101. String ptr = link.getAction().getStructurePointer();
  102. if (documentHandler.getUserAgent().isAccessibilityEnabled()
  103. && ptr != null && ptr.length() > 0) {
  104. documentHandler.getLogicalStructureHandler().addLinkContentItem(pdfLink, ptr);
  105. }
  106. documentHandler.currentPage.addAnnotation(pdfLink);
  107. }
  108. }
  109. /**
  110. * Commits all pending elements to the PDF document.
  111. */
  112. public void commit() {
  113. }
  114. /** {@inheritDoc} */
  115. public void addResolvedAction(AbstractAction action) throws IFException {
  116. assert action.isComplete();
  117. PDFAction pdfAction = (PDFAction)this.incompleteActions.remove(action.getID());
  118. if (pdfAction == null) {
  119. getAction(action);
  120. } else if (pdfAction instanceof PDFGoTo) {
  121. PDFGoTo pdfGoTo = (PDFGoTo)pdfAction;
  122. updateTargetLocation(pdfGoTo, (GoToXYAction)action);
  123. } else {
  124. throw new UnsupportedOperationException(
  125. "Action type not supported: " + pdfAction.getClass().getName());
  126. }
  127. }
  128. private PDFAction getAction(AbstractAction action) {
  129. if (action == null) {
  130. return null;
  131. }
  132. PDFAction pdfAction = (PDFAction)this.completeActions.get(action.getID());
  133. if (pdfAction != null) {
  134. return pdfAction;
  135. } else if (action instanceof GoToXYAction) {
  136. pdfAction = (PDFAction) incompleteActions.get(action.getID());
  137. if (pdfAction != null) {
  138. return pdfAction;
  139. } else {
  140. GoToXYAction a = (GoToXYAction)action;
  141. PDFGoTo pdfGoTo = new PDFGoTo(null);
  142. getPDFDoc().assignObjectNumber(pdfGoTo);
  143. if (action.isComplete()) {
  144. updateTargetLocation(pdfGoTo, a);
  145. } else {
  146. this.incompleteActions.put(action.getID(), pdfGoTo);
  147. }
  148. return pdfGoTo;
  149. }
  150. } else if (action instanceof URIAction) {
  151. URIAction u = (URIAction)action;
  152. assert u.isComplete();
  153. String uri = u.getURI();
  154. PDFFactory factory = getPDFDoc().getFactory();
  155. pdfAction = factory.getExternalAction(uri, u.isNewWindow());
  156. if (!pdfAction.hasObjectNumber()) {
  157. //Some PDF actions are pooled
  158. getPDFDoc().registerObject(pdfAction);
  159. }
  160. this.completeActions.put(action.getID(), pdfAction);
  161. return pdfAction;
  162. } else {
  163. throw new UnsupportedOperationException("Unsupported action type: "
  164. + action + " (" + action.getClass().getName() + ")");
  165. }
  166. }
  167. private void updateTargetLocation(PDFGoTo pdfGoTo, GoToXYAction action) {
  168. PageReference pageRef = this.documentHandler.getPageReference(action.getPageIndex());
  169. //Convert target location from millipoints to points and adjust for different
  170. //page origin
  171. Point2D p2d = null;
  172. p2d = new Point2D.Double(
  173. action.getTargetLocation().x / 1000.0,
  174. (pageRef.getPageDimension().height - action.getTargetLocation().y) / 1000.0);
  175. String pdfPageRef = pageRef.getPageRef();
  176. pdfGoTo.setPageReference(pdfPageRef);
  177. pdfGoTo.setPosition(p2d);
  178. //Queue this object now that it's complete
  179. getPDFDoc().addObject(pdfGoTo);
  180. this.completeActions.put(action.getID(), pdfGoTo);
  181. }
  182. }