Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileOutputStream;
- import com.itextpdf.text.BaseColor;
- import com.itextpdf.text.Chunk;
- import com.itextpdf.text.Document;
- import com.itextpdf.text.Element;
- import com.itextpdf.text.Font;
- import com.itextpdf.text.Image;
- import com.itextpdf.text.Paragraph;
- import com.itextpdf.text.Rectangle;
- import com.itextpdf.text.pdf.BaseFont;
- import com.itextpdf.text.pdf.PdfPCell;
- import com.itextpdf.text.pdf.PdfPTable;
- import com.itextpdf.text.pdf.PdfWriter;
- public class GenerateDeckOfCards {
- private static String FILE = "Deck.pdf";
- public static final String IMG = "Joker.png";
- public static void main(String[] args) {
- try {
- Document doc = new Document();
- PdfWriter.getInstance(doc, new FileOutputStream(FILE));
- doc.open();
- PdfPTable table = new PdfPTable(4);
- table.setWidthPercentage(100);
- table.getDefaultCell().setFixedHeight(160);
- BaseFont baseFont = BaseFont.createFont("arial.ttf", BaseFont.IDENTITY_H, true);
- Font suitBlack = new Font(baseFont, 30f, 0, BaseColor.BLACK);
- Font suitRed = new Font(baseFont, 30f, 0, BaseColor.RED);
- Font cardBlack = new Font(baseFont, 20f, 0, BaseColor.BLACK);
- Font cardRed = new Font(baseFont, 20f, 0, BaseColor.RED);
- String card;
- String suit[] = new String[] {"\u2660", "\u2665","\u2663","\u2666"};
- for (int i = 2; i < 15; i++) {
- switch (i) {
- case 10: card = "10";
- break;
- case 11: card = " J";
- break;
- case 12: card = " Q";
- break;
- case 13: card = " K";
- break;
- case 14: card = " A";
- break;
- default: card = " " + Integer.toString(i);
- break;
- }
- for (int j = 0; j < suit.length; j++) {
- PdfPCell cell = new PdfPCell();
- cell.setPadding(3);
- cell.setBorder(Rectangle.NO_BORDER);
- PdfPTable singleCellTable = new PdfPTable(1);
- PdfPCell singleCell = new PdfPCell();
- if (j == 0 || j == 2) {
- singleCell.addElement(setParameters(cardBlack, card,0));
- singleCell.addElement(setParameters(suitBlack, suit[j],3));
- } else {
- singleCell.addElement(setParameters(cardRed, card,0));
- singleCell.addElement(setParameters(suitRed, suit[j],3));
- }
- Image image2 = Image.getInstance(IMG);
- singleCell.setFixedHeight(140f);
- singleCell.addElement(new Chunk(image2, 20, 20, true));
- singleCellTable.addCell(singleCell);
- cell.addElement(singleCellTable);
- table.addCell(cell);
- }
- }
- doc.add(table);
- doc.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private static Element setParameters(Font font, String card, int index) {
- Paragraph pr = new Paragraph();
- pr.add(card);
- pr.setFont(font);
- pr.setSpacingAfter(5);
- pr.setIndentationLeft(index);
- pr.setAlignment(Element.ALIGN_LEFT);
- return pr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement