KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > pdf > PDFFormXObject


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
18 /* $Id: PDFFormXObject.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop.pdf;
21
22 // Java
23
import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25
26 /**
27  * PDF Form XObject
28  *
29  * A derivative of the PDFXObject, is a PDF Stream that has not only a
30  * dictionary but a stream of image data.
31  */

32 public class PDFFormXObject extends PDFXObject {
33     private PDFStream contents;
34     private String JavaDoc resRef;
35
36     /**
37      * create a FormXObject with the given number and name and load the
38      * image in the object
39      *
40      * @param xnumber the pdf object X number
41      * @param cont the pdf stream contents
42      * @param ref the resource PDF reference
43      */

44     public PDFFormXObject(int xnumber, PDFStream cont, String JavaDoc ref) {
45         super(xnumber, null);
46         contents = cont;
47         resRef = ref;
48     }
49
50     /**
51      * Output the form stream as PDF.
52      * This sets up the form XObject dictionary and adds the content
53      * data stream.
54      *
55      * @param stream the output stream to write the data
56      * @throws IOException if there is an error writing the data
57      * @return the length of the data written
58      */

59     protected int output(OutputStream JavaDoc stream) throws IOException JavaDoc {
60         int length = 0;
61
62         String JavaDoc dictEntries = getFilterList().buildFilterDictEntries();
63
64         final StreamCache encodedStream = encodeStream();
65
66         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(128);
67         sb.append(getObjectID());
68         sb.append("<</Type /XObject\n");
69         sb.append("/Subtype /Form\n");
70         sb.append("/FormType 1\n");
71         sb.append("/BBox [0 0 1000 1000]\n");
72         sb.append("/Matrix [1 0 0 1 0 0]\n");
73         sb.append("/Resources " + resRef + "\n");
74         sb.append("/Length " + (encodedStream.getSize() + 1) + "\n");
75
76         sb.append(dictEntries);
77         sb.append(">>\n");
78
79         // push the pdf dictionary on the writer
80
byte[] pdfBytes = encode(sb.toString());
81         stream.write(pdfBytes);
82         length += pdfBytes.length;
83
84         //Send encoded stream to target OutputStream
85
length += outputStreamData(encodedStream, stream);
86         encodedStream.clear(); //Encoded stream can now be discarded
87

88         pdfBytes = encode("endobj\n");
89         stream.write(pdfBytes);
90         length += pdfBytes.length;
91         
92         // let it gc
93
// this object is retained as a reference to inserting
94
// the same image but the image data is no longer needed
95
contents = null;
96         return length;
97     }
98     
99     /**
100      * @see org.apache.fop.pdf.PDFStream#outputRawStreamData(OutputStream)
101      */

102     protected void outputRawStreamData(OutputStream JavaDoc out) throws IOException JavaDoc {
103         contents.outputRawStreamData(out);
104     }
105
106     
107 }
108
109
Popular Tags