OSDN Git Service

chenge localization use resourcebundle
[posterdivider/PosterDivider.git] / src / jp / sourceforge / posterdivider / PdfGenerator.java
1 //
2 // PdfGenetator.java
3 // This file is part of PosterDivider.
4 //
5 package jp.sourceforge.posterdivider;
6
7 import java.io.OutputStream;
8
9 import com.itextpdf.text.Document;
10 import com.itextpdf.text.Image;
11 import com.itextpdf.text.Rectangle;
12 import com.itextpdf.text.pdf.PdfImportedPage;
13 import com.itextpdf.text.pdf.PdfReader;
14 import com.itextpdf.text.pdf.PdfWriter;
15
16 public class PdfGenerator {
17         public static class CreatePosterArgs {
18                 private float srcLeft = -1;
19                 private float srcBottom = -1;
20                 private float srcRight = -1;
21                 private float srcTop = -1;
22                 private float posterWidth = -1;
23                 private float posterHeight = -1;
24                 private float paperWidth = -1;
25                 private float paperHeight = -1;
26                 private float marginLeft = -1;
27                 private float marginTop = -1;
28                 private float marginRight = -1;
29                 private float marginBottom = -1;
30
31                 public float getSrcLeft() {
32                         return srcLeft;
33                 }
34
35                 public void setSrcLeft(float srcLeft) {
36                         this.srcLeft = srcLeft;
37                 }
38
39                 public float getSrcBottom() {
40                         return srcBottom;
41                 }
42
43                 public void setSrcBottom(float srcBottom) {
44                         this.srcBottom = srcBottom;
45                 }
46
47                 public float getSrcRight() {
48                         return srcRight;
49                 }
50
51                 public void setSrcRight(float srcRight) {
52                         this.srcRight = srcRight;
53                 }
54
55                 public float getSrcTop() {
56                         return srcTop;
57                 }
58
59                 public void setSrcTop(float srcTop) {
60                         this.srcTop = srcTop;
61                 }
62
63                 public float getSrcWidth() {
64                         return srcRight - srcLeft;
65                 }
66
67                 public float getSrcHeight() {
68                         return srcTop - srcBottom;
69                 }
70
71                 public float getPosterWidth() {
72                         return posterWidth;
73                 }
74
75                 public void setPosterWidth(float posterWidth) {
76                         this.posterWidth = posterWidth;
77                 }
78
79                 public float getPosterHeight() {
80                         return posterHeight;
81                 }
82
83                 public void setPosterHeight(float posterHeight) {
84                         this.posterHeight = posterHeight;
85                 }
86
87                 public float getPaperWidth() {
88                         return paperWidth;
89                 }
90
91                 public void setPaperWidth(float paperWidth) {
92                         this.paperWidth = paperWidth;
93                 }
94
95                 public float getPaperHeight() {
96                         return paperHeight;
97                 }
98
99                 public void setPaperHeight(float paperHeight) {
100                         this.paperHeight = paperHeight;
101                 }
102
103                 public float getMarginLeft() {
104                         return marginLeft;
105                 }
106
107                 public void setMarginLeft(float marginLeft) {
108                         this.marginLeft = marginLeft;
109                 }
110
111                 public float getMarginTop() {
112                         return marginTop;
113                 }
114
115                 public void setMarginTop(float marginTop) {
116                         this.marginTop = marginTop;
117                 }
118
119                 public float getMarginRight() {
120                         return marginRight;
121                 }
122
123                 public void setMarginRight(float marginRight) {
124                         this.marginRight = marginRight;
125                 }
126
127                 public float getMarginBottom() {
128                         return marginBottom;
129                 }
130
131                 public void setMarginBottom(float marginBottom) {
132                         this.marginBottom = marginBottom;
133                 }
134
135                 public void setSrcRect(float left, float bottom, float right, float top) {
136                         this.srcLeft = left;
137                         this.srcBottom = bottom;
138                         this.srcRight = right;
139                         this.srcTop = top;
140                 }
141
142                 public void setSrcRect(Rectangle rect) {
143                         setSrcRect(rect.getLeft(), rect.getBottom(), rect.getRight(), rect
144                                         .getTop());
145                 }
146
147                 public void setPosterSize(float width, float height) {
148                         this.posterWidth = width;
149                         this.posterHeight = height;
150                 }
151
152                 public void setPosterSize(Rectangle rect) {
153                         setPosterSize(rect.getWidth(), rect.getHeight());
154                 }
155
156                 public void setPaperSize(float width, float height) {
157                         this.paperWidth = width;
158                         this.paperHeight = height;
159                 }
160
161                 public void setPaperSize(Rectangle rect) {
162                         setPaperSize(rect.getWidth(), rect.getHeight());
163                 }
164
165                 public void setMargin(float left, float top, float right, float bottom) {
166                         this.marginLeft = left;
167                         this.marginTop = top;
168                         this.marginRight = right;
169                         this.marginBottom = bottom;
170                 }
171         }
172
173         private static int calcPaperCount(float posterLength, float paperLength) {
174                 if (posterLength < 0 || paperLength <= 0) {
175                         return -1;
176                 }
177                 float count = posterLength / paperLength;
178                 return (int) Math.ceil(count);
179         }
180
181         public static int simulateOutputPaperCount(CreatePosterArgs args) {
182                 int paperCountX = simulateOutputPaperCountX(args);
183                 int paperCountY = simulateOutputPaperCountY(args);
184
185                 if (paperCountX < 0 || paperCountY < 0) {
186                         return -1;
187                 }
188
189                 return paperCountX * paperCountY;
190         }
191
192         public static int simulateOutputPaperCountX(CreatePosterArgs args) {
193                 float posterWidth = args.getPosterWidth();
194                 float paperWidth = args.getPaperWidth();
195                 float marginLeft = args.getMarginLeft();
196                 float marginRight = args.getMarginRight();
197
198                 if (posterWidth < 0) {
199                         return -1;
200                 }
201                 if (paperWidth < 0) {
202                         return -1;
203                 }
204                 if (marginLeft < 0 || marginRight < 0) {
205                         return -1;
206                 }
207
208                 float paperTextWidth = paperWidth - marginLeft - marginRight;
209                 int paperCountX = calcPaperCount(posterWidth, paperTextWidth);
210
211                 if (paperCountX < 0) {
212                         return -1;
213                 }
214
215                 return paperCountX;
216         }
217
218         public static int simulateOutputPaperCountY(CreatePosterArgs args) {
219                 float posterHeight = args.getPosterHeight();
220                 float paperHeight = args.getPaperHeight();
221                 float marginTop = args.getMarginTop();
222                 float marginBottom = args.getMarginBottom();
223
224                 if (posterHeight < 0) {
225                         return -1;
226                 }
227                 if (paperHeight < 0) {
228                         return -1;
229                 }
230                 if (marginTop < 0 || marginBottom < 0) {
231                         return -1;
232                 }
233
234                 float paperTextHeight = paperHeight - marginTop - marginBottom;
235                 int paperCountY = calcPaperCount(posterHeight, paperTextHeight);
236
237                 if (paperCountY < 0) {
238                         return -1;
239                 }
240
241                 return paperCountY;
242         }
243
244         public static void createPosterFromPdf(PdfReader reader, int pageNum,
245                         CreatePosterArgs args, OutputStream os) {
246                 float srcX = args.getSrcLeft();
247                 float srcY = args.getSrcBottom();
248                 float srcWidth = args.getSrcWidth();
249                 float srcHeight = args.getSrcHeight();
250                 float posterWidth = args.getPosterWidth();
251                 float posterHeight = args.getPosterHeight();
252                 float paperWidth = args.getPaperWidth();
253                 float paperHeight = args.getPaperHeight();
254                 float marginLeft = args.getMarginLeft();
255                 float marginTop = args.getMarginTop();
256                 float marginRight = args.getMarginRight();
257                 float marginBottom = args.getMarginBottom();
258
259                 if (reader == null) {
260                         throw new NullPointerException();
261                 }
262                 if (pageNum <= 0 || pageNum > reader.getNumberOfPages()) {
263                         throw new IllegalArgumentException();
264                 }
265                 if (os == null) {
266                         throw new NullPointerException();
267                 }
268                 if (srcWidth <= 0 || srcHeight <= 0) {
269                         throw new IllegalArgumentException();
270                 }
271                 if (posterWidth < 0 || posterHeight < 0) {
272                         throw new IllegalArgumentException();
273                 }
274                 if (paperWidth < 0 || paperHeight < 0) {
275                         throw new IllegalArgumentException();
276                 }
277                 if (marginLeft < 0 || marginTop < 0 || marginRight < 0
278                                 || marginBottom < 0) {
279                         throw new IllegalArgumentException();
280                 }
281
282                 Document document = new Document(
283                                 new Rectangle(paperWidth, paperHeight), marginLeft,
284                                 marginRight, marginTop, marginBottom);
285
286                 // 念のため生成された方の値に合わせる
287                 paperWidth = document.getPageSize().getWidth();
288                 paperHeight = document.getPageSize().getHeight();
289                 marginLeft = document.leftMargin();
290                 marginTop = document.topMargin();
291                 marginRight = document.rightMargin();
292                 marginBottom = document.bottomMargin();
293
294                 float paperTextWidth = paperWidth - marginLeft - marginRight;
295                 float paperTextHeight = paperHeight - marginTop - marginBottom;
296                 int paperCountX = calcPaperCount(posterWidth, paperTextWidth);
297                 int paperCountY = calcPaperCount(posterHeight, paperTextHeight);
298
299                 if (paperCountX < 0 || paperCountY < 0) {
300                         throw new IllegalArgumentException();
301                 }
302
303                 PdfWriter writer;
304                 try {
305                         writer = PdfWriter.getInstance(document, os);
306                 } catch (Exception ex) {
307                         throw new RuntimeException();
308                 }
309
310                 document.open();
311                 PdfImportedPage page = writer.getImportedPage(reader, pageNum);
312
313                 float rateX = posterWidth / srcWidth;
314                 float rateY = posterHeight / srcHeight;
315                 float x0 = marginLeft - srcX * rateX;
316                 float y0 = paperHeight - marginTop - posterHeight - srcY * rateY;
317                 float y = y0;
318                 for (int i = 0; i < paperCountY; i++) {
319                         float x = x0;
320                         for (int j = 0; j < paperCountX; j++) {
321                                 document.newPage();
322                                 writer.getDirectContent().addTemplate(page, rateX, 0, 0, rateY,
323                                                 x, y);
324                                 x -= paperTextWidth;
325                         }
326                         y += paperTextHeight;
327                 }
328
329                 document.close();
330                 writer.close();
331         }
332
333         public static void createPosterFromImage(Image image,
334                         CreatePosterArgs args, OutputStream os) {
335                 float srcX = args.getSrcLeft();
336                 float srcY = args.getSrcBottom();
337                 float srcWidth = args.getSrcWidth();
338                 float srcHeight = args.getSrcHeight();
339                 float posterWidth = args.getPosterWidth();
340                 float posterHeight = args.getPosterHeight();
341                 float paperWidth = args.getPaperWidth();
342                 float paperHeight = args.getPaperHeight();
343                 float marginLeft = args.getMarginLeft();
344                 float marginTop = args.getMarginTop();
345                 float marginRight = args.getMarginRight();
346                 float marginBottom = args.getMarginBottom();
347
348                 if (image == null) {
349                         throw new NullPointerException();
350                 }
351                 if (os == null) {
352                         throw new NullPointerException();
353                 }
354                 if (srcWidth <= 0 || srcHeight <= 0) {
355                         throw new IllegalArgumentException();
356                 }
357                 if (posterWidth < 0 || posterHeight < 0) {
358                         throw new IllegalArgumentException();
359                 }
360                 if (paperWidth < 0 || paperHeight < 0) {
361                         throw new IllegalArgumentException();
362                 }
363                 if (marginLeft < 0 || marginTop < 0 || marginRight < 0
364                                 || marginBottom < 0) {
365                         throw new IllegalArgumentException();
366                 }
367
368                 Document document = new Document(
369                                 new Rectangle(paperWidth, paperHeight), marginLeft,
370                                 marginRight, marginTop, marginBottom);
371
372                 paperWidth = document.getPageSize().getWidth();
373                 paperHeight = document.getPageSize().getHeight();
374                 marginLeft = document.leftMargin();
375                 marginTop = document.topMargin();
376                 marginRight = document.rightMargin();
377                 marginBottom = document.bottomMargin();
378
379                 float paperTextWidth = paperWidth - marginLeft - marginRight;
380                 float paperTextHeight = paperHeight - marginTop - marginBottom;
381                 int paperCountX = calcPaperCount(posterWidth, paperTextWidth);
382                 int paperCountY = calcPaperCount(posterHeight, paperTextHeight);
383
384                 if (paperCountX < 0 || paperCountY < 0) {
385                         throw new IllegalArgumentException();
386                 }
387
388                 PdfWriter writer;
389                 try {
390                         writer = PdfWriter.getInstance(document, os);
391                 } catch (Exception ex) {
392                         throw new RuntimeException();
393                 }
394
395                 document.open();
396
397                 float rateXP = posterWidth / srcWidth;
398                 float rateYP = posterHeight / srcHeight;
399                 // Imageは倍率なしだと1x1ptとして扱われるのでこうする
400                 float rateX = rateXP * image.getWidth();
401                 float rateY = rateYP * image.getHeight();
402                 float x0 = marginLeft - srcX * rateXP;
403                 float y0 = paperHeight - marginTop - posterHeight - srcY * rateYP;
404                 float y = y0;
405                 for (int i = 0; i < paperCountY; i++) {
406                         float x = x0;
407                         for (int j = 0; j < paperCountX; j++) {
408                                 document.newPage();
409                                 try {
410                                         writer.getDirectContent().addImage(image, rateX, 0, 0,
411                                                         rateY, x, y);
412                                 } catch (Exception ex) {
413                                         throw new RuntimeException();
414                                 }
415                                 x -= paperTextWidth;
416                         }
417                         y += paperTextHeight;
418                 }
419
420                 document.close();
421                 writer.close();
422         }
423
424         public static void createPoster(ImageContainer ic, CreatePosterArgs args,
425                         OutputStream os) {
426                 switch (ic.getFileType()) {
427                 case Lib.FT_PDF:
428                         createPosterFromPdf(ic.getPdf(), ic.getPageNum(), args, os);
429                         return;
430                 case Lib.FT_BMP:
431                 case Lib.FT_GIF:
432                 case Lib.FT_JBIG2:
433                 case Lib.FT_JPEG:
434                 case Lib.FT_JPEG2000:
435                 case Lib.FT_PNG:
436                 case Lib.FT_TIFF:
437                 case Lib.FT_WMF:
438                 case Lib.FT_OTHERIMAGE:
439                         createPosterFromImage(ic.getImage(), args, os);
440                         return;
441                 default:
442                         return;
443                 }
444         }
445 }