3 // This file is part of PosterDivider.
5 package jp.sourceforge.posterdivider;
7 import java.io.OutputStream;
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;
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;
31 public float getSrcLeft() {
35 public void setSrcLeft(float srcLeft) {
36 this.srcLeft = srcLeft;
39 public float getSrcBottom() {
43 public void setSrcBottom(float srcBottom) {
44 this.srcBottom = srcBottom;
47 public float getSrcRight() {
51 public void setSrcRight(float srcRight) {
52 this.srcRight = srcRight;
55 public float getSrcTop() {
59 public void setSrcTop(float srcTop) {
63 public float getSrcWidth() {
64 return srcRight - srcLeft;
67 public float getSrcHeight() {
68 return srcTop - srcBottom;
71 public float getPosterWidth() {
75 public void setPosterWidth(float posterWidth) {
76 this.posterWidth = posterWidth;
79 public float getPosterHeight() {
83 public void setPosterHeight(float posterHeight) {
84 this.posterHeight = posterHeight;
87 public float getPaperWidth() {
91 public void setPaperWidth(float paperWidth) {
92 this.paperWidth = paperWidth;
95 public float getPaperHeight() {
99 public void setPaperHeight(float paperHeight) {
100 this.paperHeight = paperHeight;
103 public float getMarginLeft() {
107 public void setMarginLeft(float marginLeft) {
108 this.marginLeft = marginLeft;
111 public float getMarginTop() {
115 public void setMarginTop(float marginTop) {
116 this.marginTop = marginTop;
119 public float getMarginRight() {
123 public void setMarginRight(float marginRight) {
124 this.marginRight = marginRight;
127 public float getMarginBottom() {
131 public void setMarginBottom(float marginBottom) {
132 this.marginBottom = marginBottom;
135 public void setSrcRect(float left, float bottom, float right, float top) {
137 this.srcBottom = bottom;
138 this.srcRight = right;
142 public void setSrcRect(Rectangle rect) {
143 setSrcRect(rect.getLeft(), rect.getBottom(), rect.getRight(), rect
147 public void setPosterSize(float width, float height) {
148 this.posterWidth = width;
149 this.posterHeight = height;
152 public void setPosterSize(Rectangle rect) {
153 setPosterSize(rect.getWidth(), rect.getHeight());
156 public void setPaperSize(float width, float height) {
157 this.paperWidth = width;
158 this.paperHeight = height;
161 public void setPaperSize(Rectangle rect) {
162 setPaperSize(rect.getWidth(), rect.getHeight());
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;
173 private static int calcPaperCount(float posterLength, float paperLength) {
174 if (posterLength < 0 || paperLength <= 0) {
177 float count = posterLength / paperLength;
178 return (int) Math.ceil(count);
181 public static int simulateOutputPaperCount(CreatePosterArgs args) {
182 int paperCountX = simulateOutputPaperCountX(args);
183 int paperCountY = simulateOutputPaperCountY(args);
185 if (paperCountX < 0 || paperCountY < 0) {
189 return paperCountX * paperCountY;
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();
198 if (posterWidth < 0) {
201 if (paperWidth < 0) {
204 if (marginLeft < 0 || marginRight < 0) {
208 float paperTextWidth = paperWidth - marginLeft - marginRight;
209 int paperCountX = calcPaperCount(posterWidth, paperTextWidth);
211 if (paperCountX < 0) {
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();
224 if (posterHeight < 0) {
227 if (paperHeight < 0) {
230 if (marginTop < 0 || marginBottom < 0) {
234 float paperTextHeight = paperHeight - marginTop - marginBottom;
235 int paperCountY = calcPaperCount(posterHeight, paperTextHeight);
237 if (paperCountY < 0) {
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();
259 if (reader == null) {
260 throw new NullPointerException();
262 if (pageNum <= 0 || pageNum > reader.getNumberOfPages()) {
263 throw new IllegalArgumentException();
266 throw new NullPointerException();
268 if (srcWidth <= 0 || srcHeight <= 0) {
269 throw new IllegalArgumentException();
271 if (posterWidth < 0 || posterHeight < 0) {
272 throw new IllegalArgumentException();
274 if (paperWidth < 0 || paperHeight < 0) {
275 throw new IllegalArgumentException();
277 if (marginLeft < 0 || marginTop < 0 || marginRight < 0
278 || marginBottom < 0) {
279 throw new IllegalArgumentException();
282 Document document = new Document(
283 new Rectangle(paperWidth, paperHeight), marginLeft,
284 marginRight, marginTop, marginBottom);
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();
294 float paperTextWidth = paperWidth - marginLeft - marginRight;
295 float paperTextHeight = paperHeight - marginTop - marginBottom;
296 int paperCountX = calcPaperCount(posterWidth, paperTextWidth);
297 int paperCountY = calcPaperCount(posterHeight, paperTextHeight);
299 if (paperCountX < 0 || paperCountY < 0) {
300 throw new IllegalArgumentException();
305 writer = PdfWriter.getInstance(document, os);
306 } catch (Exception ex) {
307 throw new RuntimeException();
311 PdfImportedPage page = writer.getImportedPage(reader, pageNum);
313 float rateX = posterWidth / srcWidth;
314 float rateY = posterHeight / srcHeight;
315 float x0 = marginLeft - srcX * rateX;
316 float y0 = paperHeight - marginTop - posterHeight - srcY * rateY;
318 for (int i = 0; i < paperCountY; i++) {
320 for (int j = 0; j < paperCountX; j++) {
322 writer.getDirectContent().addTemplate(page, rateX, 0, 0, rateY,
326 y += paperTextHeight;
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();
349 throw new NullPointerException();
352 throw new NullPointerException();
354 if (srcWidth <= 0 || srcHeight <= 0) {
355 throw new IllegalArgumentException();
357 if (posterWidth < 0 || posterHeight < 0) {
358 throw new IllegalArgumentException();
360 if (paperWidth < 0 || paperHeight < 0) {
361 throw new IllegalArgumentException();
363 if (marginLeft < 0 || marginTop < 0 || marginRight < 0
364 || marginBottom < 0) {
365 throw new IllegalArgumentException();
368 Document document = new Document(
369 new Rectangle(paperWidth, paperHeight), marginLeft,
370 marginRight, marginTop, marginBottom);
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();
379 float paperTextWidth = paperWidth - marginLeft - marginRight;
380 float paperTextHeight = paperHeight - marginTop - marginBottom;
381 int paperCountX = calcPaperCount(posterWidth, paperTextWidth);
382 int paperCountY = calcPaperCount(posterHeight, paperTextHeight);
384 if (paperCountX < 0 || paperCountY < 0) {
385 throw new IllegalArgumentException();
390 writer = PdfWriter.getInstance(document, os);
391 } catch (Exception ex) {
392 throw new RuntimeException();
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;
405 for (int i = 0; i < paperCountY; i++) {
407 for (int j = 0; j < paperCountX; j++) {
410 writer.getDirectContent().addImage(image, rateX, 0, 0,
412 } catch (Exception ex) {
413 throw new RuntimeException();
417 y += paperTextHeight;
424 public static void createPoster(ImageContainer ic, CreatePosterArgs args,
426 switch (ic.getFileType()) {
428 createPosterFromPdf(ic.getPdf(), ic.getPageNum(), args, os);
434 case Lib.FT_JPEG2000:
438 case Lib.FT_OTHERIMAGE:
439 createPosterFromImage(ic.getImage(), args, os);