Guest User

CardsPDF

a guest
May 15th, 2014
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.93 KB | None | 0 0
  1. import java.io.FileOutputStream;
  2. import java.util.Date;
  3.  
  4. import com.itextpdf.text.Anchor;
  5. import com.itextpdf.text.BadElementException;
  6. import com.itextpdf.text.BaseColor;
  7. import com.itextpdf.text.Chapter;
  8. import com.itextpdf.text.Chunk;
  9. import com.itextpdf.text.Document;
  10. import com.itextpdf.text.DocumentException;
  11. import com.itextpdf.text.Element;
  12. import com.itextpdf.text.ElementListener;
  13. import com.itextpdf.text.Font;
  14. import com.itextpdf.text.List;
  15. import com.itextpdf.text.ListItem;
  16. import com.itextpdf.text.Paragraph;
  17. import com.itextpdf.text.Phrase;
  18. import com.itextpdf.text.Rectangle;
  19. import com.itextpdf.text.Section;
  20. import com.itextpdf.text.pdf.ColumnText;
  21. import com.itextpdf.text.pdf.PdfContentByte;
  22. import com.itextpdf.text.pdf.PdfGState;
  23. import com.itextpdf.text.pdf.PdfPCell;
  24. import com.itextpdf.text.pdf.PdfPTable;
  25. import com.itextpdf.text.pdf.PdfWriter;
  26. import com.itextpdf.text.pdf.parser.GraphicsState;
  27. import com.itextpdf.text.pdf.PdfContentByte.GraphicState;
  28.  
  29. public class P9_CardsPDF {
  30.        
  31.     private static String FILE = "Cards.pdf";
  32.      
  33.     private static Font symbolFontBlack = new Font(Font.FontFamily.SYMBOL, 36, Font.BOLD);
  34.     private static Font symbolFontRed = new Font(Font.FontFamily.SYMBOL, 36, Font.BOLD, BaseColor.RED);
  35.      
  36.     private static String[] cards = new String[]{"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
  37.     //Symbol key from Symgol.amf font
  38.     //167 - спатия
  39.     //168 - каро
  40.     //169 - купа
  41.     //170 - пика
  42.     private static int[] cardType = new int[] {167,168,169,170};
  43.      
  44.     public static void main(String[] args) {
  45.         try {
  46.           Document document = new Document();
  47.           PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FILE));
  48.           document.open();
  49.           addTitlePage(document, writer);    
  50.           document.close();
  51.         } catch (Exception e) {
  52.           e.printStackTrace();
  53.         }
  54.     }
  55.  
  56.     private static void addMetaData(Document document) {
  57.         document.addTitle("Cards Deck task");
  58.         document.addSubject("List of all cards");
  59.         document.addKeywords("Cards, PDF");
  60.     }
  61.  
  62.     private static void addTitlePage(Document document, PdfWriter writer)
  63.         throws DocumentException {
  64.         Paragraph preface = new Paragraph();
  65.         //set the highest Y of card in the page
  66.         float y = 650;
  67.         for (int i = 0; i < cards.length; i++) {
  68.             //set start X for the first card of the row
  69.             float x = 25;
  70.             for (int j = 0; j < cardType.length; j++) {
  71.                 Chunk c = new Chunk();
  72.                 //choose the color of the card
  73.                 if (cardType[j]==168 || cardType[j]==169) {
  74.                     c = new Chunk(cards[i] + "" + (char) cardType[j], symbolFontRed);                  
  75.                 } else {
  76.                     c = new Chunk(cards[i] + "" + (char) cardType[j], symbolFontBlack);
  77.                 }
  78.                 drawRect(writer, c, x, y, x+100, y+150);
  79.                 //set X position for the next card
  80.                 x=x+150;
  81.             }
  82.             //set X position for the other card
  83.             y = y-200;
  84.             //add new line
  85.             addEmptyLine(preface, 1);
  86.             //if there are 4 rows move to the next page
  87.             if (i!=0 && i%4==0) {
  88.                 document.newPage();
  89.                 y = 650;
  90.             }
  91.         }      
  92.     }
  93.      
  94.     //draw card
  95.     private static void drawRect(PdfWriter writer, Chunk c, float x, float y, float width, float height) {
  96.         PdfContentByte cb = writer.getDirectContent();                 
  97.         Rectangle rect = new Rectangle(x, y, width, height);
  98.         Phrase phrase = new Phrase(c);
  99.         ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, phrase,                       
  100.                                 // center horizontally
  101.                                 (rect.getLeft() + rect.getRight()) / 2,
  102.                                 // center vertically
  103.                                 (rect.getBottom() + rect.getTop())/2,
  104.                                 0);
  105.         //Draw rectangle
  106.         cb.saveState();
  107.         cb.rectangle(rect.getLeft(), rect.getBottom(), rect.getWidth(), rect.getHeight());
  108.         cb.stroke();
  109.         cb.restoreState();
  110.     }
  111.      
  112.     //add empty line
  113.     private static void addEmptyLine(Paragraph paragraph, int number) {
  114.         for (int i = 0; i < number; i++) {
  115.             paragraph.add(new Paragraph(" "));
  116.         }
  117.     }
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment