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

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