KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: PdfXConformanceImp.java 2772 2007-05-21 14:06:08Z blowagie $
3  *
4  * Copyright 2006 Bruno Lowagie (based on code by Paulo Soares)
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.awt.Color JavaDoc;
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     /** A key for an aspect that can be checked for PDF/X Conformance. */
74     public static final int PDFXKEY_COLOR = 1;
75     /** A key for an aspect that can be checked for PDF/X Conformance. */
76     public static final int PDFXKEY_CMYK = 2;
77     /** A key for an aspect that can be checked for PDF/X Conformance. */
78     public static final int PDFXKEY_RGB = 3;
79     /** A key for an aspect that can be checked for PDF/X Conformance. */
80     public static final int PDFXKEY_FONT = 4;
81     /** A key for an aspect that can be checked for PDF/X Conformance. */
82     public static final int PDFXKEY_IMAGE = 5;
83     /** A key for an aspect that can be checked for PDF/X Conformance. */
84     public static final int PDFXKEY_GSTATE = 6;
85     /** A key for an aspect that can be checked for PDF/X Conformance. */
86     public static final int PDFXKEY_LAYER = 7;
87     
88     /**
89      * The value indicating if the PDF has to be in conformance with PDF/X.
90      */

91     protected int pdfxConformance = PdfWriter.PDFXNONE;
92     
93     /**
94      * @see com.lowagie.text.pdf.interfaces.PdfXConformance#setPDFXConformance(int)
95      */

96     public void setPDFXConformance(int pdfxConformance) {
97         this.pdfxConformance = pdfxConformance;
98     }
99
100     /**
101      * @see com.lowagie.text.pdf.interfaces.PdfXConformance#getPDFXConformance()
102      */

103     public int getPDFXConformance() {
104         return pdfxConformance;
105     }
106     
107     /**
108      * Checks if the PDF/X Conformance is necessary.
109      * @return true if the PDF has to be in conformance with any of the PDF/X specifications
110      */

111     public boolean isPdfX() {
112         return pdfxConformance != PdfWriter.PDFXNONE;
113     }
114     /**
115      * Checks if the PDF has to be in conformance with PDF/X-1a:2001
116      * @return true of the PDF has to be in conformance with PDF/X-1a:2001
117      */

118     public boolean isPdfX1A2001() {
119         return pdfxConformance == PdfWriter.PDFX1A2001;
120     }
121     /**
122      * Checks if the PDF has to be in conformance with PDF/X-3:2002
123      * @return true of the PDF has to be in conformance with PDF/X-3:2002
124      */

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     /**
166      * Business logic that checks if a certain object is in conformance with PDF/X.
167      * @param writer the writer that is supposed to write the PDF/X file
168      * @param key the type of PDF/X conformance that has to be checked
169      * @param obj1 the object that is checked for conformance
170      */

171     public static void checkPDFXConformance(PdfWriter writer, int key, Object JavaDoc 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 JavaDoc)
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