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 WriteOffProductsPDF { private static File template = new File("pdf_docs\\templates\\otpis_proizvoda_template.pdf"); private Date dateOfWriteOff; private ArrayList<Product> products = new ArrayList(); private static PDPageContentStream contentStream; private double sumProductPrice; public WriteOffProductsPDF() { } public WriteOffProductsPDF(ArrayList<Product> products, Date dateOfWriteOff) throws IOException { this.products = products; this.dateOfWriteOff = dateOfWriteOff; sumProductPrice = 0; createWriteOffProducts(); } public void createWriteOffProductsPNG(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\\write_off_products_picture\\" + "woppic-" + 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; for (Product pr : products) { if (row < 9) { fillTables(pr.getSelector(), 78 + (column * offsetColumn), 432 - (row * offsetRow), contentStream); fillTables(pr.getMeasureUnit(), 196 + (column * offsetColumn), 432 - (row * offsetRow), contentStream); fillTables(Double.toString(pr.getTempQuantity()), 238 + (column * offsetColumn), 432 - (row * offsetRow), contentStream); fillTables(Double.toString(pr.getTempPrice()), 290 + (column * offsetColumn), 432 - (row * offsetRow), contentStream); fillTables(Double.toString(pr.getTempQuantity() * pr.getTempPrice()), 345 + (column * offsetColumn), 432 - (row * offsetRow), contentStream); contentStream.close(); } else { fillTables(pr.getSelector(), 78 + (column * offsetColumn), 432 - (row * offsetRow) - delay, contentStream); fillTables(pr.getMeasureUnit(), 196 + (column * offsetColumn), 432 - (row * offsetRow) - delay, contentStream); fillTables(Double.toString(pr.getTempQuantity()), 238 + (column * offsetColumn), 432 - (row * offsetRow) - delay, contentStream); fillTables(Double.toString(pr.getTempPrice()), 290 + (column * offsetColumn), 432 - (row * offsetRow) - delay, contentStream); fillTables(Double.toString(pr.getTempQuantity() * pr.getTempPrice()), 345 + (column * offsetColumn), 432 - (row * offsetRow) - delay, contentStream); contentStream.close(); delay++; } sumProductPrice += pr.getTempQuantity() * pr.getTempPrice(); if (row == 19) { column = 1; } row = ++row % 20; } } private void writeOffProductsInfo() 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 void createWriteOffProducts() 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.writeOffProductsInfo(); contentStream.close(); SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy."); String path = "pdf_docs\\write_off_products\\" + "write_off_products-" + sdf.format(dateOfWriteOff) + ".pdf"; document.save(path); createWriteOffProductsPNG(path); 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(); } } }