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.

PDFFormXObject.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.awt.geom.AffineTransform;
  21. import java.awt.geom.Rectangle2D;
  22. import java.io.IOException;
  23. import java.io.OutputStream;
  24. /**
  25. * PDF Form XObject
  26. *
  27. * A derivative of the PDFXObject, is a PDF Stream that has not only a
  28. * dictionary but a stream of image data.
  29. */
  30. public class PDFFormXObject extends PDFXObject {
  31. private PDFStream contents;
  32. /**
  33. * create a FormXObject with the given number and name and load the
  34. * image in the object
  35. *
  36. * @param xnumber the pdf object X number
  37. * @param contents the form's contents
  38. * @param resources the resource PDF reference
  39. */
  40. public PDFFormXObject(int xnumber, PDFStream contents, PDFReference resources) {
  41. super(contents.getDictionary());
  42. put("Name", new PDFName("Form" + xnumber));
  43. this.contents = contents;
  44. put("Type", new PDFName("XObject"));
  45. put("Subtype", new PDFName("Form"));
  46. put("FormType", 1);
  47. setMatrix(new AffineTransform());
  48. if (resources != null) {
  49. put("Resources", resources);
  50. }
  51. }
  52. /**
  53. * Sets the bounding box of the Form XObject.
  54. * @param bbox the bounding box
  55. */
  56. public void setBBox(Rectangle2D bbox) {
  57. PDFArray array = (PDFArray)get("BBox");
  58. if (array == null) {
  59. array = new PDFArray(this);
  60. array.add(bbox.getX());
  61. array.add(bbox.getY());
  62. array.add(bbox.getWidth());
  63. array.add(bbox.getHeight());
  64. put("BBox", array);
  65. } else {
  66. array.set(0, bbox.getX());
  67. array.set(1, bbox.getY());
  68. array.set(2, bbox.getWidth());
  69. array.set(3, bbox.getHeight());
  70. }
  71. }
  72. /**
  73. * Returns the bounding box.
  74. * @return the BBox value
  75. */
  76. public Rectangle2D getBBox() {
  77. PDFArray array = (PDFArray)get("BBox");
  78. if (array != null) {
  79. Rectangle2D rect = new Rectangle2D.Double();
  80. double x = ((Number)array.get(0)).doubleValue();
  81. double y = ((Number)array.get(1)).doubleValue();
  82. double w = ((Number)array.get(2)).doubleValue();
  83. double h = ((Number)array.get(3)).doubleValue();
  84. rect.setFrame(x, y, w, h);
  85. return rect;
  86. } else {
  87. return null;
  88. }
  89. }
  90. /**
  91. * Sets the Matrix value
  92. * @param at the AffineTransform defining the transformation matrix
  93. */
  94. public void setMatrix(AffineTransform at) {
  95. PDFArray array = (PDFArray)get("Matrix");
  96. double[] m = new double[6];
  97. at.getMatrix(m);
  98. if (array == null) {
  99. array = new PDFArray(this);
  100. array.add(m[0]);
  101. array.add(m[1]);
  102. array.add(m[2]);
  103. array.add(m[3]);
  104. array.add(m[4]);
  105. array.add(m[5]);
  106. put("Matrix", array);
  107. } else {
  108. array.set(0, m[0]);
  109. array.set(1, m[1]);
  110. array.set(2, m[2]);
  111. array.set(3, m[3]);
  112. array.set(4, m[4]);
  113. array.set(5, m[5]);
  114. }
  115. }
  116. /**
  117. * Returns the Matrix value.
  118. * @return the Matrix
  119. */
  120. public AffineTransform getMatrix() {
  121. PDFArray array = (PDFArray)get("Matrix");
  122. if (array != null) {
  123. AffineTransform at = new AffineTransform();
  124. double m00 = ((Number)array.get(0)).doubleValue();
  125. double m10 = ((Number)array.get(1)).doubleValue();
  126. double m01 = ((Number)array.get(2)).doubleValue();
  127. double m11 = ((Number)array.get(3)).doubleValue();
  128. double m02 = ((Number)array.get(4)).doubleValue();
  129. double m12 = ((Number)array.get(5)).doubleValue();
  130. at.setTransform(m00, m10, m01, m11, m02, m12);
  131. return at;
  132. } else {
  133. return null;
  134. }
  135. }
  136. /**
  137. * Used to set the contents of the PDF stream.
  138. * @param data the contents as a byte array
  139. * @throws IOException in case of an I/O problem
  140. */
  141. public void setData(byte[] data) throws IOException {
  142. this.contents.setData(data);
  143. }
  144. /** {@inheritDoc} */
  145. protected void outputRawStreamData(OutputStream out) throws IOException {
  146. contents.outputRawStreamData(out);
  147. }
  148. /** {@inheritDoc} */
  149. public int output(OutputStream stream) throws IOException {
  150. final int len = super.output(stream);
  151. //Now that the data has been written, it can be discarded.
  152. this.contents = null;
  153. return len;
  154. }
  155. /** {@inheritDoc} */
  156. protected void populateStreamDict(Object lengthEntry) {
  157. if (get("Matrix") == null) {
  158. put("Matrix", new PDFArray(this, new int[] {1, 0, 0, 1, 0, 0}));
  159. }
  160. super.populateStreamDict(lengthEntry);
  161. }
  162. }