PDFBox メモ
PDFBoxを調査する機会があったので、そのメモ。
PDFBoxとは
Apache PDFBoxで開発されているPDF処理用のJavaのライブラリ。
事前準備
JDKのインストールとPDFBox - Download
ここでは、pdfbox-1.8.2.jarを使用した。
サンプルプログラム
ページが1ページあるだけのPDFを作成する
try {
//PDFドキュメントを作成
PDDocument document = new PDDocument();
//ページを追加
document.addPage(new PDPage());
//作成したPDFを保存
document.save("hogehoge.pdf");
document.close();
} catch (IOException e) {
e.printStackTrace();
} catch (COSVisitorException e) {
e.printStackTrace();
}
ページが2ページあるだけのPDFを作成する
try {
//PDFドキュメントを作成
PDDocument document = new PDDocument();
//ページを追加(1ページ目)
document.addPage(new PDPage());
//ページを追加(2ページ目)
document.addPage(new PDPage());
//作成したPDFを保存
document.save("hogehoge.pdf");
document.close();
} catch (IOException e) {
e.printStackTrace();
} catch (COSVisitorException e) {
e.printStackTrace();
}
ページサイズを指定して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ページ目)
document.addPage(new PDPage(rec));
//作成したPDFを保存
document.save("hogehoge.pdf");
document.close();
} catch (IOException e) {
e.printStackTrace();
} catch (COSVisitorException e) {
e.printStackTrace();
}
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();
}
JPEGファイルを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);
//イメージオブジェクトを生成
PDXObjectImage xImage = new PDJpeg(document, new FileInputStream("hogehoge.jpg"));
//DocumentへのObjectの登録はContentStream生成の前で実施。
//書き込む用のストリームを準備
PDPageContentStream stream = new PDPageContentStream(document, page);
//アフィン変換を指定
AffineTransform transform = new AffineTransform(1000, 0, 0, 1000, 0, 0);
//イメージ描画
stream.drawXObject(xImage, transform);
//ストリームを閉じる
stream.close();
//作成したPDFを保存
document.save("hogehoge.pdf");
document.close();
} catch (IOException e) {
e.printStackTrace();
} catch (COSVisitorException e) {
e.printStackTrace();
}
PDFからJPEGファイルを抽出する
try {
//PDFドキュメントをロード
PDDocument document = PDDocument.load("hogehoge.pdf");
//ページのリストから最初の1ページを取得する
PDPage page = (PDPage) document.getDocumentCatalog().getAllPages().get(0);
//ページからリソースを取得し、全てのイメージを取得する。
PDResources resources = page.getResources();
Map images = resources.getImages();
if (images != null) {
Iterator> ite = images.entrySet().iterator();
while (ite.hasNext()) {
PDXObjectImage image = ite.next().getValue();
//取得したイメージをファイルに出力
image.write2file(System.currentTimeMillis() + "hogehoge");
}
}
document.close();
} catch (IOException e) {
e.printStackTrace();
}
PDFからJPEGデータをBufferedImageとして取得する
try {
//PDFドキュメントをロード
PDDocument document = PDDocument.load("hogehoge.pdf");
//ページのリストから最初の1ページを取得する
PDPage page = (PDPage) document.getDocumentCatalog().getAllPages().get(0);
//ページからリソースを取得し、全てのイメージを取得する。
PDResources resources = page.getResources();
Map images = resources.getImages();
if (images != null) {
Iterator imageIter = images.keySet().iterator();
while (imageIter.hasNext()) {
String key = (String) imageIter.next();
PDXObjectImage image = (PDXObjectImage) images.get(key);
//取得したイメージをファイルに出力
}
}
document.close();
} catch (IOException e) {
e.printStackTrace();
}
PDFにTEXTデータを一つ出力する
この方法では日本語が文字化けする。
try {
//PDFドキュメントを作成
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
//書き込む用のストリームを準備
PDPageContentStream stream = new PDPageContentStream(document, page);
//テキスト出力開始
stream.beginText();
//フォント設定
stream.setFont(PDType1Font.COURIER, 12);
//文字の配置設定
stream.moveTextPositionByAmount(10, 10);
//文字列出力
stream.drawString("test漢字OK");
//テキスト出力終了
stream.endText();
//書き込む用のストリームを閉じる
stream.close();
document.save("hogehoge.pdf");
document.close();
} catch (IOException e) {
e.printStackTrace();
} catch (COSVisitorException e) {
e.printStackTrace();
}
PDFにTEXTデータを二つ出力する
この方法では日本語が文字化けする。
try {
//PDFドキュメントを作成
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
//書き込む用のストリームを準備
PDPageContentStream stream = new PDPageContentStream(document, page);
//テキスト1出力開始
stream.beginText();
//フォント設定
stream.setFont(PDType1Font.COURIER, 12);
//文字の配置設定
stream.moveTextPositionByAmount(10, 10);
//文字列出力
stream.drawString("test漢字OK");
//テキスト1出力終了
stream.endText();
//テキスト2出力開始
stream.beginText();
//フォント設定
stream.setFont(PDType1Font.COURIER_BOLD, 12);
//文字の配置設定
stream.moveTextPositionByAmount(20, 20);
//文字列出力
stream.drawString("test漢字OK?");
//テキスト2出力終了
stream.endText();
//書き込む用のストリームを閉じる
stream.close();
document.save("hogehoge.pdf");
document.close();
} catch (IOException e) {
e.printStackTrace();
} catch (COSVisitorException e) {
e.printStackTrace();
}
PDFからTEXTデータを抽出してテキストファイルへ出力する
try {
//PDFドキュメントをロード
PDDocument document = PDDocument.load("hogehoge.pdf");
//抽出データ出力ファイル
Writer writer = new OutputStreamWriter(new FileOutputStream("hogehoge.txt"));
//テキスト分解クラス生成
PDFTextStripper stripper = new PDFTextStripper();
//抽出&出力実施
stripper.writeText(document, writer);
document.close();
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
PDFからTEXTデータを抽出して標準出力へ出力する
try {
//PDFドキュメントをロード
PDDocument document = PDDocument.load("hogehoge.pdf");
//テキスト分解クラス生成
PDFTextStripper stripper = new PDFTextStripper();
//抽出実施
String text = stripper.getText(document);
//標準出力で確認
System.out.println(text);
document.close();
} catch (IOException e) {
e.printStackTrace();
}
PDFから解像度を取得する
try {
//PDFドキュメントをロード
PDDocument document = PDDocument.load("hogehoge.pdf");
//ページのリストから最初の1ページを取得する
PDPage page = (PDPage) document.getDocumentCatalog().getAllPages().get(0);
//ページからリソースを取得し、全てのイメージを取得する。
PDResources resources = page.getResources();
Map images = resources.getImages();
if (images != null) {
Iterator imageIter = images.keySet().iterator();
while (imageIter.hasNext()) {
String key = (String) imageIter.next();
PDXObjectImage image = (PDXObjectImage) images.get(key);
PDRectangle cropBox = page.findCropBox();
if (cropBox != null) {
//dpiの算出(pdfboxのデフォルトdpiは72)
int dpi = Math.round(image.getHeight() * 72 / cropBox.getHeight());
//dpi
System.out.println("dpi:" + dpi);
}
}
}
document.close();
} catch (IOException e) {
e.printStackTrace();
}
PDFからページの画像を取得する
ソフトマスクのあるページで取得したJPEGがそのまま使えなかったので、こちらからページのイメージを取得する.
try {
//PDFドキュメントをロード
PDDocument document = PDDocument.load("hogehoge.pdf");
//ページのリストから最初の1ページを取得する
PDPage page = (PDPage) document.getDocumentCatalog().getAllPages().get(0);
//ページからリソースを取得し、全てのイメージを取得する。
PDResources resources = page.getResources();
Map images = resources.getImages();
if (images != null) {
Iterator imageIter = images.keySet().iterator();
while (imageIter.hasNext()) {
String key = (String) imageIter.next();
PDXObjectImage image = (PDXObjectImage) images.get(key);
PDRectangle cropBox = page.findCropBox();
if (cropBox != null) {
//dpiの算出(pdfboxのデフォルトdpiは72)
int dpi = Math.round(image.getHeight() * 72 / cropBox.getHeight());
//dpiのサイズに変換しつつイメージ取得
BufferedImage bufferedImage = page.convertToImage(BufferedImage.TYPE_3BYTE_BGR, dpi);
File outputfile = new File(System.currentTimeMillis() + "_hoge.jpg");
ImageIO.write(bufferedImage, "jpg", outputfile);
}
}
}
document.close();
} catch (IOException e) {
e.printStackTrace();
}