1 49 50 package com.lowagie.text.pdf.internal; 51 52 import java.io.IOException ; 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 66 67 public class PdfVersionImp implements PdfVersion { 68 69 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 77 protected boolean headerWasWritten = false; 78 79 protected boolean appendmode = false; 80 81 protected char header_version = PdfWriter.VERSION_1_4; 82 83 protected PdfName catalog_version = null; 84 85 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 100 public void setAtLeastPdfVersion(char version) { 101 if (version > header_version) { 102 setPdfVersion(version); 103 } 104 } 105 106 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 118 public void setAppendmode(boolean appendmode) { 119 this.appendmode = appendmode; 120 } 121 122 126 public void writeHeader(OutputStreamCounter os) throws IOException { 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 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 165 public byte[] getVersionAsByteArray(char version) { 166 return DocWriter.getISOBytes(getVersionAsName(version).toString().substring(1)); 167 } 168 169 170 public void addToCatalog(PdfDictionary catalog) { 171 if(catalog_version != null) { 172 catalog.put(PdfName.VERSION, catalog_version); 173 } 174 } 175 } | Popular Tags |