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.

PDFPages.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.util.List;
  21. import java.util.ArrayList;
  22. /**
  23. * class representing a /Pages object.
  24. *
  25. * A /Pages object is an ordered collection of pages (/Page objects)
  26. * (Actually, /Pages can contain further /Pages as well but this
  27. * implementation doesn't allow this)
  28. */
  29. public class PDFPages extends PDFObject {
  30. /**
  31. * the /Page objects
  32. */
  33. protected List kids = new ArrayList();
  34. /**
  35. * the number of /Page objects
  36. */
  37. protected int count = 0;
  38. // private PDFPages parent;
  39. /**
  40. * create a /Pages object. NOTE: The PDFPages
  41. * object must be created before the PDF document is
  42. * generated, but it is not written to the stream immediately.
  43. * It must also be allocated an object ID (so that the kids
  44. * can refer to the parent) so that the XRef table needs to
  45. * be updated before this object is written.
  46. *
  47. * @param objnum the object's number
  48. */
  49. public PDFPages(int objnum) {
  50. setObjectNumber(objnum);
  51. }
  52. /**
  53. * add a /Page object.
  54. *
  55. * @param page the PDFPage to add.
  56. */
  57. public void addPage(PDFPage page) {
  58. page.setParent(this);
  59. this.incrementCount();
  60. }
  61. /**
  62. * Use this method to notify the PDFPages object that a child page
  63. * @param page the child page
  64. */
  65. public void notifyKidRegistered(PDFPage page) {
  66. int idx = page.getPageIndex();
  67. if (idx >= 0) {
  68. while (idx > this.kids.size() - 1) {
  69. this.kids.add(null);
  70. }
  71. if (this.kids.get(idx) != null) {
  72. throw new IllegalStateException("A page already exists at index "
  73. + idx + " (zero-based).");
  74. }
  75. this.kids.set(idx, page.referencePDF());
  76. } else {
  77. this.kids.add(page.referencePDF());
  78. }
  79. }
  80. /**
  81. * get the count of /Page objects
  82. *
  83. * @return the number of pages
  84. */
  85. public int getCount() {
  86. return this.count;
  87. }
  88. /**
  89. * increment the count of /Page objects
  90. */
  91. public void incrementCount() {
  92. this.count++;
  93. // log.debug("Incrementing count to " + this.getCount());
  94. }
  95. /**
  96. * {@inheritDoc}
  97. */
  98. public String toPDFString() {
  99. StringBuffer sb = new StringBuffer(64);
  100. sb.append(getObjectID()).
  101. append("<< /Type /Pages\n/Count ").
  102. append(this.getCount()).
  103. append("\n/Kids [");
  104. for (int i = 0; i < kids.size(); i++) {
  105. Object kid = kids.get(i);
  106. if (kid == null) {
  107. throw new IllegalStateException("Gap in the kids list!");
  108. }
  109. sb.append(kid).append(" ");
  110. }
  111. sb.append("] >>\nendobj\n");
  112. return sb.toString();
  113. }
  114. }