@Component public class PDFGenerator { public static final float SEPARATOR_BORDER_WIDTH = 0.1f; public static final float SEPARATOR_HEIGHT = 5F; public void generateFromJavaObject() throws IOException, DocumentException, URISyntaxException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("E:/iTextHelloWorld.pdf")); document.open(); PdfPTable invoiceSection = createFirstSection(); PdfPTable consumerAndSellerSection = createConsumerAndSellerSection(); PdfPTable detailsSection = createDetailsSection(); document.add(invoiceSection); document.add(consumerAndSellerSection); document.add(detailsSection); document.close(); } private PdfPTable createFirstSection() { PdfPTable table = new PdfPTable(2); table.setHorizontalAlignment(Element.ALIGN_RIGHT); table.setWidthPercentage(60); PdfPCell header = new PdfPCell(); header.setHorizontalAlignment(Element.ALIGN_CENTER); Font headerFont = FontFactory.getFont("/fonts/arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 16, Font.BOLD, BaseColor.BLACK); header.setPhrase(new Phrase("Commercial Invoice", headerFont)); header.setColspan(2); table.addCell(header); Font font = FontFactory.getFont("/fonts/arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 10, Font.BOLD, BaseColor.BLACK); table.addCell(createCell("Consignment Number:", font, Element.ALIGN_CENTER)); table.addCell(createCell("text", font, Element.ALIGN_CENTER)); addSeparator(table, 2); table.addCell(createCell("Invoice Number:", font, Element.ALIGN_CENTER)); table.addCell(createCell("text", font, Element.ALIGN_CENTER)); addSeparator(table, 2); table.addCell(createCell("Shipping Date:", font, Element.ALIGN_CENTER)); table.addCell(createCell("text", font, Element.ALIGN_CENTER)); addSeparator(table, 2); table.addCell(createCell("Reason for Exportation: (sale/return/non comercial operarion)", font, Element.ALIGN_CENTER)); table.addCell(createCell("text", font, Element.ALIGN_CENTER)); addSeparator(table, 2); table.addCell(createCell("Invoice Currency:", font, Element.ALIGN_CENTER)); table.addCell(createCell("text", font, Element.ALIGN_CENTER)); addSeparator(table, 2); return table; } private PdfPTable createConsumerAndSellerSection() { PdfPTable consumerAndSellerSection = new PdfPTable(2); consumerAndSellerSection.setWidthPercentage(100); Font headerFont = FontFactory.getFont("/fonts/arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 11, Font.BOLD, BaseColor.BLACK); Font font = FontFactory.getFont("/fonts/arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 11, Font.NORMAL, BaseColor.BLACK); consumerAndSellerSection.addCell(createCell("SELLER (full name and adress)", headerFont, Element.ALIGN_CENTER)); consumerAndSellerSection.addCell(createCell(" CONSUMER (full name and adress)", headerFont, Element.ALIGN_CENTER)); consumerAndSellerSection.addCell(createCell("seller", font, Element.ALIGN_LEFT)); consumerAndSellerSection.addCell(createCell("consumer", font, Element.ALIGN_LEFT)); consumerAndSellerSection.addCell(createCell("seller", font, Element.ALIGN_LEFT)); consumerAndSellerSection.addCell(createCell("consumer", font, Element.ALIGN_LEFT)); consumerAndSellerSection.addCell(createCell("seller", font, Element.ALIGN_LEFT)); consumerAndSellerSection.addCell(createCell("consumer", font, Element.ALIGN_LEFT)); consumerAndSellerSection.addCell(createCell("seller", font, Element.ALIGN_LEFT)); consumerAndSellerSection.addCell(createCell("consumer", font, Element.ALIGN_LEFT)); addSeparator(consumerAndSellerSection, 2); return consumerAndSellerSection; } private PdfPTable createDetailsSection() { int tableSize = 15; PdfPTable detailsSection = new PdfPTable(tableSize); detailsSection.setWidthPercentage(100); Font headerFont = FontFactory.getFont("/fonts/arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 10, Font.BOLD, BaseColor.BLACK); Font font = FontFactory.getFont("/fonts/arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 10, Font.NORMAL, BaseColor.BLACK); detailsSection.addCell(createCell("Description", headerFont, Element.ALIGN_CENTER, 6)); detailsSection.addCell(createCell("Quantity", headerFont, Element.ALIGN_CENTER)); detailsSection.addCell(createCell("Weight per unit", headerFont, Element.ALIGN_CENTER)); detailsSection.addCell(createCell("Total Weight (kg)", headerFont, Element.ALIGN_CENTER)); detailsSection.addCell(createCell("UNSPSC Code", headerFont, Element.ALIGN_CENTER, 2)); detailsSection.addCell(createCell("Country of origin", headerFont, Element.ALIGN_CENTER, 2)); detailsSection.addCell(createCell("Unit price", headerFont, Element.ALIGN_CENTER)); detailsSection.addCell(createCell("Total Value", headerFont, Element.ALIGN_CENTER)); detailsSection.addCell(createCell("text", font, Element.ALIGN_CENTER, 6)); detailsSection.addCell(createCell("text", font, Element.ALIGN_CENTER)); detailsSection.addCell(createCell("text", font, Element.ALIGN_CENTER)); detailsSection.addCell(createCell("text", font, Element.ALIGN_CENTER)); detailsSection.addCell(createCell("text", font, Element.ALIGN_CENTER, 2)); detailsSection.addCell(createCell("text", font, Element.ALIGN_CENTER, 2)); detailsSection.addCell(createCell("text", font, Element.ALIGN_CENTER)); detailsSection.addCell(createCell("text", font, Element.ALIGN_CENTER)); addSeparator(detailsSection, tableSize); detailsSection.addCell(createCell("Number of places:", headerFont, Element.ALIGN_CENTER, 3)); detailsSection.addCell(createCell("text", headerFont, Element.ALIGN_CENTER, 3)); detailsSection.addCell(createCellSeparator(9)); addSeparator(detailsSection, tableSize); detailsSection.addCell(createCell("Tipe of packing:", headerFont, Element.ALIGN_CENTER, 3)); detailsSection.addCell(createCell("text", headerFont, Element.ALIGN_CENTER, 3)); detailsSection.addCell(createCellSeparator(9)); addSeparator(detailsSection, tableSize); detailsSection.addCell(createCell("Gross weight:", headerFont, Element.ALIGN_CENTER, 3)); detailsSection.addCell(createCell("text", headerFont, Element.ALIGN_CENTER, 4)); detailsSection.addCell(createCell("кг/kg", headerFont, Element.ALIGN_CENTER)); detailsSection.addCell(createCell("Delivery cost", headerFont, Element.ALIGN_CENTER, 4)); detailsSection.addCell(createCell("", headerFont, Element.ALIGN_CENTER, 4)); detailsSection.addCell(createCellSeparator(8)); detailsSection.addCell(createCell("Insurance", headerFont, Element.ALIGN_CENTER, 4)); detailsSection.addCell(createCell("", headerFont, Element.ALIGN_CENTER, 4)); detailsSection.addCell(createCellSeparator(8)); detailsSection.addCell(createCell("Other expenses", headerFont, Element.ALIGN_CENTER, 4)); detailsSection.addCell(createCell("", headerFont, Element.ALIGN_CENTER, 4)); detailsSection.addCell(createCellSeparator(8)); detailsSection.addCell(createCell("Total cost", headerFont, Element.ALIGN_CENTER, 4)); detailsSection.addCell(createCell("", headerFont, Element.ALIGN_CENTER, 4)); addSeparator(detailsSection, tableSize); detailsSection.addCell(createCell("Delivery terms INCOTERMS:", headerFont, Element.ALIGN_CENTER, 3)); detailsSection.addCell(createCell("", headerFont, Element.ALIGN_CENTER, 3)); detailsSection.addCell(createCellSeparator(9)); addSeparator(detailsSection, tableSize); detailsSection.addCell(createCell("I'm confirming that information in invoice is exact and correct", headerFont, Element.ALIGN_LEFT, tableSize)); addSeparator(detailsSection, tableSize); detailsSection.addCell(createCell("Sender", headerFont, Element.ALIGN_LEFT, 10)); detailsSection.addCell(createCell("Sender signature", headerFont, Element.ALIGN_LEFT, 3)); detailsSection.addCell(createCell("Date", headerFont, Element.ALIGN_LEFT, 2)); detailsSection.addCell(createCell("text", font, Element.ALIGN_LEFT, 10)); detailsSection.addCell(createCell("text", font, Element.ALIGN_LEFT, 3)); detailsSection.addCell(createCell("text", font, Element.ALIGN_LEFT, 2)); return detailsSection; } private PdfPCell createCell(String text, Font font, int alignment) { PdfPCell cell = new PdfPCell(); cell.setHorizontalAlignment(alignment); Phrase phrase = new Phrase(text, font); cell.setPhrase(phrase); return cell; } private PdfPCell createCell(String text, Font font, int alignment, int colspan) { PdfPCell cell = new PdfPCell(); cell.setHorizontalAlignment(alignment); cell.setColspan(colspan); Phrase phrase = new Phrase(text, font); cell.setPhrase(phrase); return cell; } private PdfPCell createCellSeparator(int colspan) { PdfPCell cell = new PdfPCell(); cell.setColspan(colspan); cell.setBorderWidthTop(0); cell.setBorderWidthBottom(0); return cell; } private void addSeparator(PdfPTable table, int colspan) { PdfPCell cell = createCellSeparator(colspan); cell.setFixedHeight(SEPARATOR_HEIGHT); cell.setBorderWidthLeft(SEPARATOR_BORDER_WIDTH); cell.setBorderWidthRight(SEPARATOR_BORDER_WIDTH); table.addCell(cell); }