PDFBox サンプルプログラム

目次

CropBox,BleedBox(裁ち落としを含むサイズ(塗り足し部分?)),TrimBox(仕上がりサイズ)を指定して1ページのPDFを作成する


try {
    //PDFドキュメントを作成
    PDDocument document = new PDDocument();
    //サイズ指定
    PDRectangle rec = new PDRectangle();
    rec.setUpperRightX(0);
    rec.setUpperRightY(0);
    rec.setLowerLeftX(1000);
    rec.setLowerLeftY(1000);
    //ページを追加(1ページ目)
    
    PDPage page = new PDPage(rec);
    document.addPage(page);
    
    //CropBoxのサイズ指定
    rec = new PDRectangle();
    rec.setUpperRightX(0);
    rec.setUpperRightY(0);
    rec.setLowerLeftX(1000);
    rec.setLowerLeftY(1000);
    
    //CropBoxを設定
    page.setCropBox(rec);
    
    //BleedBox(裁ち落としを含む)のサイズ指定 ※TrimBoxよりも大きくなる
    rec = new PDRectangle();
    rec.setUpperRightX(585.11F);
    rec.setUpperRightY(3.17F);
    rec.setLowerLeftX(3.17F);
    rec.setLowerLeftY(805.59F);
    
    //BleedBoxを設定
    page.setBleedBox(rec);
    
    //TrimBox(仕上がり)のサイズ指定
    rec = new PDRectangle();
    rec.setUpperRightX(580F);
    rec.setUpperRightY(10F);
    rec.setLowerLeftX(10F);
    rec.setLowerLeftY(800F);
    
    //TrimBoxを設定
    page.setTrimBox(rec);
    
    //作成したPDFを保存
    document.save("hogehoge.pdf");
    document.close();
} catch (IOException e) {
    e.printStackTrace();
} catch (COSVisitorException e) {
    e.printStackTrace();
}