pdf.WriteOffMaterialsPDF.java Source code

Java tutorial

Introduction

Here is the source code for pdf.WriteOffMaterialsPDF.java

Source

/*
 * 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 WriteOffMaterialsPDF {
    private static File template = new File("pdf_docs\\templates\\otpis_materijala_template.pdf");
    private Date dateOfWriteOff;
    private ArrayList<Supply> supplies = new ArrayList();
    private static PDPageContentStream contentStream;
    private double sumSuppliesPrice;

    public WriteOffMaterialsPDF() {
    }

    public WriteOffMaterialsPDF(ArrayList<Supply> supplies, Date dateOfWriteOff) throws IOException {
        this.supplies = supplies;
        this.dateOfWriteOff = dateOfWriteOff;
        sumSuppliesPrice = 0;
        createWriteOffMaterials();
    }

    public void createWriteOffMaterialsPNG(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_materials_picture\\" + "wompic-" + sdf.format(dateOfWriteOff) + ".png"));
        doc.close();
    }

    public void insertSupplies() throws IOException {
        int row = 0;
        int column = 0;
        int offsetRow = 20;
        int offsetColumn = 353;
        int delay = 2;

        DecimalFormat df = new DecimalFormat("####0.00");
        for (Supply s : supplies) {
            if (row < 9) {
                fillTables(s.getName(), 82 + (column * offsetColumn), 428 - (row * offsetRow), contentStream);
                fillTables(s.getMeasureUnit(), 200 + (column * offsetColumn), 428 - (row * offsetRow),
                        contentStream);
                fillTables(df.format(s.getTempQuantity()), 238 + (column * offsetColumn), 428 - (row * offsetRow),
                        contentStream);
                fillTables(Double.toString(s.getPrice()), 290 + (column * offsetColumn), 428 - (row * offsetRow),
                        contentStream);
                fillTables(df.format(s.getTempQuantity() * s.getPrice()), 345 + (column * offsetColumn),
                        428 - (row * offsetRow), contentStream);
                contentStream.close();
            } else {
                //                delay = 1;
                fillTables(s.getName(), 82 + (column * offsetColumn), 428 - (row * offsetRow) - delay,
                        contentStream);
                fillTables(s.getMeasureUnit(), 200 + (column * offsetColumn), 428 - (row * offsetRow) - delay,
                        contentStream);
                fillTables(df.format(s.getTempQuantity()), 238 + (column * offsetColumn),
                        428 - (row * offsetRow) - delay, contentStream);
                fillTables(Double.toString(s.getPrice()), 290 + (column * offsetColumn),
                        428 - (row * offsetRow) - delay, contentStream);
                fillTables(df.format(s.getTempQuantity() * s.getPrice()), 345 + (column * offsetColumn),
                        428 - (row * offsetRow) - delay, contentStream);
                contentStream.close();
                delay++;
            }

            sumSuppliesPrice += s.getTempQuantity() * s.getPrice();

            if (row == 19) {
                column = 1;
                delay = 2;
            }
            row = ++row % 20;
        }
    }

    private void writeOffMaterialsInfo() throws IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy.");

        fillInfo(sdf.format(dateOfWriteOff), 120, 512, contentStream);
        DecimalFormat df = new DecimalFormat("####0.00");
        fillInfo(df.format(sumSuppliesPrice), 750, 50, contentStream);
        contentStream.close();
    }

    public void createWriteOffMaterials() 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.writeOffMaterialsInfo();
        contentStream.close();
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy.");
        String path = "pdf_docs\\write_off_materials\\" + "write_off_materials-" + sdf.format(dateOfWriteOff)
                + ".pdf";
        document.save(path);
        createWriteOffMaterialsPNG(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();
        }

    }
}