pdf.NormativPDF.java Source code

Java tutorial

Introduction

Here is the source code for pdf.NormativPDF.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.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.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;
import static pdf.SuppliesListPDF.fillTables;

/**
 *
 * @author Casper
 */
public class NormativPDF {

    private static File template = new File("pdf_docs\\templates\\normativ_template.pdf");
    private Product product;
    private Date dateNormativ;
    private ArrayList<Supply> supplies = new ArrayList<>();
    private static PDPageContentStream contentStream;

    public NormativPDF() {

    }

    public NormativPDF(Product product, ArrayList<Supply> supplies, Date dateNormativ) throws IOException {
        this.product = product;
        this.supplies = supplies;
        this.dateNormativ = dateNormativ;
        this.product.createFormatName();
        //createNormativ();
    }

    public void createNormativPNG(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\\normativ_picture\\" + product.getName() + "-" + sdf.format(dateNormativ) + ".png"));
        doc.close();
    }

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

        DecimalFormat df = new DecimalFormat("####0.00");
        for (Supply s : supplies) {
            if (row < 5) {
                fillTables(s.getName(), 166 + (column * offsetColumn), 332 - (row * offsetRow), contentStream);
                fillTables(s.getMeasureUnit(), 308 + (column * offsetColumn), 332 - (row * offsetRow),
                        contentStream);
                fillTables(df.format(s.getTempQuantity()), 360 + (column * offsetColumn), 332 - (row * offsetRow),
                        contentStream);
                contentStream.close();
            } else {
                fillTables(s.getName(), 166 + (column * offsetColumn), 332 - (row * offsetRow) - delay,
                        contentStream);
                fillTables(s.getMeasureUnit(), 308 + (column * offsetColumn), 332 - (row * offsetRow) - delay,
                        contentStream);
                fillTables(df.format(s.getTempQuantity()), 360 + (column * offsetColumn),
                        332 - (row * offsetRow) - delay, contentStream);
                contentStream.close();
                delay += 2;
                if (row > 10)
                    delay--;
            }
            if (row == 14) {
                column = 1;
                delay = 0;
            }
            row = ++row % 15;
        }
    }

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

        fillInfo(sdf.format(dateNormativ), 128, 512, contentStream);
        contentStream.close();
    }

    private void insertProduct() throws IOException {
        String text = "";
        if (product.getSelector().equals("kolac")) {
            text = " (" + product.getName() + ")";
        }
        fillTables(product.getSelector(), 146, 432, contentStream);
        fillTables(product.getMeasureUnit(), 262, 432, contentStream);
        fillTables(product.getFormatName(), 310, 432, contentStream);
        contentStream.close();
    }

    public String createNormativ() 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.insertProduct();
        this.normativInfo();
        contentStream.close();
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy.");
        String path = "pdf_docs\\norms\\" + "norm" + "_" + dateNormativ.getTime() + ".pdf";
        document.save(path);
        //createNormativPNG(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();
        }

    }
}