Facebook
From Daniel, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 51
  1. @Component
  2. public class PDFGenerator {
  3.  
  4.     public static final float SEPARATOR_BORDER_WIDTH = 0.1f;
  5.     public static final float SEPARATOR_HEIGHT = 5F;
  6.  
  7.     public void generateFromJavaObject() throws IOException, DocumentException, URISyntaxException {
  8.         Document document = new Document();
  9.         PdfWriter.getInstance(document, new FileOutputStream("E:/iTextHelloWorld.pdf"));
  10.         document.open();
  11.  
  12.         PdfPTable invoiceSection = createFirstSection();
  13.         PdfPTable consumerAndSellerSection = createConsumerAndSellerSection();
  14.         PdfPTable detailsSection = createDetailsSection();
  15.  
  16.  
  17.         document.add(invoiceSection);
  18.         document.add(consumerAndSellerSection);
  19.         document.add(detailsSection);
  20.         document.close();
  21.     }
  22.  
  23.     private PdfPTable createFirstSection() {
  24.         PdfPTable table = new PdfPTable(2);
  25.         table.setHorizontalAlignment(Element.ALIGN_RIGHT);
  26.         table.setWidthPercentage(60);
  27.  
  28.         PdfPCell header = new PdfPCell();
  29.         header.setHorizontalAlignment(Element.ALIGN_CENTER);
  30.         Font headerFont = FontFactory.getFont("/fonts/arial.ttf",
  31.                 BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 16, Font.BOLD, BaseColor.BLACK);
  32.         header.setPhrase(new Phrase("Commercial Invoice", headerFont));
  33.         header.setColspan(2);
  34.         table.addCell(header);
  35.  
  36.         Font font = FontFactory.getFont("/fonts/arial.ttf",
  37.                 BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 10, Font.BOLD, BaseColor.BLACK);
  38.  
  39.         table.addCell(createCell("Consignment Number:", font, Element.ALIGN_CENTER));
  40.         table.addCell(createCell("text", font, Element.ALIGN_CENTER));
  41.         addSeparator(table, 2);
  42.  
  43.         table.addCell(createCell("Invoice Number:", font, Element.ALIGN_CENTER));
  44.         table.addCell(createCell("text", font, Element.ALIGN_CENTER));
  45.         addSeparator(table, 2);
  46.  
  47.         table.addCell(createCell("Shipping Date:", font, Element.ALIGN_CENTER));
  48.         table.addCell(createCell("text", font, Element.ALIGN_CENTER));
  49.         addSeparator(table, 2);
  50.  
  51.         table.addCell(createCell("Reason for Exportation: (sale/return/non comercial operarion)", font, Element.ALIGN_CENTER));
  52.         table.addCell(createCell("text", font, Element.ALIGN_CENTER));
  53.         addSeparator(table, 2);
  54.  
  55.         table.addCell(createCell("Invoice Currency:", font, Element.ALIGN_CENTER));
  56.         table.addCell(createCell("text", font, Element.ALIGN_CENTER));
  57.         addSeparator(table, 2);
  58.         return table;
  59.     }
  60.  
  61.     private PdfPTable createConsumerAndSellerSection() {
  62.         PdfPTable consumerAndSellerSection = new PdfPTable(2);
  63.         consumerAndSellerSection.setWidthPercentage(100);
  64.  
  65.         Font headerFont = FontFactory.getFont("/fonts/arial.ttf",
  66.                 BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 11, Font.BOLD, BaseColor.BLACK);
  67.  
  68.         Font font = FontFactory.getFont("/fonts/arial.ttf",
  69.                 BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 11, Font.NORMAL, BaseColor.BLACK);
  70.  
  71.         consumerAndSellerSection.addCell(createCell("SELLER (full name and adress)", headerFont, Element.ALIGN_CENTER));
  72.         consumerAndSellerSection.addCell(createCell(" CONSUMER (full name and adress)", headerFont, Element.ALIGN_CENTER));
  73.  
  74.         consumerAndSellerSection.addCell(createCell("seller", font, Element.ALIGN_LEFT));
  75.         consumerAndSellerSection.addCell(createCell("consumer", font, Element.ALIGN_LEFT));
  76.  
  77.         consumerAndSellerSection.addCell(createCell("seller", font, Element.ALIGN_LEFT));
  78.         consumerAndSellerSection.addCell(createCell("consumer", font, Element.ALIGN_LEFT));
  79.  
  80.         consumerAndSellerSection.addCell(createCell("seller", font, Element.ALIGN_LEFT));
  81.         consumerAndSellerSection.addCell(createCell("consumer", font, Element.ALIGN_LEFT));
  82.  
  83.         consumerAndSellerSection.addCell(createCell("seller", font, Element.ALIGN_LEFT));
  84.         consumerAndSellerSection.addCell(createCell("consumer", font, Element.ALIGN_LEFT));
  85.         addSeparator(consumerAndSellerSection, 2);
  86.         return consumerAndSellerSection;
  87.     }
  88.  
  89.  
  90.     private PdfPTable createDetailsSection() {
  91.         int tableSize = 15;
  92.         PdfPTable detailsSection = new PdfPTable(tableSize);
  93.         detailsSection.setWidthPercentage(100);
  94.  
  95.         Font headerFont = FontFactory.getFont("/fonts/arial.ttf",
  96.                 BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 10, Font.BOLD, BaseColor.BLACK);
  97.  
  98.         Font font = FontFactory.getFont("/fonts/arial.ttf",
  99.                 BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 10, Font.NORMAL, BaseColor.BLACK);
  100.  
  101.  
  102.         detailsSection.addCell(createCell("Description", headerFont, Element.ALIGN_CENTER, 6));
  103.         detailsSection.addCell(createCell("Quantity", headerFont, Element.ALIGN_CENTER));
  104.         detailsSection.addCell(createCell("Weight per unit", headerFont, Element.ALIGN_CENTER));
  105.         detailsSection.addCell(createCell("Total Weight (kg)", headerFont, Element.ALIGN_CENTER));
  106.         detailsSection.addCell(createCell("UNSPSC Code", headerFont, Element.ALIGN_CENTER, 2));
  107.         detailsSection.addCell(createCell("Country of origin", headerFont, Element.ALIGN_CENTER, 2));
  108.         detailsSection.addCell(createCell("Unit price", headerFont, Element.ALIGN_CENTER));
  109.         detailsSection.addCell(createCell("Total Value", headerFont, Element.ALIGN_CENTER));
  110.  
  111.         detailsSection.addCell(createCell("text", font, Element.ALIGN_CENTER, 6));
  112.         detailsSection.addCell(createCell("text", font, Element.ALIGN_CENTER));
  113.         detailsSection.addCell(createCell("text", font, Element.ALIGN_CENTER));
  114.         detailsSection.addCell(createCell("text", font, Element.ALIGN_CENTER));
  115.         detailsSection.addCell(createCell("text", font, Element.ALIGN_CENTER, 2));
  116.         detailsSection.addCell(createCell("text", font, Element.ALIGN_CENTER, 2));
  117.         detailsSection.addCell(createCell("text", font, Element.ALIGN_CENTER));
  118.         detailsSection.addCell(createCell("text", font, Element.ALIGN_CENTER));
  119.  
  120.         addSeparator(detailsSection, tableSize);
  121.         detailsSection.addCell(createCell("Number of places:", headerFont, Element.ALIGN_CENTER, 3));
  122.         detailsSection.addCell(createCell("text", headerFont, Element.ALIGN_CENTER, 3));
  123.         detailsSection.addCell(createCellSeparator(9));
  124.  
  125.         addSeparator(detailsSection, tableSize);
  126.         detailsSection.addCell(createCell("Tipe of packing:", headerFont, Element.ALIGN_CENTER, 3));
  127.         detailsSection.addCell(createCell("text", headerFont, Element.ALIGN_CENTER, 3));
  128.         detailsSection.addCell(createCellSeparator(9));
  129.  
  130.         addSeparator(detailsSection, tableSize);
  131.         detailsSection.addCell(createCell("Gross weight:", headerFont, Element.ALIGN_CENTER, 3));
  132.         detailsSection.addCell(createCell("text", headerFont, Element.ALIGN_CENTER, 4));
  133.         detailsSection.addCell(createCell("кг/kg", headerFont, Element.ALIGN_CENTER));
  134.         detailsSection.addCell(createCell("Delivery cost", headerFont, Element.ALIGN_CENTER, 4));
  135.         detailsSection.addCell(createCell("", headerFont, Element.ALIGN_CENTER, 4));
  136.  
  137.         detailsSection.addCell(createCellSeparator(8));
  138.         detailsSection.addCell(createCell("Insurance", headerFont, Element.ALIGN_CENTER, 4));
  139.         detailsSection.addCell(createCell("", headerFont, Element.ALIGN_CENTER, 4));
  140.  
  141.         detailsSection.addCell(createCellSeparator(8));
  142.         detailsSection.addCell(createCell("Other expenses", headerFont, Element.ALIGN_CENTER, 4));
  143.         detailsSection.addCell(createCell("", headerFont, Element.ALIGN_CENTER, 4));
  144.  
  145.         detailsSection.addCell(createCellSeparator(8));
  146.         detailsSection.addCell(createCell("Total cost", headerFont, Element.ALIGN_CENTER, 4));
  147.         detailsSection.addCell(createCell("", headerFont, Element.ALIGN_CENTER, 4));
  148.  
  149.         addSeparator(detailsSection, tableSize);
  150.         detailsSection.addCell(createCell("Delivery terms INCOTERMS:", headerFont, Element.ALIGN_CENTER, 3));
  151.         detailsSection.addCell(createCell("", headerFont, Element.ALIGN_CENTER, 3));
  152.         detailsSection.addCell(createCellSeparator(9));
  153.  
  154.         addSeparator(detailsSection, tableSize);
  155.         detailsSection.addCell(createCell("I'm confirming that information in invoice is exact and correct", headerFont, Element.ALIGN_LEFT, tableSize));
  156.         addSeparator(detailsSection, tableSize);
  157.  
  158.         detailsSection.addCell(createCell("Sender", headerFont, Element.ALIGN_LEFT, 10));
  159.         detailsSection.addCell(createCell("Sender signature", headerFont, Element.ALIGN_LEFT, 3));
  160.         detailsSection.addCell(createCell("Date", headerFont, Element.ALIGN_LEFT, 2));
  161.  
  162.         detailsSection.addCell(createCell("text", font, Element.ALIGN_LEFT, 10));
  163.         detailsSection.addCell(createCell("text", font, Element.ALIGN_LEFT, 3));
  164.         detailsSection.addCell(createCell("text", font, Element.ALIGN_LEFT, 2));
  165.  
  166.         return detailsSection;
  167.     }
  168.  
  169.     private PdfPCell createCell(String text, Font font, int alignment) {
  170.         PdfPCell cell = new PdfPCell();
  171.         cell.setHorizontalAlignment(alignment);
  172.         Phrase phrase = new Phrase(text, font);
  173.         cell.setPhrase(phrase);
  174.         return cell;
  175.     }
  176.  
  177.     private PdfPCell createCell(String text, Font font, int alignment, int colspan) {
  178.         PdfPCell cell = new PdfPCell();
  179.         cell.setHorizontalAlignment(alignment);
  180.         cell.setColspan(colspan);
  181.         Phrase phrase = new Phrase(text, font);
  182.         cell.setPhrase(phrase);
  183.         return cell;
  184.     }
  185.  
  186.     private PdfPCell createCellSeparator(int colspan) {
  187.         PdfPCell cell = new PdfPCell();
  188.         cell.setColspan(colspan);
  189.         cell.setBorderWidthTop(0);
  190.         cell.setBorderWidthBottom(0);
  191.         return cell;
  192.     }
  193.  
  194.     private void addSeparator(PdfPTable table, int colspan) {
  195.         PdfPCell cell = createCellSeparator(colspan);
  196.         cell.setFixedHeight(SEPARATOR_HEIGHT);
  197.         cell.setBorderWidthLeft(SEPARATOR_BORDER_WIDTH);
  198.         cell.setBorderWidthRight(SEPARATOR_BORDER_WIDTH);
  199.         table.addCell(cell);
  200.     }