KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > internal > PdfVersionImp


1 /*
2  * $Id: PdfVersionImp.java 2571 2007-02-06 13:37:06Z blowagie $
3  *
4  * Copyright 2006 Bruno Lowagie
5  *
6  * The contents of this file are subject to the Mozilla Public License Version 1.1
7  * (the "License"); you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the License.
13  *
14  * The Original Code is 'iText, a free JAVA-PDF library'.
15  *
16  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18  * All Rights Reserved.
19  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source code
23  * where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34  *
35  * This library is free software; you can redistribute it and/or modify it
36  * under the terms of the MPL as stated above or under the terms of the GNU
37  * Library General Public License as published by the Free Software Foundation;
38  * either version 2 of the License, or any later version.
39  *
40  * This library is distributed in the hope that it will be useful, but WITHOUT
41  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43  * details.
44  *
45  * If you didn't download this code from the following link, you should check if
46  * you aren't using an obsolete version:
47  * http://www.lowagie.com/iText/
48  */

49
50 package com.lowagie.text.pdf.internal;
51
52 import java.io.IOException JavaDoc;
53
54 import com.lowagie.text.DocWriter;
55 import com.lowagie.text.pdf.OutputStreamCounter;
56 import com.lowagie.text.pdf.PdfDictionary;
57 import com.lowagie.text.pdf.PdfName;
58 import com.lowagie.text.pdf.PdfWriter;
59 import com.lowagie.text.pdf.interfaces.PdfVersion;
60
61 /**
62  * Stores the PDF version information,
63  * knows how to write a PDF Header,
64  * and how to add the version to the catalog (if necessary).
65  */

66
67 public class PdfVersionImp implements PdfVersion {
68     
69     /** Contains different strings that are part of the header. */
70     public static final byte[][] HEADER = {
71         DocWriter.getISOBytes("\n"),
72         DocWriter.getISOBytes("%PDF-"),
73         DocWriter.getISOBytes("\n%\u00e2\u00e3\u00cf\u00d3\n")
74     };
75     
76     /** Indicates if the header was already written. */
77     protected boolean headerWasWritten = false;
78     /** Indicates if we are working in append mode. */
79     protected boolean appendmode = false;
80     /** The version that was or will be written to the header. */
81     protected char header_version = PdfWriter.VERSION_1_4;
82     /** The version that will be written to the catalog. */
83     protected PdfName catalog_version = null;
84     
85     /**
86      * @see com.lowagie.text.pdf.interfaces.PdfVersion#setPdfVersion(char)
87      */

88     public void setPdfVersion(char version) {
89         if (headerWasWritten || appendmode) {
90             setPdfVersion(getVersionAsName(version));
91         }
92         else {
93             this.header_version = version;
94         }
95     }
96     
97     /**
98      * @see com.lowagie.text.pdf.interfaces.PdfVersion#setAtLeastPdfVersion(char)
99      */

100     public void setAtLeastPdfVersion(char version) {
101         if (version > header_version) {
102             setPdfVersion(version);
103         }
104     }
105     
106     /**
107      * @see com.lowagie.text.pdf.interfaces.PdfVersion#setPdfVersion(com.lowagie.text.pdf.PdfName)
108      */

109     public void setPdfVersion(PdfName version) {
110         if (catalog_version == null || catalog_version.compareTo(version) < 0) {
111             this.catalog_version = version;
112         }
113     }
114     
115     /**
116      * Sets the append mode.
117      */

118     public void setAppendmode(boolean appendmode) {
119         this.appendmode = appendmode;
120     }
121     
122     /**
123      * Writes the header to the OutputStreamCounter.
124      * @throws IOException
125      */

126     public void writeHeader(OutputStreamCounter os) throws IOException JavaDoc {
127         if (appendmode) {
128             os.write(HEADER[0]);
129         }
130         else {
131             os.write(HEADER[1]);
132             os.write(getVersionAsByteArray(header_version));
133             os.write(HEADER[2]);
134             headerWasWritten = true;
135         }
136     }
137     
138     /**
139      * Returns the PDF version as a name.
140      * @param version the version character.
141      */

142     public PdfName getVersionAsName(char version) {
143         switch(version) {
144         case PdfWriter.VERSION_1_2:
145             return PdfWriter.PDF_VERSION_1_2;
146         case PdfWriter.VERSION_1_3:
147             return PdfWriter.PDF_VERSION_1_3;
148         case PdfWriter.VERSION_1_4:
149             return PdfWriter.PDF_VERSION_1_4;
150         case PdfWriter.VERSION_1_5:
151             return PdfWriter.PDF_VERSION_1_5;
152         case PdfWriter.VERSION_1_6:
153             return PdfWriter.PDF_VERSION_1_6;
154         case PdfWriter.VERSION_1_7:
155             return PdfWriter.PDF_VERSION_1_7;
156         default:
157             return PdfWriter.PDF_VERSION_1_4;
158         }
159     }
160     
161     /**
162      * Returns the version as a byte[].
163      * @param version the version character
164      */

165     public byte[] getVersionAsByteArray(char version) {
166         return DocWriter.getISOBytes(getVersionAsName(version).toString().substring(1));
167     }
168
169     /** Adds the version to the Catalog dictionary. */
170     public void addToCatalog(PdfDictionary catalog) {
171         if(catalog_version != null) {
172             catalog.put(PdfName.VERSION, catalog_version);
173         }
174     }
175 }
Popular Tags