Java での 画像フォーマットの PDF への変換
Contents
[
Hide
]
Aspose.PDF for Java は、多くのラスターおよびベクトル画像フォーマットを PDF ドキュメントに変換できます。
BMP を PDF に変換
BMP 画像を PDF ドキュメントに配置する必要がある場合は、この例を使用してください。
- 空の
Documentオブジェクトを作成し、出力 PDF を保持してください。 Pageオブジェクトを追加し、page.addImage(...)を使用して BMP を配置してください。- 対象画像矩形を
Rectangleクラスで定義し、ラスター コンテンツが PDF ページ領域全体を埋めるようにしてください。 - 出力 PDF ファイルを保存してください。
public static void convertBmpToPdf(Path inputFile, Path outputFile) {
try (Document document = new Document()) {
try (Page page = document.getPages().add()) {
page.addImage(inputFile.toString(), new Rectangle(0, 0, 595, 842, true));
}
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
CGM を PDF に変換
CGM グラフィック ファイルを PDF に変換する必要がある場合は、この例を使用してください。
- ファイルパスと
CgmLoadOptionsをDocumentのコンストラクタに渡して、CGM ソースを開いてください。 - ドキュメントの読み込み時に、Aspose.PDF に CGM グラフィックスストリームを解釈させてください。
- 変換された PDF をターゲット出力パスに保存してください。
public static void convertCgmToPdf(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString(), new CgmLoadOptions())) {
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
DICOM を PDF に変換
医療用 DICOM 画像を PDF ドキュメントにラップする必要がある場合は、この例を使用してください。
- 空の
Documentオブジェクトを PDF 出力用に作成してください。 Imageオブジェクトを作成し、そのImageFileTypeをDicomに設定したうえで、ソースファイルのパスを割り当ててください。Pageを追加し、DICOM 画像をそのページの段落コレクションに追加してください。- 結果を PDF として保存してください。
public static void convertDicomToPdf(Path inputFile, Path outputFile) {
try (Document document = new Document()) {
Image image = new Image();
image.setFileType(ImageFileType.Dicom);
image.setFile(inputFile.toString());
try (Page page = document.getPages().add()) {
page.getParagraphs().add(image);
}
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
直接文書をロードして EMF を PDF に変換
EMF ファイルを主要な EMF ロードパスを介して PDF に変換する必要がある場合は、この例を使用してください。
- 空の
Documentを作成し、EMF ソースをバイナリストリームとして開いてください。 Pageを追加し、余白をクリアして EMF アートワークがページ全体を占められるようにしてください。Imageを作成し、EMF ストリームをそれにバインドして、ページの段落コレクションに追加してください。- 出力 PDF ファイルを保存してください。
public static void convertEmfToPdf01(Path inputFile, Path outputFile) throws IOException {
try (Document document = new Document();
FileInputStream imageStream = new FileInputStream(inputFile.toFile())) {
try (Page page = document.getPages().add()) {
page.getPageInfo().getMargin().setBottom(0);
page.getPageInfo().getMargin().setTop(0);
page.getPageInfo().getMargin().setLeft(0);
page.getPageInfo().getMargin().setRight(0);
Image image = new Image();
image.setFileType(ImageFileType.Unknown);
image.setImageStream(imageStream);
page.getParagraphs().add(image);
}
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
代替ワークフローで EMF を PDF に変換
代替の設定またはページ構成フローを使用して EMF コンテンツを変換する必要がある場合は、この例を使用してください。
- Aspose.Imaging を使用して EMF ソースをロードし、PDF 配置の前にメモリ内 PNG ストリームにレンダリングしてください。
- 空の
Documentを作成し、Pageを追加してください。 - 中間バイトストリームから
Imageを作成し、ページに追加してください。 - 変換された PDF を保存してください。
public static void convertEmfToPdf02(Path inputFile, Path outputFile) throws IOException {
try (Document document = new Document();
com.aspose.imaging.Image emfImage = com.aspose.imaging.Image.load(inputFile.toString());
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
emfImage.save(byteArrayOutputStream, new PngOptions());
try (Page page = document.getPages().add()) {
Image image = new Image();
image.setImageStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
page.getParagraphs().add(image);
}
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
GIF を PDF に変換
GIF 画像を PDF ページに追加する必要がある場合は、この例を使用してください。
- 空の
Documentオブジェクトを PDF 出力用に作成してください。 Pageを追加し、page.addImage(...)を使用して GIF を配置してください。- 配置境界を
Rectangleで定義し、画像がページ領域全体を埋めるようにしてください。 - 出力 PDF を保存してください。
public static void convertGifToPdf(Path inputFile, Path outputFile) {
try (Document document = new Document()) {
try (Page page = document.getPages().add()) {
page.addImage(inputFile.toString(), new Rectangle(0, 0, 595, 842, true));
}
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
JPEG から PDF への変換
JPEG 画像を 1 ページの PDF に変換する必要がある場合は、この例を使用してください。
- 空の
Documentを出力 PDF 用に作成してください。 Pageを追加し、JPEG 画像をpage.addImage(...)で挿入してください。- ラスター画像がページ座標にどのようにマッピングされるかを制御するために、
Rectangleを使用してください。 - 生成された PDF ファイルを保存してください。
public static void convertJpegToPdf(Path inputFile, Path outputFile) {
try (Document document = new Document()) {
try (Page page = document.getPages().add()) {
page.addImage(inputFile.toString(), new Rectangle(0, 0, 595, 842, true));
}
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
PNG から PDF への変換
PNG 画像を PDF ドキュメントにラップする必要がある場合は、この例を使用してください。
- 変換出力用に空の
Documentを作成してください。 Pageを追加し、PNG 画像をpage.addImage(...)で配置してください。Rectangleを使用して、ページキャンバスに対して画像のサイズを調整してください。- 出力ファイルを保存してください。
public static void convertPngToPdf(Path inputFile, Path outputFile) {
try (Document document = new Document()) {
try (Page page = document.getPages().add()) {
page.addImage(inputFile.toString(), new Rectangle(0, 0, 595, 842, true));
}
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
SVG から PDF への変換
SVG アートワークを PDF ドキュメント内にレンダリングする必要がある場合は、この例を使用してください。
- ファイルパスと
SvgLoadOptionsをDocumentのコンストラクタに渡して、SVG ソースを開いてください。 - Aspose.PDF に SVG マークアップを解析させ、ロード中に対応する PDF グラフィックスモデルを作成させてください。
- PDF の出力を対象のファイルパスに保存してください。
public static void convertSvgToPdf(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString(), new SvgLoadOptions())) {
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
TIFF から PDF への変換
TIFF 画像を PDF に変換する必要がある場合は、この例を使用してください。
- 空の
Documentオブジェクトを PDF 出力用に作成してください。 Pageを追加し、page.addImage(...)を使用して TIFF 画像を配置してください。- 配置領域を
Rectangleで定義し、TIFF コンテンツがページ座標にマッピングされるようにしてください。 - 結果を PDF として保存してください。
public static void convertTiffToPdf(Path inputFile, Path outputFile) {
try (Document document = new Document()) {
try (Page page = document.getPages().add()) {
page.addImage(inputFile.toString(), new Rectangle(0, 0, 595, 842, true));
}
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
CDR から PDF への変換
CorelDRAW CDR ファイルを PDF に変換する必要がある場合は、この例を使用してください。
- ファイルパスと
CdrLoadOptionsをDocumentのコンストラクタに渡して、CDR ソースを開いてください。 - Aspose.PDF に CorelDRAW コンテンツを PDF ドキュメントモデルにロードさせてください。
- 変換された PDF ファイルを指定された出力パスに保存してください。
public static void convertCdrToPdf(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString(), new CdrLoadOptions())) {
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}