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

PDF Tool APIサンプルコード:画像の描画

機能イメージ

PDF文書の指定ページ上の指定した矩形内に画像を描画します。

概要

サンプルコードの概要

PDF文書の指定ページ上の指定した矩形内に画像を描画します。
対応画像形式は、BMP/JPEG/PNG/GIF/TIFF/マルチTIFFです。

  • PtlContent.drawImage(PtlRect, PtlContent.ALIGN, PtlParamDrawImage) :画像の描画
  • PtlParamDrawImage.setImageStream(PtlParamInput) :入力画像ストリームの設定
  • PtlParamDrawImage.setImagePageNumber(int) :マルチTiffだった場合のページ番号の設定

サンプルコード

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

	概要:画像の描画

	Copyright 2015-2021 Antenna House, Inc.
*/

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

using namespace PdfTk;

void drawImage(PtlPage& page, const char* pathImage);

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

		PtlPDFDocument doc;

		// PDFファイルをロードします。
		doc.load(input);

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

		if (pages.isEmpty()){
			printf("ページコンテナが空\n");
			return 1;
		}

		// 先頭ページの取得
		PtlPage page = pages.get(0);

		// 画像の描画
		drawImage(page, argv[3]);

		// ファイルに保存します。
		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;
}

void drawImage(PtlPage& page, const char* pathImage)
{
	// 挿入する画像
	PtlParamInput insertImage(pathImage);

	// ページコンテントの取得
	PtlContent& content = page.getContent();

	// ページサイズ
	PtlSize pageSize = page.getSize();

	// 描画矩形
	PtlRect rect(10.0f, 10.0f, pageSize.getWidth()-10.0f, pageSize.getHeight()-10.0f);

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

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

	// マルチTiffのページ番号の設定(Tiffファイルにのみ有効) 
	paramDrawImage.setImagePageNumber(0);

	// 画像の描画
	content.drawImage(rect, PtlContent::ALIGN_TOP_LEFT, paramDrawImage);
}

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

    概要:画像の描画

    Copyright 2015-2021 Antenna House, Inc.
*/

package Sample;

import jp.co.antenna.ptl.*;

public class DrawImage {

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

        try (PtlParamInput inputFile = new PtlParamInput(args[0]);
             PtlParamOutput outputFile = new PtlParamOutput(args[1]);
             PtlPDFDocument doc = new PtlPDFDocument())
        {
            // PDFファイルをロードします。
            doc.load(inputFile);

            try (PtlPages pages = doc.getPages()) // ページコンテナの取得
            {
                // ページコンテナが空かどうか
                if (pages.isEmpty())
                {
                    System.out.println("ページコンテナが空");
                    return;
                }
                    
                // 1ページ目の取得
                try (PtlPage page = pages.get(0))
                {
                    // 画像の描画
                    drawImage(page, args[2]);
                }
            }

            // ファイルに保存します。
            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("-- 完了 --");
        }
    }

    public static void drawImage(PtlPage page, String pathImage) throws PtlException, Exception, Error
    {
        try (PtlContent content = page.getContent(); // ページコンテントの取得
             PtlSize pageSize = page.getSize()) // ページサイズ
        {
            try (PtlParamDrawImage paramDrawImage = new PtlParamDrawImage(); // 画像の描画に使うパラメータクラス
                 PtlParamInput insertImage = new PtlParamInput(pathImage);  //挿入する画像
                 PtlRect rect = new PtlRect(10.0f, 10.0f, pageSize.getWidth() - 10.0f, pageSize.getHeight() - 10.0f)) // 描画矩形
            {
                //入力画像ストリームの設定
                paramDrawImage.setImageStream(insertImage);

                //マルチTiffのページ番号の設定(Tiffファイルにのみ有効) 
                paramDrawImage.setImagePageNumber(0);

                // 画像の描画
                content.drawImage(rect, PtlContent.ALIGN.ALIGN_TOP_LEFT, paramDrawImage);
            }
        }
    }
}

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

	概要:画像の描画

	Copyright 2013-2021 Antenna House, Inc.
*/

using System;
using PdfTkNet;

namespace DrawImage
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 3)
            {
                Console.WriteLine("usage: DrawImage.exe in-pdf-file out-pdf-file insert-image-file");
                return;
            }

            try
            {
                using (PtlParamInput inputFile = new PtlParamInput(args[0]))    //元PDF
                using (PtlParamOutput outputFile = new PtlParamOutput(args[1])) //出力PDF
                using (PtlPDFDocument doc = new PtlPDFDocument())
                {
                    // PDFファイルをロードします。
                    doc.load(inputFile);

                    using (PtlPages pages = doc.getPages())
                    {
                        //ページコンテナが空かどうか
                        if (pages.isEmpty())
                        {
                            Console.WriteLine("ページコンテナが空");
                            return;
                        }

                        using (PtlPage page = pages.get(0)) // 先頭ページ
                        {
                            // パスの描画
                            drawImage(page, args[2]);
                        }
                    }

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

        static void drawImage(PtlPage page, string pathImage)
        {
            using (PtlContent content = page.getContent()) // ページコンテントの取得
            using (PtlSize pageSize = page.getSize()) // ページサイズ
            {
                using (PtlParamDrawImage paramDrawImage = new PtlParamDrawImage()) // 画像の描画に使うパラメータクラス
                using (PtlParamInput insertImage = new PtlParamInput(pathImage))  //挿入する画像
                using (PtlRect rect = new PtlRect(10.0f, 10.0f, pageSize.getWidth() - 10.0f, pageSize.getHeight() - 10.0f)) // 描画矩形
                {
                    //入力画像ストリームの設定
                    paramDrawImage.setImageStream(insertImage);

                    //マルチTiffのページ番号の設定(Tiffファイルにのみ有効) 
                    paramDrawImage.setImagePageNumber(0);

                    // 画像の描画
                    content.drawImage(rect, PtlContent.ALIGN.ALIGN_TOP_LEFT, paramDrawImage);
                }
            }
        }
    }
}

            

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

実行例

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

DrawImage.exe C:\in\in.pdf C:\sav\outDrawImage.pdf C:\in\icon.jpg
完了!
java -jar DrawImage.jar C:\in\in.pdf C:\sav\outDrawImage.icon C:\in\test.jpg
-- 完了 --
DrawImage.exe C:\in\in.pdf C:\sav\outDrawImage.pdf C:\in\icon.jpg
-- 完了 --

出力結果イメージ

出力されたPDFの1ページ目に指定した画像が描画されている

出力イメージ

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

サンプルコード

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