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

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.pdf.PDFStructElem;
  31. import org.apache.fop.render.intermediate.IFDocumentNavigationHandler;
  32. import org.apache.fop.render.intermediate.IFException;
  33. import org.apache.fop.render.intermediate.extensions.AbstractAction;
  34. import org.apache.fop.render.intermediate.extensions.Bookmark;
  35. import org.apache.fop.render.intermediate.extensions.BookmarkTree;
  36. import org.apache.fop.render.intermediate.extensions.GoToXYAction;
  37. import org.apache.fop.render.intermediate.extensions.Link;
  38. import org.apache.fop.render.intermediate.extensions.NamedDestination;
  39. import org.apache.fop.render.intermediate.extensions.URIAction;
  40. import org.apache.fop.render.pdf.PDFDocumentHandler.PageReference;
  41. /**
  42. * Implementation of the {@link IFDocumentNavigationHandler} interface for PDF output.
  43. */
  44. public class PDFDocumentNavigationHandler implements IFDocumentNavigationHandler {
  45. private final PDFDocumentHandler documentHandler;
  46. private final Map incompleteActions = new java.util.HashMap();
  47. private final Map completeActions = new java.util.HashMap();
  48. /**
  49. * Default constructor.
  50. * @param documentHandler the parent document handler
  51. */
  52. public PDFDocumentNavigationHandler(PDFDocumentHandler documentHandler) {
  53. super();
  54. this.documentHandler = documentHandler;
  55. }
  56. PDFDocument getPDFDoc() {
  57. return this.documentHandler.pdfDoc;
  58. }
  59. /** {@inheritDoc} */
  60. public void renderNamedDestination(NamedDestination destination) throws IFException {
  61. PDFAction action = getAction(destination.getAction());
  62. getPDFDoc().getFactory().makeDestination(
  63. destination.getName(), action.makeReference());
  64. }
  65. /** {@inheritDoc} */
  66. public void renderBookmarkTree(BookmarkTree tree) throws IFException {
  67. Iterator iter = tree.getBookmarks().iterator();
  68. while (iter.hasNext()) {
  69. Bookmark b = (Bookmark)iter.next();
  70. renderBookmark(b, null);
  71. }
  72. }
  73. private void renderBookmark(Bookmark bookmark, PDFOutline parent) {
  74. if (parent == null) {
  75. parent = getPDFDoc().getOutlineRoot();
  76. }
  77. PDFAction action = getAction(bookmark.getAction());
  78. String actionRef = (action != null ? action.makeReference().toString() : null);
  79. PDFOutline pdfOutline = getPDFDoc().getFactory().makeOutline(parent,
  80. bookmark.getTitle(), actionRef, bookmark.isShown());
  81. Iterator iter = bookmark.getChildBookmarks().iterator();
  82. while (iter.hasNext()) {
  83. Bookmark b = (Bookmark)iter.next();
  84. renderBookmark(b, pdfOutline);
  85. }
  86. }
  87. /** {@inheritDoc} */
  88. public void renderLink(Link link) throws IFException {
  89. Rectangle targetRect = link.getTargetRect();
  90. int pageHeight = documentHandler.currentPageRef.getPageDimension().height;
  91. Rectangle2D targetRect2D = new Rectangle2D.Double(
  92. targetRect.getMinX() / 1000.0,
  93. (pageHeight - targetRect.getMinY() - targetRect.getHeight()) / 1000.0,
  94. targetRect.getWidth() / 1000.0,
  95. targetRect.getHeight() / 1000.0);
  96. PDFAction pdfAction = getAction(link.getAction());
  97. //makeLink() currently needs a PDFAction and not a reference
  98. //TODO Revisit when PDFLink is converted to a PDFDictionary
  99. PDFLink pdfLink = getPDFDoc().getFactory().makeLink(
  100. targetRect2D, pdfAction);
  101. if (pdfLink != null) {
  102. PDFStructElem structure = (PDFStructElem) link.getAction().getStructureTreeElement();
  103. if (documentHandler.getUserAgent().isAccessibilityEnabled() && structure != null) {
  104. documentHandler.getLogicalStructureHandler().addLinkContentItem(pdfLink, structure);
  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. }