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 java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.text.DecimalFormat; import java.text.SimpleDateFormat; 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 Casper */ public class StockItemsPDF { private static File template = new File("pdf_docs\\templates\\stanje_proizvoda_template.pdf"); private Date dateOfWriteOff; private ArrayList<Product> products = new ArrayList(); private static PDPageContentStream contentStream; private double sumProductPrice; public StockItemsPDF() { } public StockItemsPDF(ArrayList<Product> products, Date dateOfWriteOff) throws IOException { this.products = products; this.dateOfWriteOff = dateOfWriteOff; sumProductPrice = 0; createStockItems(); } public void createStockItemsPNG(String path) throws IOException { File file = new File(path); PDDocument doc = PDDocument.load(file); PDFRenderer renderer = new PDFRenderer(doc); BufferedImage image = renderer.renderImage(0); SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy."); ImageIO.write(image, "PNG", new File("pdf_docs\\stock_items_picture\\" + "sipic-" + sdf.format(dateOfWriteOff) + ".png")); doc.close(); } public void insertProducts() throws IOException { int row = 0; int column = 0; int offsetRow = 20; int offsetColumn = 353; int delay = 2; sumProductPrice = 0; for (Product pr : products) { String naz = ""; if (pr.getSelector().equals("kolac")) { naz = " ("; naz += pr.getName(); naz += ")"; } if (row < 4) { fillTables(pr.getSelector() + naz, 83 + (column * offsetColumn), 430 - (row * offsetRow), contentStream); fillTables(pr.getMeasureUnit(), 196 + (column * offsetColumn), 430 - (row * offsetRow), contentStream); fillTables(Double.toString(pr.getTempQuantity()), 238 + (column * offsetColumn), 430 - (row * offsetRow), contentStream); fillTables(Double.toString(pr.getTempPrice()), 290 + (column * offsetColumn), 430 - (row * offsetRow), contentStream); fillTables(Double.toString(pr.getTempQuantity() * pr.getTempPrice()), 345 + (column * offsetColumn), 430 - (row * offsetRow), contentStream); fillTables(pr.getFormatName(), 400 + (column * offsetColumn), 430 - (row * offsetRow), contentStream); contentStream.close(); } else { fillTables(pr.getSelector() + naz, 83 + (column * offsetColumn), 430 - (row * offsetRow) - delay, contentStream); fillTables(pr.getMeasureUnit(), 196 + (column * offsetColumn), 430 - (row * offsetRow) - delay, contentStream); fillTables(Double.toString(pr.getTempQuantity()), 238 + (column * offsetColumn), 430 - (row * offsetRow) - delay, contentStream); fillTables(Double.toString(pr.getTempPrice()), 290 + (column * offsetColumn), 430 - (row * offsetRow) - delay, contentStream); fillTables(Double.toString(pr.getTempQuantity() * pr.getTempPrice()), 345 + (column * offsetColumn), 430 - (row * offsetRow) - delay, contentStream); fillTables(pr.getFormatName(), 400 + (column * offsetColumn), 430 - (row * offsetRow) - delay, contentStream); contentStream.close(); if (row < 11) delay++; } sumProductPrice += pr.getTempQuantity() * pr.getTempPrice(); row = ++row % 20; } } private void stockItemsInfo() throws IOException { SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy."); fillInfo(sdf.format(dateOfWriteOff), 120, 516, contentStream); DecimalFormat df = new DecimalFormat("####0.00"); fillInfo(df.format(sumProductPrice), 750, 50, contentStream); contentStream.close(); } public String createStockItems() 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.stockItemsInfo(); contentStream.close(); SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy."); String path = "pdf_docs\\stock_items\\" + "stock_item_" + dateOfWriteOff.getTime() + ".pdf"; document.save(path); //createStockItemsPNG(path); document.close(); return path; } 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); if (text != null) { 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(); } } }