Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileOutputStream;
- import java.util.Date;
- import com.itextpdf.text.Anchor;
- import com.itextpdf.text.BadElementException;
- import com.itextpdf.text.BaseColor;
- import com.itextpdf.text.Chapter;
- import com.itextpdf.text.Chunk;
- import com.itextpdf.text.Document;
- import com.itextpdf.text.DocumentException;
- import com.itextpdf.text.Element;
- import com.itextpdf.text.ElementListener;
- import com.itextpdf.text.Font;
- import com.itextpdf.text.List;
- import com.itextpdf.text.ListItem;
- import com.itextpdf.text.Paragraph;
- import com.itextpdf.text.Phrase;
- import com.itextpdf.text.Rectangle;
- import com.itextpdf.text.Section;
- import com.itextpdf.text.pdf.ColumnText;
- import com.itextpdf.text.pdf.PdfContentByte;
- import com.itextpdf.text.pdf.PdfGState;
- import com.itextpdf.text.pdf.PdfPCell;
- import com.itextpdf.text.pdf.PdfPTable;
- import com.itextpdf.text.pdf.PdfWriter;
- import com.itextpdf.text.pdf.parser.GraphicsState;
- import com.itextpdf.text.pdf.PdfContentByte.GraphicState;
- public class P9_CardsPDF {
- private static String FILE = "Cards.pdf";
- private static Font symbolFontBlack = new Font(Font.FontFamily.SYMBOL, 36, Font.BOLD);
- private static Font symbolFontRed = new Font(Font.FontFamily.SYMBOL, 36, Font.BOLD, BaseColor.RED);
- private static String[] cards = new String[]{"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
- //Symbol key from Symgol.amf font
- //167 - спатия
- //168 - каро
- //169 - купа
- //170 - пика
- private static int[] cardType = new int[] {167,168,169,170};
- public static void main(String[] args) {
- try {
- Document document = new Document();
- PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FILE));
- document.open();
- addTitlePage(document, writer);
- document.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private static void addMetaData(Document document) {
- document.addTitle("Cards Deck task");
- document.addSubject("List of all cards");
- document.addKeywords("Cards, PDF");
- }
- private static void addTitlePage(Document document, PdfWriter writer)
- throws DocumentException {
- Paragraph preface = new Paragraph();
- //set the highest Y of card in the page
- float y = 650;
- for (int i = 0; i < cards.length; i++) {
- //set start X for the first card of the row
- float x = 25;
- for (int j = 0; j < cardType.length; j++) {
- Chunk c = new Chunk();
- //choose the color of the card
- if (cardType[j]==168 || cardType[j]==169) {
- c = new Chunk(cards[i] + "" + (char) cardType[j], symbolFontRed);
- } else {
- c = new Chunk(cards[i] + "" + (char) cardType[j], symbolFontBlack);
- }
- drawRect(writer, c, x, y, x+100, y+150);
- //set X position for the next card
- x=x+150;
- }
- //set X position for the other card
- y = y-200;
- //add new line
- addEmptyLine(preface, 1);
- //if there are 4 rows move to the next page
- if (i!=0 && i%4==0) {
- document.newPage();
- y = 650;
- }
- }
- }
- //draw card
- private static void drawRect(PdfWriter writer, Chunk c, float x, float y, float width, float height) {
- PdfContentByte cb = writer.getDirectContent();
- Rectangle rect = new Rectangle(x, y, width, height);
- Phrase phrase = new Phrase(c);
- ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, phrase,
- // center horizontally
- (rect.getLeft() + rect.getRight()) / 2,
- // center vertically
- (rect.getBottom() + rect.getTop())/2,
- 0);
- //Draw rectangle
- cb.saveState();
- cb.rectangle(rect.getLeft(), rect.getBottom(), rect.getWidth(), rect.getHeight());
- cb.stroke();
- cb.restoreState();
- }
- //add empty line
- private static void addEmptyLine(Paragraph paragraph, int number) {
- for (int i = 0; i < number; i++) {
- paragraph.add(new Paragraph(" "));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment