1 package org.apache.maven.doxia.module.itext;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import com.lowagie.text.DocumentException;
23 import com.lowagie.text.PageSize;
24 import com.lowagie.text.Rectangle;
25 import com.lowagie.text.xml.XmlToHtml;
26 import com.lowagie.text.xml.XmlToPdf;
27 import com.lowagie.text.xml.XmlToRtf;
28
29 import java.io.InputStream;
30 import java.io.OutputStream;
31 import java.lang.reflect.Field;
32 import java.util.Locale;
33
34
35
36
37
38
39
40 public class ITextUtil
41 {
42
43
44
45
46
47
48
49 public static Rectangle getDefaultPageSize()
50 {
51 String defaultCountry = Locale.getDefault().getCountry();
52 if ( defaultCountry != null
53 && ( defaultCountry.equals( Locale.US.getCountry() )
54 || defaultCountry.equals( Locale.CANADA.getCountry() ) ) )
55 {
56 return PageSize.LETTER;
57 }
58
59 return PageSize.A4;
60 }
61
62
63
64
65
66
67
68
69 public static String getPageSize( Rectangle rect )
70 {
71 Field[] fields = PageSize.class.getFields();
72 for ( int i = 0; i < fields.length; i++ )
73 {
74 Field currentField = fields[i];
75 try
76 {
77 if ( currentField.getType().equals( Rectangle.class ) )
78 {
79 Rectangle fPageSize = (Rectangle) currentField.get( null );
80 if ( ( rect.width() == fPageSize.width() ) && ( rect.height() == fPageSize.height() ) )
81 {
82 return currentField.getName();
83 }
84 }
85 }
86 catch ( Exception e )
87 {
88
89 }
90 }
91
92 return "A4";
93 }
94
95
96
97
98
99
100
101
102 public static boolean isPageSizeSupported( String aPageSize )
103 {
104 Field[] fields = PageSize.class.getFields();
105 for ( int i = 0; i < fields.length; i++ )
106 {
107 Field currentField = fields[i];
108 if ( ( currentField.getName().equalsIgnoreCase( aPageSize ) )
109 && ( currentField.getType().equals( Rectangle.class ) ) )
110 {
111 return true;
112 }
113 }
114
115 return false;
116 }
117
118
119
120
121
122
123
124
125
126 public static void writePdf( InputStream is, OutputStream os )
127 {
128 try
129 {
130 XmlToPdf x = new XmlToPdf();
131
132 x.parse( is, os );
133 }
134 catch ( DocumentException e )
135 {
136 throw new RuntimeException( "DocumentException : " + e.getMessage(), e );
137 }
138 }
139
140
141
142
143
144
145
146
147
148 public static void writeRtf( InputStream is, OutputStream os )
149 {
150 try
151 {
152 XmlToRtf x = new XmlToRtf();
153 x.parse( is, os );
154 }
155 catch ( DocumentException e )
156 {
157 throw new RuntimeException( "DocumentException : " + e.getMessage(), e );
158 }
159 }
160
161
162
163
164
165
166
167
168
169 public static void writeHtml( InputStream is, OutputStream os )
170 {
171 try
172 {
173 XmlToHtml x = new XmlToHtml();
174 x.parse( is, os );
175 }
176 catch ( DocumentException e )
177 {
178 throw new RuntimeException( "DocumentException : " + e.getMessage(), e );
179 }
180 }
181 }