OEM販売のご相談
ご相談ください!

PDF Tool APIサンプルコード:画像ファイルのPDF化

機能イメージ

指定の画像ファイルをPDFとして出力します。

概要

サンプルコードの概要

指定した画像を元のサイズのまま新規PDFとして出力します。

  • PtlParamImagePage :画像のページ作成に使うパラメータクラスです
  • PtlParamDrawImage :画像の描画に使うパラメータクラスです
  • PtlParamDrawImage.setImageStream() :入力画像ストリームを設定
  • PtlParamImagePage.setImage() :ページに挿入する画像パラメータを設定
  • PtlParamImagePage.setPaperType() :用紙タイプを設定
  • PtlParamImagePage.PAPER_TYPE.PAPER_IMAGE_SIZE :画像サイズに合わせる(デフォルト値)

サンプルコード

/*
	Antenna House PDF Tool API V8.0
	C++ Interface sample program

	概要:画像ファイルのPDF化

	Copyright 2013-2025 Antenna House, Inc.
*/

#include < PdfTk.h >
#include < stdio.h >

using namespace PdfTk;

int main(int argc, char* argv[])
{
	if (argc < 3) {
		printf("usage: ImageToPdf.exe append-image-file out-pdf-file\n");
		return 1;
	}
	try
	{
		PtlParamInput inputImage(argv[1]);
		PtlParamOutput output(argv[2]);

		PtlPDFDocument doc;

		// ページコンテナの取得
		PtlPages& pages = doc.getPages();

		// 画像のページ作成に使うパラメータクラス
		PtlParamImagePage paramImagePage;

		// 画像の描画に使うパラメータクラス
		PtlParamDrawImage paramDrawImage;

		// 入力画像ストリームの設定
		paramDrawImage.setImageStream(inputImage);

		// ページに挿入する画像パラメータの設定。
		paramImagePage.setImage(paramDrawImage);

		// 用紙タイプの設定  PAPER_IMAGE_SIZE /* 画像サイズに合わせる */
		paramImagePage.setPaperType(PtlParamImagePage::PAPER_IMAGE_SIZE);

		// ページの追加
		pages.append(paramImagePage);

		// ファイルに保存します。
		doc.save(output);

		printf("完了!\n");
	}
	catch (const PtlException &e)
	{
		fprintf(stderr, "Error code : %d\n %s\n", e.getErrorCode(), e.getErrorMessage().c_str());
		return 1;
	}	
	return 0;
}


            
/*
    Antenna House PDF Tool API V8.0
    Java Interface sample program

    概要:画像ファイルのPDF化

    Copyright 2015-2025 Antenna House, Inc.
*/

package Sample;

import jp.co.antenna.ptl.*;

public class ImageToPdf {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 2)
        {
            System.out.println("usage: java ImageToPdf image-file out-pdf-file");
            return;
        }

        try (PtlParamOutput outputFile = new PtlParamOutput(args[1]);
             PtlPDFDocument doc = new PtlPDFDocument();
             PtlPages pages = doc.getPages())  // ページコンテナの取得
        {
            try (PtlParamDrawImage paramDrawImage = new PtlParamDrawImage();  // 画像描画パラメータ
                 PtlParamInput inputImage = new PtlParamInput(args[0]);       // 画像ファイル
                 PtlParamImagePage paramImagePage = new PtlParamImagePage())  // 画像ページパラメータ
            {
                // 画像描画パラメータに画像ファイルを設定
                paramDrawImage.setImageStream(inputImage);

                // 画像ページパラメータに画像描画パラメータを設定
                paramImagePage.setImage(paramDrawImage);

                // 画像ページのサイズを画像サイズにあわせる
                paramImagePage.setPaperType(PtlParamImagePage.PAPER_TYPE.PAPER_IMAGE_SIZE);

                // ページコンテナに画像ページパラメータを追加
                pages.append(paramImagePage);
            }

            // ファイルに保存します。
            doc.save(outputFile);
        }
        catch (PtlException pex) {
             System.out.println("PtlException : ErrorCode = " + pex.getErrorCode() + "\n  " + pex.getErrorMessage());
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
        catch (Error ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
        finally {
            System.out.println("-- 完了 --");
        }
    }
}

            
/*
	Antenna House PDF Tool API V8.0
	.NET Interface sample program

	概要:画像ファイルのPDF化

	Copyright 2013-2025 Antenna House, Inc.
*/

using System;
using PdfTkNet;

namespace ImageToPdf
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("usage: ImageToPdf.exe image-file output-pdf");
                return;
            }

            try
            {
                using (PtlParamInput inputImage = new PtlParamInput(args[0]))
                using (PtlParamOutput outputFile = new PtlParamOutput(args[1]))
                using (PtlPDFDocument doc = new PtlPDFDocument())
                using (PtlPages pages = doc.getPages())
                {
                    // 画像のページ作成に使うパラメータクラス
                    using (PtlParamImagePage paramImagePage = new PtlParamImagePage())
                    {
                        // 画像の描画に使うパラメータクラス
                        using (PtlParamDrawImage paramDrawImage = new PtlParamDrawImage())
                        {
                            // 入力画像ストリームの設定
                            paramDrawImage.setImageStream(inputImage);

                            // ページに挿入する画像パラメータの設定。
                            paramImagePage.setImage(paramDrawImage);
                        }
                        // 用紙タイプの設定  PAPER_IMAGE_SIZE /* 画像サイズに合わせる */
                        paramImagePage.setPaperType(PtlParamImagePage.PAPER_TYPE.PAPER_IMAGE_SIZE);

                        // ページの追加
                        pages.append(paramImagePage);
                    }
                    // ファイルに保存します。
                    doc.save(outputFile);
                }
            }
            catch (PtlException pex)
            {
                Console.WriteLine(pex.getErrorCode() + " : " + pex.getErrorMessageJP());
                pex.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.WriteLine("-- 完了 --");
            }
        }
    }
}

            
AHPDFToolCmd80.exe -imageToPdf -d C:\in\test.jpg -o C:\sav\outImageToPdf.pdf

            

サンプルコードのダウンロードはこちら

実行例

コマンドラインでの実行例

ImageToPdf.exe C:\in\test.jpg C:\sav\outImageToPdf.pdf
完了!
java -jar ImageToPdf.jar C:\in\test.jpg C:\sav\outImageToPdf.pdf
-- 完了 --
ImageToPdf.exe C:\in\test.jpg C:\sav\outImageToPdf.pdf
-- 完了 --
AHPDFToolCmd80.exe -imageToPdf -d C:\in\test.jpg -o C:\sav\outImageToPdf.pdf
 use time 0.305000s

出力結果イメージ

指定した画像を1ページとしたPDFが出力されます。

出力イメージ

サンプルコードのダウンロード

サンプルコード

サンプルで使用した入出力PDF