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

PDF Tool APIサンプルコード:フォームの描画

機能イメージ

PPDF文書のページにフォームとしてPDF文書のページを描画します。

概要

サンプルコードの概要

PDF文書のページにフォームとしてPDF文書のページを描画します。

  • PtlContent :ページに描画される内容(コンテント)を表現するクラス
  • PtlContent.drawForm() :フォームとしてページを描画

サンプルコード

/*
	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 drawForm(PtlPage& page, const char* pathPdf);

int main(int argc, char* argv[])
{
	if (argc < 4) {
		printf("usage: DrawForm.exe in-pdf-file out-pdf-file in-pdf-file2\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);

		// フォームの描画
		drawForm(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 drawForm(PtlPage& page, const char* pathPdf)
{
	// ページコンテントの取得
	PtlContent& content = page.getContent();

	// 描画矩形
	PtlRect rect(0.0f, 0.0f, 100.0f, 100.0f);

	// 挿入するPDF
	PtlParamInput input2(pathPdf);

	PtlPDFDocument doc2;

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

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

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

	// フォームの描画
	content.drawForm(rect, PtlContent::ALIGN_CENTER, pageInsert);
}

            
/*
    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 DrawForm {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 3)
        {
            System.out.println("usage: java DrawForm in-pdf-file out-pdf-file in-pdf-file2");
            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))
                {
                    // フォームの描画
                    drawForm(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 drawForm(PtlPage page, String pathPDF) throws PtlException, Exception, Error
    {
        try (PtlContent content = page.getContent(); // ページコンテントの取得
             PtlParamInput inputFile2 = new PtlParamInput(pathPDF);  //挿入するPDF
             PtlPDFDocument doc2 = new PtlPDFDocument();
             PtlRect rect = new PtlRect(0.0f, 0.0f, 100.0f, 100.0f)) // 描画矩形
        {
            // PDFファイルをロードします。
            doc2.load(inputFile2);

            try (PtlPages pages2 = doc2.getPages()) // ページコンテナの取得
            {
                // ページコンテナが空かどうか
                if (pages2.isEmpty())
                {
                    System.out.println("ページコンテナが空");
                    return;
                }

                // 1ページ目の取得
                try (PtlPage pageInsert = pages2.get(0))
                {
                    // フォームの描画
                    content.drawForm(rect, PtlContent.ALIGN.ALIGN_CENTER, pageInsert);
                }
            }
        }
    }
}

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

	概要:フォームの描画

	Copyright 2015-2021 Antenna House, Inc.
*/

using System;
using PdfTkNet;

namespace DrawForm
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 3)
            {
                Console.WriteLine("usage: DrawForm.exe in-pdf-file out-pdf-file in-pdf-file2");
                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)) // 先頭ページ
                        {
                            // フォームの描画
                            drawForm(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 drawForm(PtlPage page, string pathPdf)
        {
            using (PtlContent content = page.getContent()) // ページコンテントの取得
            using (PtlParamInput inputFile2 = new PtlParamInput(pathPdf))  //挿入するPDF
            using (PtlRect rect = new PtlRect(0.0f, 0.0f, 100.0f, 100.0f)) // 描画矩形
            using (PtlPDFDocument doc2 = new PtlPDFDocument())
            {
                // PDFファイルをロードします。
                doc2.load(inputFile2);

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

                    using (PtlPage pageInsert = pages2.get(0)) // 先頭ページ
                    {
                        // フォームの描画
                        content.drawForm(rect, PtlContent.ALIGN.ALIGN_CENTER, pageInsert);
                    }
                }
            }
        }
    }
}

            

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

実行例

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

DrawForm.exe C:\in\in.pdf C:\sav\outDrawForm.pdf C:\in\imagetopdf1.pdf
完了!
java -jar DrawForm.jar C:\in\in.pdf C:\sav\outDrawForm.pdf C:\in\imagetopdf1.pdf
-- 完了 --
DrawForm.exe C:\in\in.pdf C:\sav\outDrawForm.pdf C:\in\imagetopdf1.pdf
-- 完了 --

出力結果イメージ

出力されたPDFの1ページ目にフォームとして挿入するPDFの1ページ目が表示されている。

出力イメージ

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

サンプルコード

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