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.

PDFPage.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. import java.awt.geom.Rectangle2D;
  20. /**
  21. * Class representing a /Page object.
  22. * <p>
  23. * There is one of these for every page in a PDF document. The object
  24. * specifies the dimensions of the page and references a /Resources
  25. * object, a contents stream and the page's parent in the page
  26. * hierarchy.
  27. */
  28. public class PDFPage extends PDFResourceContext {
  29. /** the page index (zero-based) */
  30. protected int pageIndex;
  31. /**
  32. * Create a /Page object
  33. *
  34. * @param resources the /Resources object
  35. * @param pageIndex the page's zero-based index (or -1 if the page number is auto-determined)
  36. * @param mediaBox the MediaBox
  37. * @param cropBox the CropBox. If null, mediaBox is used.
  38. * @param bleedBox the BleedBox. If null, cropBox is used.
  39. * @param trimBox the TrimBox. If null, bleedBox is used.
  40. */
  41. public PDFPage(PDFResources resources, int pageIndex,
  42. Rectangle2D mediaBox, Rectangle2D cropBox,
  43. Rectangle2D bleedBox, Rectangle2D trimBox) {
  44. /* generic creation of object */
  45. super(resources);
  46. put("Type", new PDFName("Page"));
  47. /* set fields using parameters */
  48. setSimplePageSize(mediaBox, cropBox, bleedBox, trimBox);
  49. this.pageIndex = pageIndex;
  50. }
  51. private void setSimplePageSize(Rectangle2D mediaBox, Rectangle2D cropBox,
  52. Rectangle2D bleedBox, Rectangle2D trimBox) {
  53. setMediaBox(mediaBox);
  54. if (cropBox == null) {
  55. cropBox = mediaBox;
  56. }
  57. setCropBox(cropBox);
  58. if (bleedBox == null) {
  59. bleedBox = cropBox;
  60. }
  61. setBleedBox(bleedBox); //Recommended by PDF/X
  62. if (trimBox == null) {
  63. trimBox = bleedBox;
  64. }
  65. setTrimBox(trimBox); //Needed for PDF/X
  66. }
  67. private PDFArray toPDFArray(Rectangle2D box) {
  68. return new PDFArray(this, new double[] {
  69. box.getX(), box.getY(), box.getMaxX(), box.getMaxY()});
  70. }
  71. /**
  72. * Sets the "MediaBox" entry
  73. * @param box the media rectangle
  74. */
  75. public void setMediaBox(Rectangle2D box) {
  76. put("MediaBox", toPDFArray(box));
  77. }
  78. /**
  79. * Sets the "CropBox" entry
  80. * @param box the bleed rectangle
  81. */
  82. public void setCropBox(Rectangle2D box) {
  83. put("CropBox", toPDFArray(box));
  84. }
  85. /**
  86. * Sets the "BleedBox" entry
  87. * @param box the bleed rectangle
  88. */
  89. public void setBleedBox(Rectangle2D box) {
  90. put("BleedBox", toPDFArray(box));
  91. }
  92. /**
  93. * Sets the "TrimBox" entry
  94. * @param box the trim rectangle
  95. */
  96. public void setTrimBox(Rectangle2D box) {
  97. put("TrimBox", toPDFArray(box));
  98. }
  99. /**
  100. * set this page contents
  101. *
  102. * @param contents the contents of the page
  103. */
  104. public void setContents(PDFReference contents) {
  105. if (contents != null) {
  106. put("Contents", contents);
  107. }
  108. }
  109. /**
  110. * set this page's parent
  111. *
  112. * @param parent the /Pages object that is this page's parent
  113. */
  114. public void setParent(PDFPages parent) {
  115. put("Parent", new PDFReference(parent));
  116. }
  117. /**
  118. * Set the transition dictionary and duration.
  119. * This sets the duration of the page and the transition
  120. * dictionary used when going to the next page.
  121. *
  122. * @param dur the duration in seconds
  123. * @param tr the transition dictionary
  124. */
  125. public void setTransition(int dur, TransitionDictionary tr) {
  126. put("Dur", Integer.valueOf(dur));
  127. put("Trans", tr);
  128. }
  129. /**
  130. * @return the page Index of this page (zero-based), -1 if it the page index should
  131. * automatically be determined.
  132. */
  133. public int getPageIndex() {
  134. return this.pageIndex;
  135. }
  136. /**
  137. * Sets the "StructParents" value.
  138. * @param structParents the integer key of this object's entry in the structural parent tree.
  139. */
  140. public void setStructParents(int structParents) {
  141. put("StructParents", structParents);
  142. //This is a PDF 1.5 feature. It is set as a work-around for a bug in Adobe Acrobat
  143. //which reports this missing even if the PDF file is PDF 1.4.
  144. setTabs(new PDFName("S"));
  145. }
  146. /**
  147. * Returns the value of the StructParents entry.
  148. *
  149. * @return the StructParents value, <code>null</code> if the entry has not been set
  150. */
  151. public Integer getStructParents() {
  152. return (Integer) get("StructParents");
  153. }
  154. /**
  155. * Specifies the tab order for annotations on a page.
  156. * @param value one of the allowed values (see PDF 1.5)
  157. * @since PDF 1.5
  158. */
  159. public void setTabs(PDFName value) {
  160. put("Tabs", value);
  161. }
  162. }