Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package homework;
- import java.awt.Color;
- import java.io.*;
- import com.lowagie.text.*;
- import com.lowagie.text.pdf.*;
- public class GeneratePDF {
- public static void main(String[] args) throws IOException, DocumentException {
- char[] suits = new char[] {
- '\u2660', '\u2665', '\u2666', '\u2663'
- };
- String[] ranks = new String[] {
- "2", "3", "4", "5", "6", "7", "8",
- "9", "10", "J", "Q", "K", "A"
- };
- File file = new File("deck.pdf");
- Document pdf = new Document();
- PdfWriter writer = PdfWriter.getInstance(pdf, new FileOutputStream(file));
- pdf.open();
- PdfContentByte cb = writer.getDirectContent();
- BaseFont baseFont = BaseFont.createFont("DejaVuSansMono.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
- Font font = new Font(baseFont, 16);
- float fontSize = font.getCalculatedSize();
- float offset = fontSize / 2F;
- float width = fontSize * 2F;
- float height = fontSize * 3F;
- float lowerLeftX = offset;
- float topRightY = pdf.getPageSize().getTop() - offset;
- for (String rank : ranks) {
- for (int i = 0; i < suits.length; ++i) {
- font.setColor((i == 0 || i == suits.length - 1) ? Color.BLACK : Color.RED);
- DrawCardInRect(new Rectangle(lowerLeftX, topRightY - height, lowerLeftX + width, topRightY),
- rank, suits[i], cb, font);
- lowerLeftX += width + offset;
- }
- topRightY -= height + offset;
- lowerLeftX = offset;
- }
- pdf.close();
- }
- private static void DrawCardInRect(Rectangle rect, String rank, char suit, PdfContentByte cb, Font font) {
- String text = rank + suit;
- Phrase phrase = new Phrase(text, font);
- ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, phrase,
- (rect.getLeft() + rect.getRight()) / 2,
- rect.getBottom() + rect.getHeight() / 2 - font.getCalculatedSize() / 2,
- 0);
- cb.saveState();
- cb.setColorStroke(Color.BLACK);
- cb.rectangle(rect.getLeft(), rect.getBottom(), rect.getWidth(), rect.getHeight());
- cb.stroke();
- cb.restoreState();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement