Java での 画像フォーマットの PDF への変換

Aspose.PDF for Java は、多くのラスターおよびベクトル画像フォーマットを PDF ドキュメントに変換できます。

BMP を PDF に変換

BMP 画像を PDF ドキュメントに配置する必要がある場合は、この例を使用してください。

  1. 空の Document オブジェクトを作成し、出力 PDF を保持してください。
  2. Page オブジェクトを追加し、page.addImage(...) を使用して BMP を配置してください。
  3. 対象画像矩形を Rectangle クラスで定義し、ラスター コンテンツが PDF ページ領域全体を埋めるようにしてください。
  4. 出力 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 に変換する必要がある場合は、この例を使用してください。

  1. ファイルパスと CgmLoadOptions を Document のコンストラクタに渡して、CGM ソースを開いてください。
  2. ドキュメントの読み込み時に、Aspose.PDF に CGM グラフィックスストリームを解釈させてください。
  3. 変換された 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 ドキュメントにラップする必要がある場合は、この例を使用してください。

  1. 空の Document オブジェクトを PDF 出力用に作成してください。
  2. Image オブジェクトを作成し、その ImageFileType を Dicom に設定したうえで、ソースファイルのパスを割り当ててください。
  3. Page を追加し、DICOM 画像をそのページの段落コレクションに追加してください。
  4. 結果を 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 に変換する必要がある場合は、この例を使用してください。

  1. 空の Document を作成し、EMF ソースをバイナリストリームとして開いてください。
  2. Page を追加し、余白をクリアして EMF アートワークがページ全体を占められるようにしてください。
  3. Image を作成し、EMF ストリームをそれにバインドして、ページの段落コレクションに追加してください。
  4. 出力 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 コンテンツを変換する必要がある場合は、この例を使用してください。

  1. Aspose.Imaging を使用して EMF ソースをロードし、PDF 配置の前にメモリ内 PNG ストリームにレンダリングしてください。
  2. 空の Document を作成し、Page を追加してください。
  3. 中間バイトストリームから Image を作成し、ページに追加してください。
  4. 変換された 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 ページに追加する必要がある場合は、この例を使用してください。

  1. 空の Document オブジェクトを PDF 出力用に作成してください。
  2. Page を追加し、page.addImage(...) を使用して GIF を配置してください。
  3. 配置境界を Rectangle で定義し、画像がページ領域全体を埋めるようにしてください。
  4. 出力 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 に変換する必要がある場合は、この例を使用してください。

  1. 空の Document を出力 PDF 用に作成してください。
  2. Page を追加し、JPEG 画像を page.addImage(...) で挿入してください。
  3. ラスター画像がページ座標にどのようにマッピングされるかを制御するために、Rectangle を使用してください。
  4. 生成された 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 ドキュメントにラップする必要がある場合は、この例を使用してください。

  1. 変換出力用に空の Document を作成してください。
  2. Page を追加し、PNG 画像を page.addImage(...) で配置してください。
  3. Rectangle を使用して、ページキャンバスに対して画像のサイズを調整してください。
  4. 出力ファイルを保存してください。
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 ドキュメント内にレンダリングする必要がある場合は、この例を使用してください。

  1. ファイルパスと SvgLoadOptions を Document のコンストラクタに渡して、SVG ソースを開いてください。
  2. Aspose.PDF に SVG マークアップを解析させ、ロード中に対応する PDF グラフィックスモデルを作成させてください。
  3. 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 に変換する必要がある場合は、この例を使用してください。

  1. 空の Document オブジェクトを PDF 出力用に作成してください。
  2. Page を追加し、page.addImage(...) を使用して TIFF 画像を配置してください。
  3. 配置領域を Rectangle で定義し、TIFF コンテンツがページ座標にマッピングされるようにしてください。
  4. 結果を 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 に変換する必要がある場合は、この例を使用してください。

  1. ファイルパスと CdrLoadOptions を Document のコンストラクタに渡して、CDR ソースを開いてください。
  2. Aspose.PDF に CorelDRAW コンテンツを PDF ドキュメントモデルにロードさせてください。
  3. 変換された 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);
}