1 49 50 package com.lowagie.text.pdf.internal; 51 52 import java.awt.Color ; 53 54 import com.lowagie.text.pdf.BaseFont; 55 import com.lowagie.text.pdf.ExtendedColor; 56 import com.lowagie.text.pdf.PatternColor; 57 import com.lowagie.text.pdf.PdfArray; 58 import com.lowagie.text.pdf.PdfDictionary; 59 import com.lowagie.text.pdf.PdfGState; 60 import com.lowagie.text.pdf.PdfImage; 61 import com.lowagie.text.pdf.PdfName; 62 import com.lowagie.text.pdf.PdfNumber; 63 import com.lowagie.text.pdf.PdfObject; 64 import com.lowagie.text.pdf.PdfString; 65 import com.lowagie.text.pdf.PdfWriter; 66 import com.lowagie.text.pdf.PdfXConformanceException; 67 import com.lowagie.text.pdf.ShadingColor; 68 import com.lowagie.text.pdf.SpotColor; 69 import com.lowagie.text.pdf.interfaces.PdfXConformance; 70 71 public class PdfXConformanceImp implements PdfXConformance { 72 73 74 public static final int PDFXKEY_COLOR = 1; 75 76 public static final int PDFXKEY_CMYK = 2; 77 78 public static final int PDFXKEY_RGB = 3; 79 80 public static final int PDFXKEY_FONT = 4; 81 82 public static final int PDFXKEY_IMAGE = 5; 83 84 public static final int PDFXKEY_GSTATE = 6; 85 86 public static final int PDFXKEY_LAYER = 7; 87 88 91 protected int pdfxConformance = PdfWriter.PDFXNONE; 92 93 96 public void setPDFXConformance(int pdfxConformance) { 97 this.pdfxConformance = pdfxConformance; 98 } 99 100 103 public int getPDFXConformance() { 104 return pdfxConformance; 105 } 106 107 111 public boolean isPdfX() { 112 return pdfxConformance != PdfWriter.PDFXNONE; 113 } 114 118 public boolean isPdfX1A2001() { 119 return pdfxConformance == PdfWriter.PDFX1A2001; 120 } 121 125 public boolean isPdfX32002() { 126 return pdfxConformance == PdfWriter.PDFX32002; 127 } 128 129 public void completeInfoDictionary(PdfDictionary info) { 130 if (isPdfX()) { 131 if (info.get(PdfName.GTS_PDFXVERSION) == null) { 132 if (isPdfX1A2001()) { 133 info.put(PdfName.GTS_PDFXVERSION, new PdfString("PDF/X-1:2001")); 134 info.put(new PdfName("GTS_PDFXConformance"), new PdfString("PDF/X-1a:2001")); 135 } 136 else if (isPdfX32002()) 137 info.put(PdfName.GTS_PDFXVERSION, new PdfString("PDF/X-3:2002")); 138 } 139 if (info.get(PdfName.TITLE) == null) { 140 info.put(PdfName.TITLE, new PdfString("Pdf document")); 141 } 142 if (info.get(PdfName.CREATOR) == null) { 143 info.put(PdfName.CREATOR, new PdfString("Unknown")); 144 } 145 if (info.get(PdfName.TRAPPED) == null) { 146 info.put(PdfName.TRAPPED, new PdfName("False")); 147 } 148 } 149 } 150 151 public void completeExtraCatalog(PdfDictionary extraCatalog) { 152 if (isPdfX()) { 153 if (extraCatalog.get(PdfName.OUTPUTINTENTS) == null) { 154 PdfDictionary out = new PdfDictionary(PdfName.OUTPUTINTENT); 155 out.put(PdfName.OUTPUTCONDITION, new PdfString("SWOP CGATS TR 001-1995")); 156 out.put(PdfName.OUTPUTCONDITIONIDENTIFIER, new PdfString("CGATS TR 001")); 157 out.put(PdfName.REGISTRYNAME, new PdfString("http://www.color.org")); 158 out.put(PdfName.INFO, new PdfString("")); 159 out.put(PdfName.S, PdfName.GTS_PDFX); 160 extraCatalog.put(PdfName.OUTPUTINTENTS, new PdfArray(out)); 161 } 162 } 163 } 164 165 171 public static void checkPDFXConformance(PdfWriter writer, int key, Object obj1) { 172 if (writer == null || !writer.isPdfX()) 173 return; 174 int conf = writer.getPDFXConformance(); 175 switch (key) { 176 case PDFXKEY_COLOR: 177 switch (conf) { 178 case PdfWriter.PDFX1A2001: 179 if (obj1 instanceof ExtendedColor) { 180 ExtendedColor ec = (ExtendedColor)obj1; 181 switch (ec.getType()) { 182 case ExtendedColor.TYPE_CMYK: 183 case ExtendedColor.TYPE_GRAY: 184 return; 185 case ExtendedColor.TYPE_RGB: 186 throw new PdfXConformanceException("Colorspace RGB is not allowed."); 187 case ExtendedColor.TYPE_SEPARATION: 188 SpotColor sc = (SpotColor)ec; 189 checkPDFXConformance(writer, PDFXKEY_COLOR, sc.getPdfSpotColor().getAlternativeCS()); 190 break; 191 case ExtendedColor.TYPE_SHADING: 192 ShadingColor xc = (ShadingColor)ec; 193 checkPDFXConformance(writer, PDFXKEY_COLOR, xc.getPdfShadingPattern().getShading().getColorSpace()); 194 break; 195 case ExtendedColor.TYPE_PATTERN: 196 PatternColor pc = (PatternColor)ec; 197 checkPDFXConformance(writer, PDFXKEY_COLOR, pc.getPainter().getDefaultColor()); 198 break; 199 } 200 } 201 else if (obj1 instanceof Color ) 202 throw new PdfXConformanceException("Colorspace RGB is not allowed."); 203 break; 204 } 205 break; 206 case PDFXKEY_CMYK: 207 break; 208 case PDFXKEY_RGB: 209 if (conf == PdfWriter.PDFX1A2001) 210 throw new PdfXConformanceException("Colorspace RGB is not allowed."); 211 break; 212 case PDFXKEY_FONT: 213 if (!((BaseFont)obj1).isEmbedded()) 214 throw new PdfXConformanceException("All the fonts must be embedded."); 215 break; 216 case PDFXKEY_IMAGE: 217 PdfImage image = (PdfImage)obj1; 218 if (image.get(PdfName.SMASK) != null) 219 throw new PdfXConformanceException("The /SMask key is not allowed in images."); 220 switch (conf) { 221 case PdfWriter.PDFX1A2001: 222 PdfObject cs = image.get(PdfName.COLORSPACE); 223 if (cs == null) 224 return; 225 if (cs.isName()) { 226 if (PdfName.DEVICERGB.equals(cs)) 227 throw new PdfXConformanceException("Colorspace RGB is not allowed."); 228 } 229 else if (cs.isArray()) { 230 if (PdfName.CALRGB.equals(((PdfArray)cs).getArrayList().get(0))) 231 throw new PdfXConformanceException("Colorspace CalRGB is not allowed."); 232 } 233 break; 234 } 235 break; 236 case PDFXKEY_GSTATE: 237 PdfDictionary gs = (PdfDictionary)obj1; 238 PdfObject obj = gs.get(PdfName.BM); 239 if (obj != null && !PdfGState.BM_NORMAL.equals(obj) && !PdfGState.BM_COMPATIBLE.equals(obj)) 240 throw new PdfXConformanceException("Blend mode " + obj.toString() + " not allowed."); 241 obj = gs.get(PdfName.CA); 242 double v = 0.0; 243 if (obj != null && (v = ((PdfNumber)obj).doubleValue()) != 1.0) 244 throw new PdfXConformanceException("Transparency is not allowed: /CA = " + v); 245 obj = gs.get(PdfName.ca); 246 v = 0.0; 247 if (obj != null && (v = ((PdfNumber)obj).doubleValue()) != 1.0) 248 throw new PdfXConformanceException("Transparency is not allowed: /ca = " + v); 249 break; 250 case PDFXKEY_LAYER: 251 throw new PdfXConformanceException("Layers are not allowed."); 252 } 253 } 254 } 255 | Popular Tags |