tech.chakapoko.com
Home / Java / Apache PDFBox

[Java]PDFBoxでテーブルを表示する

package com.example;

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 java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;


public class PDFBoxExample {

    static class Student {
        private int id;
        private String name;

        public Student(int id, String name) {
            this.id = id;
            this.name = name;
        }

        public int getId() {
            return id;
        }

        public String getName() {
            return name;
        }
    }

    public static void main(String[] args) throws IOException {
        List<Student> students = Arrays.asList(
                new Student(1, "Tom"),
                new Student(2, "Mary"),
                new Student(3, "John")
        );

        try (PDDocument pdDocument = new PDDocument()) {
            PDPage pdPage = new PDPage();
            pdDocument.addPage(pdPage);

            float pageHeight = pdPage.getMediaBox().getHeight();
            float rowHeight = 30;

            try (PDPageContentStream stream = new PDPageContentStream(pdDocument, pdPage)) {

                // table header
                float headerLinePostionY = pageHeight - rowHeight;
                float headerTextPositionY = headerLinePostionY + 5f;

                stream.moveTo(100f, headerLinePostionY);
                stream.lineTo(500f, headerLinePostionY);
                stream.setStrokingColor(Color.BLACK);
                stream.setLineWidth(1f);
                stream.stroke();

                stream.beginText();
                stream.setFont(PDType1Font.HELVETICA_BOLD, 12);
                stream.newLineAtOffset(130f, headerTextPositionY);
                stream.showText("ID");
                stream.endText();

                stream.beginText();
                stream.setFont(PDType1Font.HELVETICA_BOLD, 12);
                stream.newLineAtOffset(300f, headerTextPositionY);
                stream.showText("NAME");
                stream.endText();

                // table body
                for (int i = 0; i < students.size(); i++) {
                    Student student = students.get(i);

                    float linePostionY = pageHeight - ((i + 2) * rowHeight);
                    float textPositionY = linePostionY + 5f;

                    stream.moveTo(100f, linePostionY);
                    stream.lineTo(500f, linePostionY);
                    stream.setStrokingColor(Color.BLACK);
                    stream.setLineWidth(1f);
                    stream.stroke();

                    stream.beginText();
                    stream.setFont(PDType1Font.HELVETICA_BOLD, 12);
                    stream.newLineAtOffset(130f, textPositionY);
                    stream.showText(String.valueOf(student.getId()));
                    stream.endText();

                    stream.beginText();
                    stream.setFont(PDType1Font.HELVETICA, 12);
                    stream.newLineAtOffset(300f, textPositionY);
                    stream.showText(student.getName());
                    stream.endText();
                }
            }

            pdDocument.save(new File("Example.pdf"));
        }
    }
}

draw-a-table.png