KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > ImgWMF


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

50
51 package com.lowagie.text;
52
53 import java.io.IOException JavaDoc;
54 import java.io.InputStream JavaDoc;
55 import java.net.MalformedURLException JavaDoc;
56 import java.net.URL JavaDoc;
57
58 import com.lowagie.text.pdf.PdfTemplate;
59 import com.lowagie.text.pdf.codec.wmf.InputMeta;
60 import com.lowagie.text.pdf.codec.wmf.MetaDo;
61
62 /**
63  * An <CODE>ImgWMF</CODE> is the representation of a windows metafile
64  * that has to be inserted into the document
65  *
66  * @see Element
67  * @see Image
68  */

69
70 public class ImgWMF extends Image {
71     
72     // Constructors
73

74     ImgWMF(Image image) {
75         super(image);
76     }
77     
78     /**
79      * Constructs an <CODE>ImgWMF</CODE>-object, using an <VAR>url</VAR>.
80      *
81      * @param url the <CODE>URL</CODE> where the image can be found
82      * @throws BadElementException on error
83      * @throws IOException on error
84      */

85     
86     public ImgWMF(URL JavaDoc url) throws BadElementException, IOException JavaDoc {
87         super(url);
88         processParameters();
89     }
90     
91     /**
92      * Constructs an <CODE>ImgWMF</CODE>-object, using a <VAR>filename</VAR>.
93      *
94      * @param filename a <CODE>String</CODE>-representation of the file that contains the image.
95      * @throws BadElementException on error
96      * @throws MalformedURLException on error
97      * @throws IOException on error
98      */

99     
100     public ImgWMF(String JavaDoc filename) throws BadElementException, MalformedURLException JavaDoc, IOException JavaDoc {
101         this(Utilities.toURL(filename));
102     }
103     
104     /**
105      * Constructs an <CODE>ImgWMF</CODE>-object from memory.
106      *
107      * @param img the memory image
108      * @throws BadElementException on error
109      * @throws IOException on error
110      */

111     
112     public ImgWMF(byte[] img) throws BadElementException, IOException JavaDoc {
113         super((URL JavaDoc)null);
114         rawData = img;
115         originalData = img;
116         processParameters();
117     }
118     
119 /**
120  * This method checks if the image is a valid WMF and processes some parameters.
121  * @throws BadElementException
122  * @throws IOException
123  */

124     
125     private void processParameters() throws BadElementException, IOException JavaDoc {
126         type = IMGTEMPLATE;
127         originalType = ORIGINAL_WMF;
128         InputStream JavaDoc is = null;
129         try {
130             String JavaDoc errorID;
131             if (rawData == null){
132                 is = url.openStream();
133                 errorID = url.toString();
134             }
135             else{
136                 is = new java.io.ByteArrayInputStream JavaDoc(rawData);
137                 errorID = "Byte array";
138             }
139             InputMeta in = new InputMeta(is);
140             if (in.readInt() != 0x9AC6CDD7) {
141                 throw new BadElementException(errorID + " is not a valid placeable windows metafile.");
142             }
143             in.readWord();
144             int left = in.readShort();
145             int top = in.readShort();
146             int right = in.readShort();
147             int bottom = in.readShort();
148             int inch = in.readWord();
149             dpiX = 72;
150             dpiY = 72;
151             scaledHeight = (float)(bottom - top) / inch * 72f;
152             setTop(scaledHeight);
153             scaledWidth = (float)(right - left) / inch * 72f;
154             setRight(scaledWidth);
155         }
156         finally {
157             if (is != null) {
158                 is.close();
159             }
160             plainWidth = getWidth();
161             plainHeight = getHeight();
162         }
163     }
164     
165     /** Reads the WMF into a template.
166      * @param template the template to read to
167      * @throws IOException on error
168      * @throws DocumentException on error
169      */

170     public void readWMF(PdfTemplate template) throws IOException JavaDoc, DocumentException {
171         setTemplateData(template);
172         template.setWidth(getWidth());
173         template.setHeight(getHeight());
174         InputStream JavaDoc is = null;
175         try {
176             if (rawData == null){
177                 is = url.openStream();
178             }
179             else{
180                 is = new java.io.ByteArrayInputStream JavaDoc(rawData);
181             }
182             MetaDo meta = new MetaDo(is, template);
183             meta.readAll();
184         }
185         finally {
186             if (is != null) {
187                 is.close();
188             }
189         }
190     }
191 }
192
Popular Tags