KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > PdfAppearance


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

47
48 package com.lowagie.text.pdf;
49 import java.util.HashMap JavaDoc;
50
51 import com.lowagie.text.Rectangle;
52
53 /**
54  * Implements the appearance stream to be used with form fields..
55  */

56
57 public class PdfAppearance extends PdfTemplate {
58     
59     public static final HashMap JavaDoc stdFieldFontNames = new HashMap JavaDoc();
60     static {
61         stdFieldFontNames.put("Courier-BoldOblique", new PdfName("CoBO"));
62         stdFieldFontNames.put("Courier-Bold", new PdfName("CoBo"));
63         stdFieldFontNames.put("Courier-Oblique", new PdfName("CoOb"));
64         stdFieldFontNames.put("Courier", new PdfName("Cour"));
65         stdFieldFontNames.put("Helvetica-BoldOblique", new PdfName("HeBO"));
66         stdFieldFontNames.put("Helvetica-Bold", new PdfName("HeBo"));
67         stdFieldFontNames.put("Helvetica-Oblique", new PdfName("HeOb"));
68         stdFieldFontNames.put("Helvetica", PdfName.HELV);
69         stdFieldFontNames.put("Symbol", new PdfName("Symb"));
70         stdFieldFontNames.put("Times-BoldItalic", new PdfName("TiBI"));
71         stdFieldFontNames.put("Times-Bold", new PdfName("TiBo"));
72         stdFieldFontNames.put("Times-Italic", new PdfName("TiIt"));
73         stdFieldFontNames.put("Times-Roman", new PdfName("TiRo"));
74         stdFieldFontNames.put("ZapfDingbats", PdfName.ZADB);
75         stdFieldFontNames.put("HYSMyeongJo-Medium", new PdfName("HySm"));
76         stdFieldFontNames.put("HYGoThic-Medium", new PdfName("HyGo"));
77         stdFieldFontNames.put("HeiseiKakuGo-W5", new PdfName("KaGo"));
78         stdFieldFontNames.put("HeiseiMin-W3", new PdfName("KaMi"));
79         stdFieldFontNames.put("MHei-Medium", new PdfName("MHei"));
80         stdFieldFontNames.put("MSung-Light", new PdfName("MSun"));
81         stdFieldFontNames.put("STSong-Light", new PdfName("STSo"));
82         stdFieldFontNames.put("MSungStd-Light", new PdfName("MSun"));
83         stdFieldFontNames.put("STSongStd-Light", new PdfName("STSo"));
84         stdFieldFontNames.put("HYSMyeongJoStd-Medium", new PdfName("HySm"));
85         stdFieldFontNames.put("KozMinPro-Regular", new PdfName("KaMi"));
86     }
87     
88     /**
89      *Creates a <CODE>PdfAppearance</CODE>.
90      */

91     
92     PdfAppearance() {
93         super();
94         separator = ' ';
95     }
96     
97     PdfAppearance(PdfIndirectReference iref) {
98         thisReference = iref;
99     }
100     
101     /**
102      * Creates new PdfTemplate
103      *
104      * @param wr the <CODE>PdfWriter</CODE>
105      */

106     
107     PdfAppearance(PdfWriter wr) {
108         super(wr);
109         separator = ' ';
110     }
111     
112     /**
113      * Creates a new appearance to be used with form fields.
114      *
115      * @param writer the PdfWriter to use
116      * @param width the bounding box width
117      * @param height the bounding box height
118      * @return the appearance created
119      */

120     public static PdfAppearance createAppearance(PdfWriter writer, float width, float height) {
121         return createAppearance(writer, width, height, null);
122     }
123     
124     static PdfAppearance createAppearance(PdfWriter writer, float width, float height, PdfName forcedName) {
125         PdfAppearance template = new PdfAppearance(writer);
126         template.setWidth(width);
127         template.setHeight(height);
128         writer.addDirectTemplateSimple(template, forcedName);
129         return template;
130     }
131
132     /**
133      * Set the font and the size for the subsequent text writing.
134      *
135      * @param bf the font
136      * @param size the font size in points
137      */

138     public void setFontAndSize(BaseFont bf, float size) {
139         checkWriter();
140         state.size = size;
141         if (bf.getFontType() == BaseFont.FONT_TYPE_DOCUMENT) {
142             state.fontDetails = new FontDetails(null, ((DocumentFont)bf).getIndirectReference(), bf);
143         }
144         else
145             state.fontDetails = writer.addSimple(bf);
146         PdfName psn = (PdfName)stdFieldFontNames.get(bf.getPostscriptFontName());
147         if (psn == null) {
148             if (bf.isSubset() && bf.getFontType() == BaseFont.FONT_TYPE_TTUNI)
149                 psn = state.fontDetails.getFontName();
150             else {
151                 psn = new PdfName(bf.getPostscriptFontName());
152                 state.fontDetails.setSubset(false);
153             }
154         }
155         PageResources prs = getPageResources();
156 // PdfName name = state.fontDetails.getFontName();
157
prs.addFont(psn, state.fontDetails.getIndirectReference());
158         content.append(psn.getBytes()).append(' ').append(size).append(" Tf").append_i(separator);
159     }
160
161     public PdfContentByte getDuplicate() {
162         PdfAppearance tpl = new PdfAppearance();
163         tpl.writer = writer;
164         tpl.pdf = pdf;
165         tpl.thisReference = thisReference;
166         tpl.pageResources = pageResources;
167         tpl.bBox = new Rectangle(bBox);
168         tpl.group = group;
169         tpl.layer = layer;
170         if (matrix != null) {
171             tpl.matrix = new PdfArray(matrix);
172         }
173         tpl.separator = separator;
174         return tpl;
175     }
176 }
Popular Tags