View difference between Paste ID: ScyAYd8j and FjEEUYkh
SHOW: | | - or go back to the newest paste.
1
package homework;
2
3
import java.awt.Color;
4
import java.io.*;
5
6
import com.lowagie.text.*;
7
import com.lowagie.text.pdf.*;
8
9
public class GeneratePDF {
10
	public static void main(String[] args) throws IOException, DocumentException {
11
		char[] suits = new char[] {
12
				'\u2660', '\u2665', '\u2666', '\u2663'
13
		};
14
		String[] ranks = new String[] {
15
			"2", "3", "4", "5", "6", "7", "8", 
16
			"9", "10", "J", "Q", "K", "A"
17
		};
18
		
19
		File file = new File("deck.pdf");
20
		Document pdf = new Document();
21
		PdfWriter writer = PdfWriter.getInstance(pdf, new FileOutputStream(file));
22
		pdf.open();
23
		PdfContentByte cb = writer.getDirectContent();
24
		BaseFont baseFont = BaseFont.createFont("DejaVuSansMono.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
25
		Font font = new Font(baseFont, 16);
26
		float fontSize = font.getCalculatedSize();
27
		float offset = fontSize / 2F;
28
		
29
		float width = fontSize * 2F;
30
		float height = fontSize * 3F;
31
		float lowerLeftX = offset;
32
		float topRightY = pdf.getPageSize().getTop() - offset;
33
		
34
		for (String rank : ranks) {
35
			for (int i = 0; i < suits.length; ++i) {
36
				font.setColor((i == 0 || i == suits.length - 1) ? Color.BLACK : Color.RED);
37
				
38
				DrawCardInRect(new Rectangle(lowerLeftX, topRightY - height, lowerLeftX + width, topRightY),
39
						rank, suits[i], cb, font);
40
		        
41-
		        lowerLeftX += width  + offset;
41+
		        	lowerLeftX += width  + offset;
42
			}
43
			topRightY -= height + offset;
44
			lowerLeftX = offset;
45
			
46
		}
47
		
48
		pdf.close();
49
	}
50
	
51
	private static void DrawCardInRect(Rectangle rect, String rank, char suit, PdfContentByte cb, Font font) {
52
		String text = rank + suit;
53
		Phrase phrase = new Phrase(text, font);
54
		ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, phrase,
55
                	(rect.getLeft() + rect.getRight()) / 2,
56
                	rect.getBottom() + rect.getHeight() / 2 - font.getCalculatedSize() / 2,
57
                	0);
58
        	cb.saveState();
59
        	cb.setColorStroke(Color.BLACK);
60
        	cb.rectangle(rect.getLeft(), rect.getBottom(), rect.getWidth(), rect.getHeight());
61
        	cb.stroke();
62
        	cb.restoreState();
63
	}
64
}