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.Supply; 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 SuppliesListPDF { private static File template = new File("pdf_docs\\templates\\lager_list_template.pdf"); private Date dateSuppliesList; private ArrayList<Supply> supplies = new ArrayList(); private static PDPageContentStream contentStream; private double sumSuppliesPrice; public SuppliesListPDF() { } public SuppliesListPDF(ArrayList<Supply> supplies, Date dateSuppliesList) throws IOException { this.supplies = supplies; this.dateSuppliesList = dateSuppliesList; sumSuppliesPrice = 0; createSuppliesList(); } public void createDailyReportPNG(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\\lager_lists_picture\\" + "llpic-" + dateSuppliesList.getTime() + ".png")); doc.close(); } public void insertSupplies() throws IOException { int row = 0; int column = 0; int offsetRow = 14; int offsetColumn = 348; int delay = 2; sumSuppliesPrice = 0; DecimalFormat df = new DecimalFormat("####0.00"); for (Supply s : supplies) { if (row < 5) { fillTables(s.getName(), 72 + (column * offsetColumn), 394 - (row * offsetRow), contentStream); fillTables(s.getMeasureUnit(), 188 + (column * offsetColumn), 394 - (row * offsetRow), contentStream); fillTables(df.format(s.getQuantityInStore()), 238 + (column * offsetColumn), 394 - (row * offsetRow), contentStream); fillTables(Double.toString(s.getPrice()), 290 + (column * offsetColumn), 394 - (row * offsetRow), contentStream); fillTables(df.format(s.getQuantityInStore() * s.getPrice()), 345 + (column * offsetColumn), 394 - (row * offsetRow), contentStream); contentStream.close(); } else { fillTables(s.getName(), 72 + (column * offsetColumn), 394 - (row * offsetRow) - delay, contentStream); fillTables(s.getMeasureUnit(), 188 + (column * offsetColumn), 394 - (row * offsetRow) - delay, contentStream); fillTables(df.format(s.getQuantityInStore()), 238 + (column * offsetColumn), 394 - (row * offsetRow) - delay, contentStream); fillTables(Double.toString(s.getPrice()), 290 + (column * offsetColumn), 394 - (row * offsetRow) - delay, contentStream); fillTables(df.format(s.getQuantityInStore() * s.getPrice()), 345 + (column * offsetColumn), 394 - (row * offsetRow) - delay, contentStream); contentStream.close(); delay += 2; } sumSuppliesPrice += Double.parseDouble(df.format(s.getQuantityInStore() * s.getPrice())); if (row == 19) { column = 1; delay = 2; } row = ++row % 20; } } private void suppliesListInfo() throws IOException { SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy."); fillInfo(sdf.format(dateSuppliesList), 100, 486, contentStream); DecimalFormat df = new DecimalFormat("####0.00"); fillInfo(df.format(sumSuppliesPrice), 750, 100, contentStream); contentStream.close(); } public String createSuppliesList() throws IOException { PDDocument document = PDDocument.load(template); //prva strana PDPage page = document.getPage(0); contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false); this.insertSupplies(); this.suppliesListInfo(); contentStream.close(); SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy."); String path = "pdf_docs\\lager_lists\\" + "supplies_list_" + dateSuppliesList.getTime() + ".pdf"; document.save(path); //createDailyReportPNG(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); 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(); } } }