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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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.Map;
  23. import org.apache.fop.pdf.PDFAction;
  24. import org.apache.fop.pdf.PDFDocument;
  25. import org.apache.fop.pdf.PDFFactory;
  26. import org.apache.fop.pdf.PDFGoTo;
  27. import org.apache.fop.pdf.PDFLink;
  28. import org.apache.fop.pdf.PDFOutline;
  29. import org.apache.fop.pdf.PDFReference;
  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.getPDFDocument();
  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. for (Object o : tree.getBookmarks()) {
  68. Bookmark b = (Bookmark) o;
  69. renderBookmark(b, null);
  70. }
  71. }
  72. private void renderBookmark(Bookmark bookmark, PDFOutline parent) throws IFException {
  73. if (parent == null) {
  74. parent = getPDFDoc().getOutlineRoot();
  75. }
  76. PDFAction action = getAction(bookmark.getAction());
  77. PDFReference actionRef = (action != null ? action.makeReference() : null);
  78. PDFOutline pdfOutline = getPDFDoc().getFactory().makeOutline(parent,
  79. bookmark.getTitle(), actionRef, bookmark.isShown());
  80. for (Object o : bookmark.getChildBookmarks()) {
  81. Bookmark b = (Bookmark) o;
  82. renderBookmark(b, pdfOutline);
  83. }
  84. }
  85. /** {@inheritDoc} */
  86. public void renderLink(Link link) throws IFException {
  87. Rectangle targetRect = link.getTargetRect();
  88. int pageHeight = documentHandler.getCurrentPageRef().getPageDimension().height;
  89. Rectangle2D targetRect2D = new Rectangle2D.Double(
  90. targetRect.getMinX() / 1000.0,
  91. (pageHeight - targetRect.getMinY() - targetRect.getHeight()) / 1000.0,
  92. targetRect.getWidth() / 1000.0,
  93. targetRect.getHeight() / 1000.0);
  94. PDFAction pdfAction = getAction(link.getAction());
  95. //makeLink() currently needs a PDFAction and not a reference
  96. //TODO Revisit when PDFLink is converted to a PDFDictionary
  97. PDFLink pdfLink = getPDFDoc().getFactory().makeLink(
  98. targetRect2D, pdfAction);
  99. if (pdfLink != null) {
  100. PDFStructElem structure = (PDFStructElem) link.getAction().getStructureTreeElement();
  101. if (documentHandler.getUserAgent().isAccessibilityEnabled() && structure != null) {
  102. documentHandler.getLogicalStructureHandler().addLinkContentItem(pdfLink, structure);
  103. }
  104. documentHandler.getCurrentPage().addAnnotation(pdfLink);
  105. }
  106. }
  107. /**
  108. * Commits all pending elements to the PDF document.
  109. */
  110. public void commit() {
  111. }
  112. /** {@inheritDoc} */
  113. public void addResolvedAction(AbstractAction action) throws IFException {
  114. assert action.isComplete();
  115. PDFAction pdfAction = (PDFAction)this.incompleteActions.remove(action.getID());
  116. if (pdfAction == null) {
  117. getAction(action);
  118. } else if (pdfAction instanceof PDFGoTo) {
  119. PDFGoTo pdfGoTo = (PDFGoTo)pdfAction;
  120. updateTargetLocation(pdfGoTo, (GoToXYAction)action);
  121. } else {
  122. throw new UnsupportedOperationException(
  123. "Action type not supported: " + pdfAction.getClass().getName());
  124. }
  125. }
  126. public int getPageIndex() {
  127. return documentHandler.getCurrentPage().getPageIndex();
  128. }
  129. private PDFAction getAction(AbstractAction action) throws IFException {
  130. if (action == null) {
  131. return null;
  132. }
  133. PDFAction pdfAction = (PDFAction)this.completeActions.get(getCompleteID(action));
  134. if (pdfAction != null) {
  135. return pdfAction;
  136. } else if (action instanceof GoToXYAction) {
  137. pdfAction = (PDFAction) incompleteActions.get(action.getID());
  138. if (pdfAction != null) {
  139. return pdfAction;
  140. } else {
  141. GoToXYAction a = (GoToXYAction)action;
  142. PDFGoTo pdfGoTo = new PDFGoTo(null);
  143. getPDFDoc().assignObjectNumber(pdfGoTo);
  144. if (action.isComplete()) {
  145. updateTargetLocation(pdfGoTo, a);
  146. } else {
  147. this.incompleteActions.put(action.getID(), pdfGoTo);
  148. }
  149. return pdfGoTo;
  150. }
  151. } else if (action instanceof URIAction) {
  152. URIAction u = (URIAction)action;
  153. assert u.isComplete();
  154. String uri = u.getURI();
  155. PDFFactory factory = getPDFDoc().getFactory();
  156. pdfAction = factory.getExternalAction(uri, u.isNewWindow());
  157. if (!pdfAction.hasObjectNumber()) {
  158. //Some PDF actions are pooled
  159. getPDFDoc().registerObject(pdfAction);
  160. }
  161. this.completeActions.put(getCompleteID(action), pdfAction);
  162. return pdfAction;
  163. } else {
  164. throw new UnsupportedOperationException("Unsupported action type: "
  165. + action + " (" + action.getClass().getName() + ")");
  166. }
  167. }
  168. private void updateTargetLocation(PDFGoTo pdfGoTo, GoToXYAction action)
  169. throws IFException {
  170. PageReference pageRef = this.documentHandler.getPageReference(action.getPageIndex());
  171. if (pageRef == null) {
  172. throw new
  173. IFException("Can't resolve page reference @ index: " + action.getPageIndex(), null);
  174. } else {
  175. //Convert target location from millipoints to points and adjust for different
  176. //page origin
  177. Point2D p2d = null;
  178. p2d = new Point2D.Double(
  179. action.getTargetLocation().x / 1000.0,
  180. (pageRef.getPageDimension().height - action.getTargetLocation().y) / 1000.0);
  181. PDFReference pdfPageRef = pageRef.getPageRef();
  182. pdfGoTo.setPageReference(pdfPageRef);
  183. pdfGoTo.setPosition(p2d);
  184. //Queue this object now that it's complete
  185. getPDFDoc().addObject(pdfGoTo);
  186. this.completeActions.put(getCompleteID(action), pdfGoTo);
  187. }
  188. }
  189. private String getCompleteID(AbstractAction action) {
  190. if (action instanceof GoToXYAction && action.isComplete()) {
  191. int extra = ((GoToXYAction) action).getPageIndex();
  192. return action.getID() + "_" + extra;
  193. }
  194. return action.getID();
  195. }
  196. }