Created
January 17, 2017 08:17
-
-
Save uchicom/bc068b304b5b47b39a8048bc603160ac to your computer and use it in GitHub Desktop.
pdfbox 2.0.4 JPEGファイルをPDFに出力する(Java8)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// (c) 2017 uchicom | |
package com.uchicom.pdfbox; | |
import java.io.IOException; | |
import org.apache.pdfbox.pdmodel.PDDocument; | |
import org.apache.pdfbox.pdmodel.PDPage; | |
import org.apache.pdfbox.pdmodel.PDPageContentStream; | |
import org.apache.pdfbox.pdmodel.common.PDRectangle; | |
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; | |
/** | |
* @author uchicom: Shigeki Uchiyama | |
* | |
*/ | |
public class Pdfbox2Sample5 { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
//PDFドキュメントを作成 | |
try (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); | |
//イメージオブジェクトを生成 | |
PDImageXObject xImage = PDImageXObject.createFromFile("sample1.jpg", document); | |
//DocumentへのObjectの登録はContentStream生成の前で実施。 | |
//書き込む用のストリームを準備 | |
PDPageContentStream stream = new PDPageContentStream(document, page); | |
//イメージ描画 | |
stream.drawImage(xImage, 100, 100); | |
//ストリームを閉じる | |
stream.close(); | |
//作成したPDFを保存 | |
document.save("p2s5.pdf"); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment