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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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;
  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. public PDFPages(PDFDocument document) {
  47. setObjectNumber(document);
  48. }
  49. /**
  50. * add a /Page object.
  51. *
  52. * @param page the PDFPage to add.
  53. */
  54. public void addPage(PDFPage page) {
  55. page.setParent(this);
  56. this.incrementCount();
  57. }
  58. /**
  59. * Use this method to notify the PDFPages object that a child page
  60. * @param page the child page
  61. */
  62. public void notifyKidRegistered(PDFPage page) {
  63. int idx = page.getPageIndex();
  64. if (idx >= 0) {
  65. while (idx > this.kids.size() - 1) {
  66. this.kids.add(null);
  67. }
  68. if (this.kids.get(idx) != null) {
  69. throw new IllegalStateException("A page already exists at index "
  70. + idx + " (zero-based).");
  71. }
  72. this.kids.set(idx, page.makeReference());
  73. } else {
  74. this.kids.add(page.makeReference());
  75. }
  76. }
  77. /**
  78. * get the count of /Page objects
  79. *
  80. * @return the number of pages
  81. */
  82. public int getCount() {
  83. return this.count;
  84. }
  85. /**
  86. * increment the count of /Page objects
  87. */
  88. public void incrementCount() {
  89. this.count++;
  90. // log.debug("Incrementing count to " + this.getCount());
  91. }
  92. /**
  93. * {@inheritDoc}
  94. */
  95. public String toPDFString() {
  96. StringBuffer sb = new StringBuffer(64);
  97. sb.append("<< /Type /Pages\n/Count ")
  98. .append(this.getCount())
  99. .append("\n/Kids [");
  100. for (Object kid : kids) {
  101. if (kid == null) {
  102. throw new IllegalStateException("Gap in the kids list!");
  103. }
  104. sb.append(kid).append(" ");
  105. }
  106. sb.append("] >>");
  107. return sb.toString();
  108. }
  109. }