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

PDF Tool APIサンプルコード:ページ割り付け

機能イメージ

PDF文書の4ページを新しいPDF文書の1ページに割り付け(面付け:4in1)します。

概要

サンプルコードの概要

新しいA4版サイズのPDF文書を用意し、各ページの上に、その高さと幅を半分にした配置矩形を上下左右に4つ設定します。
元のPDF文書を1ページから順にコピーして、新しいPDF文書の配置矩形に順番に貼り付けます。
新しいPDF文書では1ページの上に元のPDF文書が4ページずつ割り付けられます。

  • PtlContent.drawForm(PtlRect, PtlContent.ALIGN, PtlPage) :フォームとしてページを描画
  • PtlPDFDocument.getPages() :ページコンテナの取得
  • PtlPages.append(PtlPage, int) :最後にページを追加する
  • PtlPage.setViewBox(PtlRect) :表示矩形を設定
  • PtlPages.get(int index) :指定位置にあるページを取得
  • PtlPage.getContent() :ページコンテントの取得

サンプルコード

/*
	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;

int main(int argc, char* argv[])
{
	if (argc < 3) {
		printf("usage: AllocatePages.exe in-pdf-file out-pdf-file\n");
		return 1;
	}
	try
	{
		float width = 210.0f; // A4横長
		float height = 297.0f;// A4縦長
		PtlRect rectA4V(0.0f, 0.0f, width, height);	// A4縦用紙
		PtlRect rect[4] = {PtlRect(0.0f, height/2, width/2, height),  // 上左
			               PtlRect(width/2, height/2, width, height), // 上右
						   PtlRect(0.0f, 0.0f, width/2, height/2),    // 下左
						   PtlRect(width/2, 0.0f, width, height/2)};  // 下右
		PtlParamInput input(argv[1]);
		PtlParamOutput output(argv[2]);
		PtlPDFDocument doc;
		PtlPDFDocument docNew;

		// 新規PDFのページコンテナの取得
		PtlPages& pagesNew = docNew.getPages();

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

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

		int numPages = doc.getPageCount();

		int pageNumber = 0;
		int i=0;
		while (i < numPages) {
			// 割り付けを行うA4縦ページ
			PtlPage pageTemp;
			pageTemp.setViewBox(rectA4V);
			// ページを文書に追加
			pagesNew.append(pageTemp, PtlPages::OPTION_NONE);

			// 文書からページの取得
			PtlPage pageNew = pagesNew.get(pageNumber);

			// 割り付けを行うA4縦ページのコンテント取得
			PtlContent& content = pageNew.getContent();

			// 4ページを1ページに割り付けます。
			for(int j=0; j < 4 && i < numPages; ++j,++i) {
				// 割り付けするページ
				PtlPage page = pages.get(i);
				// ページ割り付け
				content.drawForm(rect[j], PtlContent::ALIGN_CENTER, page);
			}

			++pageNumber;
		}

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

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

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

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

        float width = 210.0f;  // A4横長
        float height = 297.0f; // A4縦長
        
        PtlRect rect[] = null;
        
        try (PtlParamInput inputFile = new PtlParamInput(args[0]);
             PtlParamOutput outputFile = new PtlParamOutput(args[1]);
             PtlPDFDocument doc = new PtlPDFDocument();
             PtlPDFDocument docNew = new PtlPDFDocument())
        {
            // PDFファイルをロード
            doc.load(inputFile);

            try (PtlPages pagesNew = docNew.getPages(); //新規PDFページコンテナの取得
                 PtlPages pages = doc.getPages(); //入力PDFページコンテナの取得
                 PtlRect rectA4V = new PtlRect(0.0f, 0.0f, width, height))// A4縦用紙
            {
                rect = new PtlRect[] {new PtlRect(0.0f, height/2, width/2, height),  // 上左
                                      new PtlRect(width/2, height/2, width, height), // 上右
                                      new PtlRect(0.0f, 0.0f, width/2, height/2),    // 下左
                                      new PtlRect(width/2, 0.0f, width, height/2)};  // 下右
                int numPages = doc.getPageCount();
                int pageNumber = 0;
                int i = 0;

                while (i < numPages) {
                    try (PtlPage pageTemp = new PtlPage()) // 割り付けを行うA4縦ページ
                    {
                        // 割り付けを行うA4縦ページの追加
                        pagesNew.append(pageTemp, PtlPages.OPTION_NONE);

                        // 追加したページの取得
                        try (PtlPage pageNew = pagesNew.get(pageNumber))
                        {
                            pageNew.setViewBox(rectA4V);

                            // 割り付けを行うA4縦ページのコンテント取得
                            try (PtlContent content = pageNew.getContent())
                            {
                                // 4ページを1ページに割り付ける
                                for(int j=0; j < 4 && i < numPages; ++j,++i) {
                                    // 割り付けするページ
                                    try (PtlPage page = pages.get(i))
                                    {
                                        // ページ割り付け
                                        content.drawForm(rect[j], PtlContent.ALIGN.ALIGN_CENTER, page);
                                    }
                                }
                            }
                        }
                        ++pageNumber;
                    }
                }
            }
            finally {
                if ( rect != null ) 
                {
                    for(int i=0; i < 4; ++i) {
                        rect[i].close();
                    }
                }
            }
            // ファイルに保存します。
            docNew.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 V7.0
	.NET Interface sample program

	概要:ページ割り付け

	Copyright 2015-2021 Antenna House, Inc.
*/

