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.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

import java.io.File;
import java.io.IOException;

public class PDFBoxExample {

    public static void main(String[] args) throws IOException {
        try (PDDocument doc = new PDDocument()) {
            PDPage pdPage = new PDPage();
            doc.addPage(pdPage);

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

            try (PDPageContentStream stream = new PDPageContentStream(doc, pdPage)) {
                PDFont font = PDType1Font.HELVETICA;

                float padding = 5f;
                float x = 100f;
                float y = pageHeight - 100f;

                stream.setNonStrokingColor(200, 200, 200);
                stream.addRect(x,y, 100f, 30f);
                stream.fill();


                stream.beginText();
                stream.setNonStrokingColor(0, 0, 0);
                stream.setFont(font, 24);
                stream.newLineAtOffset(x + padding, y + padding);
                stream.showText("Hello");
                stream.endText();

            }

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

    }

}

set-background-color.png