Advertisement
Guest User

Untitled

a guest
May 7th, 2014
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. import java.io.FileOutputStream;
  2. import java.io.IOException;
  3. import com.itextpdf.text.BaseColor;
  4. import com.itextpdf.text.Document;
  5. import com.itextpdf.text.DocumentException;
  6. import com.itextpdf.text.Font;
  7. import com.itextpdf.text.pdf.BaseFont;
  8. import com.itextpdf.text.pdf.PdfPTable;
  9. import com.itextpdf.text.Font.FontFamily;
  10. import com.itextpdf.text.Paragraph;
  11. import com.itextpdf.text.pdf.PdfWriter;
  12.  
  13. public class _09_GeneratePDFbyExternalLibrary{      
  14.         public static String RESULT = "F:/Cards.pdf";      
  15.         public static Font NORMAL = new Font(FontFamily.SYMBOL, 12);      
  16.         public static void main(String[] args) throws DocumentException, IOException {
  17.             createPdf(RESULT);
  18.         }      
  19.         public static void createPdf(String filename) throws DocumentException, IOException {      
  20.                 try{
  21.                         Document document = new Document();
  22.                         PdfWriter.getInstance(document, new FileOutputStream(filename));
  23.                         document.open();                                                            
  24.                         BaseFont baseFont = BaseFont.createFont("C:/Windows/Fonts/Arial.ttf", BaseFont.IDENTITY_H, true);
  25.                         Font black = new Font(baseFont, 20f, 0, BaseColor.BLACK);
  26.                         Font red = new Font(baseFont, 20f, 0, BaseColor.RED);
  27.                         Paragraph p = new Paragraph(" ");          
  28.                         String[] suits = {"\u2663", "\u2666", "\u2665", "\u2660"};
  29.                         String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "D", "K", "A"};
  30.                         for (int i = 0; i<52; i++){
  31.                             Font color;
  32.                             int suit = i/13;
  33.                             int rank = i%13;
  34.                             if (suit==0 || suit ==3)  {color = black;}
  35.                             else  {color = red;}
  36.                             PdfPTable table = new PdfPTable(1);
  37.                             table.setWidthPercentage(10);
  38.                             table.getDefaultCell().setFixedHeight(70);
  39.                             table.addCell(new Paragraph(suits[suit] +" "+ ranks[rank] + " ", color));
  40.                             document.add(table);
  41.                             document.add(p);                            
  42.                         }                        
  43.                         document.close();
  44.                 }
  45.                 catch (Exception e){
  46.                         e.printStackTrace();
  47.                 }
  48.         }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement