001: /*
002: * $Id: PdfVersionImp.java 2571 2007-02-06 13:37:06Z blowagie $
003: *
004: * Copyright 2006 Bruno Lowagie
005: *
006: * The contents of this file are subject to the Mozilla Public License Version 1.1
007: * (the "License"); you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
009: *
010: * Software distributed under the License is distributed on an "AS IS" basis,
011: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
012: * for the specific language governing rights and limitations under the License.
013: *
014: * The Original Code is 'iText, a free JAVA-PDF library'.
015: *
016: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
017: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
018: * All Rights Reserved.
019: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
020: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
021: *
022: * Contributor(s): all the names of the contributors are added in the source code
023: * where applicable.
024: *
025: * Alternatively, the contents of this file may be used under the terms of the
026: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
027: * provisions of LGPL are applicable instead of those above. If you wish to
028: * allow use of your version of this file only under the terms of the LGPL
029: * License and not to allow others to use your version of this file under
030: * the MPL, indicate your decision by deleting the provisions above and
031: * replace them with the notice and other provisions required by the LGPL.
032: * If you do not delete the provisions above, a recipient may use your version
033: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
034: *
035: * This library is free software; you can redistribute it and/or modify it
036: * under the terms of the MPL as stated above or under the terms of the GNU
037: * Library General Public License as published by the Free Software Foundation;
038: * either version 2 of the License, or any later version.
039: *
040: * This library is distributed in the hope that it will be useful, but WITHOUT
041: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
042: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
043: * details.
044: *
045: * If you didn't download this code from the following link, you should check if
046: * you aren't using an obsolete version:
047: * http://www.lowagie.com/iText/
048: */
049:
050: package com.lowagie.text.pdf.internal;
051:
052: import java.io.IOException;
053:
054: import com.lowagie.text.DocWriter;
055: import com.lowagie.text.pdf.OutputStreamCounter;
056: import com.lowagie.text.pdf.PdfDictionary;
057: import com.lowagie.text.pdf.PdfName;
058: import com.lowagie.text.pdf.PdfWriter;
059: import com.lowagie.text.pdf.interfaces.PdfVersion;
060:
061: /**
062: * Stores the PDF version information,
063: * knows how to write a PDF Header,
064: * and how to add the version to the catalog (if necessary).
065: */
066:
067: public class PdfVersionImp implements PdfVersion {
068:
069: /** Contains different strings that are part of the header. */
070: public static final byte[][] HEADER = {
071: DocWriter.getISOBytes("\n"),
072: DocWriter.getISOBytes("%PDF-"),
073: DocWriter.getISOBytes("\n%\u00e2\u00e3\u00cf\u00d3\n") };
074:
075: /** Indicates if the header was already written. */
076: protected boolean headerWasWritten = false;
077: /** Indicates if we are working in append mode. */
078: protected boolean appendmode = false;
079: /** The version that was or will be written to the header. */
080: protected char header_version = PdfWriter.VERSION_1_4;
081: /** The version that will be written to the catalog. */
082: protected PdfName catalog_version = null;
083:
084: /**
085: * @see com.lowagie.text.pdf.interfaces.PdfVersion#setPdfVersion(char)
086: */
087: public void setPdfVersion(char version) {
088: if (headerWasWritten || appendmode) {
089: setPdfVersion(getVersionAsName(version));
090: } else {
091: this .header_version = version;
092: }
093: }
094:
095: /**
096: * @see com.lowagie.text.pdf.interfaces.PdfVersion#setAtLeastPdfVersion(char)
097: */
098: public void setAtLeastPdfVersion(char version) {
099: if (version > header_version) {
100: setPdfVersion(version);
101: }
102: }
103:
104: /**
105: * @see com.lowagie.text.pdf.interfaces.PdfVersion#setPdfVersion(com.lowagie.text.pdf.PdfName)
106: */
107: public void setPdfVersion(PdfName version) {
108: if (catalog_version == null
109: || catalog_version.compareTo(version) < 0) {
110: this .catalog_version = version;
111: }
112: }
113:
114: /**
115: * Sets the append mode.
116: */
117: public void setAppendmode(boolean appendmode) {
118: this .appendmode = appendmode;
119: }
120:
121: /**
122: * Writes the header to the OutputStreamCounter.
123: * @throws IOException
124: */
125: public void writeHeader(OutputStreamCounter os) throws IOException {
126: if (appendmode) {
127: os.write(HEADER[0]);
128: } else {
129: os.write(HEADER[1]);
130: os.write(getVersionAsByteArray(header_version));
131: os.write(HEADER[2]);
132: headerWasWritten = true;
133: }
134: }
135:
136: /**
137: * Returns the PDF version as a name.
138: * @param version the version character.
139: */
140: public PdfName getVersionAsName(char version) {
141: switch (version) {
142: case PdfWriter.VERSION_1_2:
143: return PdfWriter.PDF_VERSION_1_2;
144: case PdfWriter.VERSION_1_3:
145: return PdfWriter.PDF_VERSION_1_3;
146: case PdfWriter.VERSION_1_4:
147: return PdfWriter.PDF_VERSION_1_4;
148: case PdfWriter.VERSION_1_5:
149: return PdfWriter.PDF_VERSION_1_5;
150: case PdfWriter.VERSION_1_6:
151: return PdfWriter.PDF_VERSION_1_6;
152: case PdfWriter.VERSION_1_7:
153: return PdfWriter.PDF_VERSION_1_7;
154: default:
155: return PdfWriter.PDF_VERSION_1_4;
156: }
157: }
158:
159: /**
160: * Returns the version as a byte[].
161: * @param version the version character
162: */
163: public byte[] getVersionAsByteArray(char version) {
164: return DocWriter.getISOBytes(getVersionAsName(version)
165: .toString().substring(1));
166: }
167:
168: /** Adds the version to the Catalog dictionary. */
169: public void addToCatalog(PdfDictionary catalog) {
170: if (catalog_version != null) {
171: catalog.put(PdfName.VERSION, catalog_version);
172: }
173: }
174: }
|