using System;
using PdfTkNet;

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

            PtlRect[] rect = null;

            try
            {
                float width = 210.0f;  // A4横長
                float height = 297.0f; // A4縦長

                rect = new PtlRect[] { new PtlRect(0.0f, height / 2, width / 2, height),  // 上左
                                       new PtlRect(width / 2, height / 2, width, height), // 上右
                                       new PtlRect(0.0f, 0.0f, width / 2, height / 2),    // 下左
                                       new PtlRect(width / 2, 0.0f, width, height / 2) }; // 下右

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

                    using (PtlPages pagesNew = docNew.getPages()) //新規PDFページコンテナの取得
                    using (PtlPages pages = doc.getPages()) //入力PDFページコンテナの取得
                    using (PtlRect rectA4V = new PtlRect(0.0f, 0.0f, width, height)) // A4縦用紙
                    {
                        int numPages = doc.getPageCount();
                        int pageNumber = 0;
                        int i = 0;

                        while (i < numPages)
                        {
                            using (PtlPage pageTemp = new PtlPage()) // 割り付けを行うA4縦ページ
                            {
                                // 割り付けを行うA4縦ページの追加
                                pagesNew.append(pageTemp, PtlPages.INSERT_OPTION.OPTION_NONE);

                                // 追加したページの取得
                                using (PtlPage pageNew = pagesNew.get(pageNumber))
                                {
                                    pageNew.setViewBox(rectA4V);

                                    // 割り付けを行うA4縦ページのコンテント取得
                                    using (PtlContent content = pageNew.getContent())
                                    {
                                        // 4ページを1ページに割り付けます。
                                        for (int j = 0; j < 4 && i < numPages; ++j, ++i)
                                        {
                                            // 割り付けするページ
                                            using (PtlPage page = pages.get(i))
                                            {
                                                // ページ割り付け
                                                content.drawForm(rect[j], PtlContent.ALIGN.ALIGN_CENTER, page);
                                            }
                                        }
                                    }
                                }
                            }
                            ++pageNumber;
		                }
                    }
                    // ファイルに保存します。
                    docNew.save(outputFile);
                }
            }
            catch (PtlException pex)
            {
                Console.WriteLine(pex.getErrorCode() + " : " + pex.getErrorMessageJP());
                pex.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (rect != null)
                {
                    for (int i = 0; i < rect.Length; ++i)
                    {
                        rect[i].Dispose();
                    }
                }
                Console.WriteLine("-- 完了 --");
            }
        }
    }
}

            

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

実行例

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

AllocatePages.exe c:\in\AllocatePages.pdf c:\sav\outAllocatePages.pdf
-- 完了 --
java -jar AllocatePages.jar c:\in\AllocatePages.pdf c:\sav\outAllocatePages.pdf
-- 完了 --
AllocatePages.exe c:\in\AllocatePages.pdf c:\sav\outAllocatePages.pdf
-- 完了 --

出力結果イメージ

入力PDF(AllocatePages.pdf)はA5サイズです。これを出力PDFでは4ページ毎にA4の1ページに配置しています。

出力イメージ

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

サンプルコード

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