summaryrefslogtreecommitdiff
path: root/src/main/java/it/alessandroiezzi/simplyreports/pdf/PDFTableHeader.java
blob: b9e55eb4905dd6e66b75ba405fb9d8d913766dae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Copyright (C) 2022  Alessandro Iezzi <aiezzi AT alessandroiezzi PERIOD it>
 *
 * This file is part of Simply Reports.
 *
 * Simply Reports is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Simply Reports is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Simply Reports.  If not, see <https://www.gnu.org/licenses/>.
 */

package it.alessandroiezzi.simplyreports.pdf;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Element;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import java.io.IOException;
import java.io.InputStream;

import lombok.*;

public class PDFTableHeader extends PDFEntity<PdfPCell> {
    private PdfReader reader;
    private @Getter @Setter Integer colspan;
    private @Getter @Setter Integer height;
    private @Getter @Setter Integer border;
    private @Getter @Setter String title;

    public PDFTableHeader(String logoLocation) {
        this(logoLocation, null, null, null, null);
    }

    public PDFTableHeader(InputStream inputStreamLogo, String title, Integer colspan, Integer height, Integer border) {
        this(colspan, height, border, title);
        try {
            this.reader = new PdfReader(inputStreamLogo);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public PDFTableHeader(Integer colspan, Integer height, Integer border, String title) {
        this.colspan = colspan;
        this.height = height;
        this.border = border;
        this.title = title;
    }

    public PDFTableHeader(String logoLocation, String title, Integer colspan, Integer height, Integer border) {
        this(colspan, height, border, title);

        try {
            this.reader = new PdfReader(logoLocation);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected PdfPCell generate(PdfWriter writer) {
        try {
            PdfPCell headerCell = new PdfPCell();
            headerCell.setPadding(5f);

            if (reader != null) {
                PdfImportedPage header = writer.getImportedPage(reader, 1);
                /* PdfPCell cell = new PdfPCell(Image.getInstance(header)); */
                Image headerImage = Image.getInstance(header);
                headerImage.setWidthPercentage(20f);
                headerCell.addElement(headerImage);
            }

            if (height != null) {
                headerCell.setFixedHeight(height);
            }

            if (border != null) {
                headerCell.setBorder(border);
            }

            if (colspan != null) {
                headerCell.setColspan(colspan);
            }

            if (title != null) {
                Paragraph p = new Paragraph(title);
                p.setAlignment(Element.ALIGN_CENTER);
                headerCell.addElement(p);
            }

            return headerCell;
        } catch (BadElementException ex) {
            throw new RuntimeException(ex);
        }
    }
}