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 public class ITextUtil
40 {
41
42
43
44
45
46
47
48 public static Rectangle getDefaultPageSize()
49 {
50 String defaultCountry = Locale.getDefault().getCountry();
51 if ( defaultCountry != null
52 && ( defaultCountry.equals( Locale.US.getCountry() )
53 || defaultCountry.equals( Locale.CANADA.getCountry() ) ) )
54 {
55 return PageSize.LETTER;
56 }
57
58 return PageSize.A4;
59 }
60
61
62
63
64
65
66
67
68 public static String getPageSize( Rectangle rect )
69 {
70 Field[] fields = PageSize.class.getFields();
71 for ( Field currentField : fields )
72 {
73 try
74 {
75 if ( currentField.getType().equals( Rectangle.class ) )
76 {
77 Rectangle fPageSize = (Rectangle) currentField.get( null );
78 if ( ( rect.width() == fPageSize.width() ) && ( rect.height() == fPageSize.height() ) )
79 {
80 return currentField.getName();
81 }
82 }
83 }
84 catch ( Exception e )
85 {
86
87 }
88 }
89
90 return "A4";
91 }
92
93
94
95
96
97
98
99
100 public static boolean isPageSizeSupported( String aPageSize )
101 {
102 Field[] fields = PageSize.class.getFields();
103 for ( Field currentField : fields )
104 {
105 if ( ( currentField.getName().equalsIgnoreCase( aPageSize ) ) && ( currentField.getType().equals(
106 Rectangle.class ) ) )
107 {
108 return true;
109 }
110 }
111
112 return false;
113 }
114
115
116
117
118
119
120
121
122
123 public static void writePdf( InputStream is, OutputStream os )
124 {
125 try
126 {
127 XmlToPdf x = new XmlToPdf();
128
129 x.parse( is, os );
130 }
131 catch ( DocumentException e )
132 {
133 throw new RuntimeException( "DocumentException : " + e.getMessage(), e );
134 }
135 }
136
137
138
139
140
141
142
143
144
145 public static void writeRtf( InputStream is, OutputStream os )
146 {
147 try
148 {
149 XmlToRtf x = new XmlToRtf();
150 x.parse( is, os );
151 }
152 catch ( DocumentException e )
153 {
154 throw new RuntimeException( "DocumentException : " + e.getMessage(), e );
155 }
156 }
157
158
159
160
161
162
163
164
165
166 public static void writeHtml( InputStream is, OutputStream os )
167 {
168 try
169 {
170 XmlToHtml x = new XmlToHtml();
171 x.parse( is, os );
172 }
173 catch ( DocumentException e )
174 {
175 throw new RuntimeException( "DocumentException : " + e.getMessage(), e );
176 }
177 }
178 }