Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pdf; import dto.Product; import dto.Supply; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Date; import javax.imageio.ImageIO; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.font.PDType1Font; import org.apache.pdfbox.rendering.PDFRenderer; /** * * @author Bojan */ public class WorkOrderPDF { //private static File template = new File("C:\\Users\\Bojan\\Desktop\\ISProject\\GitHub\\ISProject\\FirmaApp\\pdf_docs\\work_orders\\work_order_template.pdf"); private static File template = new File("pdf_docs\\templates\\work_order_template.pdf"); private String numOfWorkOrder; private Date dateOfWorkOrder; private ArrayList<Product> products = new ArrayList<>(); private ArrayList<Supply> supplies = new ArrayList<>(); private static PDPageContentStream contentStream; private double sumProducts; private double sumSupplies; public WorkOrderPDF() { } public WorkOrderPDF(String numOfWorkOrder, Date dateOfWorkOrder, ArrayList<Product> products, ArrayList<Supply> supplies) { this.numOfWorkOrder = numOfWorkOrder; this.dateOfWorkOrder = dateOfWorkOrder; this.products = products; this.supplies = supplies; System.out.println(this.toString()); } public static void main(String[] args) throws IOException { //DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); //System.out.println(dtf.format(java.time.LocalDateTime.now()));; File file = new File("pdf_docs\\work_orders\\work_order_template.pdf"); PDDocument doc = PDDocument.load(file); PDFRenderer renderer = new PDFRenderer(doc); BufferedImage image = renderer.renderImage(0); ImageIO.write(image, "PNG", new File("pdf_docs\\work_orders_picture\\" + "wopic.png")); doc.close(); } public void createWorkOrderPNG(String path) throws IOException { File file = new File(path); PDDocument doc = PDDocument.load(file); PDFRenderer renderer = new PDFRenderer(doc); renderer.renderImageWithDPI(0, 600); BufferedImage image = renderer.renderImage(0); SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy."); ImageIO.write(image, "PNG", new File("pdf_docs\\work_orders_picture\\" + "wopic-" + numOfWorkOrder.split("\\/")[0] + "_" + numOfWorkOrder.split("/")[1] + ".pdf")); doc.close(); } @Override public String toString() { String str = ""; str = "NUMBER WORK ORDER --> " + numOfWorkOrder; str += "\n DATE WORK ORDER --> " + dateOfWorkOrder; for (Product p : products) { str += "\n\n PRODUCT: " + p; } for (Supply s : supplies) { str += "\n\n SUPPLY: " + s; } return str; } public void insertProducts() throws IOException { int row = 0; int column = 0; int offsetRow = 16; int offsetColumn = 353; for (Product pr : products) { fillTables(pr.getSelector(), 67 + (column * offsetColumn), 439 - (row * offsetRow), contentStream); fillTables(pr.getMeasureUnit(), 188 + (column * offsetColumn), 439 - (row * offsetRow), contentStream); fillTables(Double.toString(pr.getTempQuantity()), 238 + (column * offsetColumn), 439 - (row * offsetRow), contentStream); fillTables(Double.toString(pr.getTempPrice()), 290 + (column * offsetColumn), 439 - (row * offsetRow), contentStream); fillTables(Double.toString(pr.getTempQuantity() * pr.getTempPrice()), 345 + (column * offsetColumn), 439 - (row * offsetRow), contentStream); contentStream.close(); sumProducts += pr.getTempQuantity() * pr.getTempPrice(); if (row == 4) { column = 1; } row = ++row % 5; } } public void insertSupplies() throws IOException { int row = 0; int column = 0; int offsetRow = 16; int offsetColumn = 353; int delay = 2; DecimalFormat df = new DecimalFormat("####0.00"); for (Supply s : supplies) { if (row < 5) { fillTables(s.getName(), 67 + (column * offsetColumn), 287 - (row * offsetRow), contentStream); fillTables(s.getMeasureUnit(), 188 + (column * offsetColumn), 287 - (row * offsetRow), contentStream); fillTables(df.format(s.getTempQuantity()), 238 + (column * offsetColumn), 287 - (row * offsetRow), contentStream); fillTables(Double.toString(s.getPrice()), 290 + (column * offsetColumn), 287 - (row * offsetRow), contentStream); fillTables(df.format(s.getTempQuantity() * s.getPrice()), 345 + (column * offsetColumn), 287 - (row * offsetRow), contentStream); contentStream.close(); } else { fillTables(s.getName(), 67 + (column * offsetColumn), 287 - (row * offsetRow) - delay, contentStream); fillTables(s.getMeasureUnit(), 188 + (column * offsetColumn), 287 - (row * offsetRow) - delay, contentStream); fillTables(df.format(s.getTempQuantity()), 238 + (column * offsetColumn), 287 - (row * offsetRow) - delay, contentStream); fillTables(Double.toString(s.getPrice()), 290 + (column * offsetColumn), 287 - (row * offsetRow) - delay, contentStream); fillTables(df.format(s.getTempQuantity() * s.getPrice()), 345 + (column * offsetColumn), 287 - (row * offsetRow) - delay, contentStream); contentStream.close(); delay += 2; } sumSupplies += s.getTempQuantity() * s.getPrice(); if (row == 14) { column = 1; delay = 2; } row = ++row % 15; } } public void workOrderInfo() throws IOException { fillInfo(numOfWorkOrder, 745, 552, contentStream); SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy."); fillInfo(sdf.format(dateOfWorkOrder), 100, 520, contentStream); DecimalFormat df = new DecimalFormat("####0.00"); fillInfo(df.format(sumProducts), 750, 372, contentStream); fillInfo(df.format(sumSupplies), 750, 40, contentStream); contentStream.close(); } public void createWorkOrder() throws IOException { PDDocument document = PDDocument.load(template); //prva strana PDPage page = document.getPage(0); contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false); this.insertProducts(); this.insertSupplies(); this.workOrderInfo(); document.save("pdf_docs\\work_orders\\" + "work_order_" + numOfWorkOrder.split("\\/")[0] + "_" + numOfWorkOrder.split("/")[1] + ".pdf"); document.close(); } public static void fillTables(String text, int x, int y, PDPageContentStream contentStream) { try { contentStream.beginText(); contentStream.setFont(PDType1Font.TIMES_ROMAN, 10); contentStream.newLineAtOffset(x, y); contentStream.showText(text); contentStream.endText(); } catch (IOException e) { e.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } } public static void fillInfo(String text, int x, int y, PDPageContentStream contentStream) { try { contentStream.beginText(); contentStream.setFont(PDType1Font.TIMES_BOLD, 16); contentStream.newLineAtOffset(x, y); contentStream.showText(text); contentStream.endText(); } catch (IOException e) { e.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } } public static File getTemplate() { return template; } public static void setTemplate(File template) { WorkOrderPDF.template = template; } public String getNumOfWorkOrder() { return numOfWorkOrder; } public void setNumOfWorkOrder(String numOfWorkOrder) { this.numOfWorkOrder = numOfWorkOrder; } public Date getDateOfWorkOrder() { return dateOfWorkOrder; } public void setDateOfWorkOrder(Date dateOfWorkOrder) { this.dateOfWorkOrder = dateOfWorkOrder; } public ArrayList<Product> getProducts() { return products; } public void setProducts(ArrayList<Product> products) { this.products = products; } public ArrayList<Supply> getSupplies() { return supplies; } public void setSupplies(ArrayList<Supply> supplies) { this.supplies = supplies; } }