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

PDF Tool APIサンプルコード:ページを上下左右に分割

機能イメージ

PDF文書のページを上下または左右に分割します。

概要

サンプルコードの概要

入力PDF文書の表示矩形を取得して、指定方向の半分の長さを求めます。
PDFを複製して、複製後のPDF文書の表示矩形の座標値を、半分の長さを使って再設定します。
再設定ではViewBoxを用いることでMediaBox、CropBoxが表示矩形の値に設定されます。
本プログラムではBleedBox、Trim、ArtBoxは削除されます。

  • PtlPage :PDFのページオブジェクトを表現したクラス
  • PtlPage.getSize() :ページサイズを取得
  • PtlPage.getViewBox() :表示矩形を取得
  • PtlSize.getWidth(), getHeight() :幅を取得、高さを取得
  • PtlRect.setTop(), setBottom(), setLeft(), setRight() :上下左右の座標値を設定
  • PtlPage.setViewBox(PtlRect) :表示矩形を設定

サンプルコード

/*
	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 removeLinkAction(PtlPage& page);

int main(int argc, char* argv[])
{
	if (argc < 4) {
		printf("usage: DividePage.exe in-pdf-file out-pdf-file 分割方向\n\n");
		printf("分割方向\n0 : ページを上下に分割  1 : ページを左右に分割\n");
		return 1;
	}
	try
	{
		PtlParamInput input(argv[1]);
		PtlParamOutput output(argv[2]);

		PtlPDFDocument doc;

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

		// ページ数の取得
		int numPages = doc.getPageCount();
		printf("ページ数:%d\n", numPages);

		// ページコンテナの取得
		PtlPages& pages = doc.getPages();
		if (pages.isEmpty()){
			printf("ページコンテナが空\n");
			return 1;
		}

		const char* direction = argv[3];
		switch (direction[0]) {
		case '0':
		case '1':
			break;
		default:
			printf("usage: DividePage.exe in-pdf-file out-pdf-file 分割方向\n\n");
			printf("分割方向\n0 : ページを上下に分割  1 : ページを左右に分割\n");
			return 1;
		}

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

		for (int i=0; i < numPages; ++i) {
			// 読み込んだページの取得
			PtlPage page = pages.get(i);
			PtlSize size = page.getSize();
			float width = size.getWidth();
			float height = size.getHeight();
			// 読み込んだページの表示矩形取得
			PtlRect viewBox = page.getViewBox();

			// ページを分割していくので注釈のLinkアクションを削除しておく
			removeLinkAction(page);

			// 読み込んだページを新規PDFに2ページ分追加
			pagesNew.append(page, PtlPages::OPTION_NONE);
			pagesNew.append(page, PtlPages::OPTION_NONE);

			// 追加されたページを取得
			int numPagesNew = pagesNew.getCount();
			PtlPage pageNew1 = pagesNew.get(numPagesNew-2);
			PtlPage pageNew2 = pagesNew.get(numPagesNew-1);

			// 追加されたページに設定する表示矩形
			PtlRect rect1(viewBox);
			PtlRect rect2(viewBox);

			switch (direction[0]) {
			case '0':
				{
					// 半分の高さ
					float halfHeight = size.getHeight()/2;
					// 矩形の上半分
					rect1.setBottom(viewBox.getBottom()+halfHeight);
					// 矩形の下半分
					rect2.setTop(viewBox.getTop()-halfHeight);
				}
				break;
			case '1':
				{
					// 半分の幅
					float halfWidth = size.getWidth()/2;
					// 矩形の左半分
					rect1.setRight(viewBox.getRight()-halfWidth);
					// 矩形の右半分
					rect2.setLeft(viewBox.getLeft()+halfWidth);
				}
				break;
			}
			// 追加されたページの表示矩形を設定
			pageNew1.setViewBox(rect1);
			pageNew2.setViewBox(rect2);
		}

		// ファイルに保存します。
		docNew.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 removeLinkAction(PtlPage& page)
{
	if (!page.hasAnnots()) {
		return;
	}
	PtlAnnots& annots = page.getAnnots() ;
	int numAnnots = annots.getCount();
	for(int i=0; i < numAnnots; ++i) {
		PtlAnnot& annot = annots.get(i);
		switch (annot.getType()) {
		case PtlAnnot::TYPE_LINK:
			{
				PtlAnnotLink& annotLink = (PtlAnnotLink&)annot;
				PtlAction& action = annotLink.getAction();
				switch (action.getType()) {
				case PtlAction::TYPE_GOTO:
					annotLink.removeAction();
					break;
				default:
					break;
				}
			}
			break;
		default:
			break;
		}
	}
}

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

    概要:ページ分割

    Copyright 2015-2021 Antenna House, Inc.
*/

package Sample;

import java.io.*;
import jp.co.antenna.ptl.*;

public class DividePage {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 3)
        {
            System.out.println("usage: java DividePage in-pdf-file out-pdf-file 分割方向\n");
            System.out.println("分割方向\n0 : ページを上下に分割  1 : ページを左右に分割\n");
            return;
        }

        String direction = args[2];
        switch (direction) {
        case "0":
        case "1":
            break;
        default:
            return;
        }

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

            // ページ数の取得
            int numPages = doc.getPageCount();
            System.out.println("ページ数:" + numPages);

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

                for (int i = 0; i < numPages; i++)
                {
                    // 読み込んだページの取得
                    try (PtlPage page = pages.get(i);
                         PtlSize size = page.getSize();
                         PtlRect viewBox = page.getViewBox()) // 読み込んだページの表示矩形取得
                    {
                        float width = size.getWidth();
                        float height = size.getHeight();

                        // ページを分割していくので注釈のLinkアクションを削除しておく
                        removeLinkAction(page);

                        // 読み込んだページを新規PDFに2ページ分追加
                        pagesNew.append(page, PtlPages.OPTION_NONE);
                        pagesNew.append(page, PtlPages.OPTION_NONE);

                        int numPagesNew = pagesNew.getCount();
                        try (PtlPage pageNew1 = pagesNew.get(numPagesNew - 2); // 追加されたページを取得
                             PtlPage pageNew2 = pagesNew.get(numPagesNew - 1); // 追加されたページを取得
                             PtlRect rect1 = new PtlRect(viewBox); // 追加されたページに設定する表示矩形
                             PtlRect rect2 = new PtlRect(viewBox)) // 追加されたページに設定する表示矩形
                        {
                            switch (direction)
                            {
                            case "0":
                                {
                                    // 半分の高さ
                                    float halfHeight = size.getHeight() / 2;
                                    // 矩形の上半分
                                    rect1.setBottom(viewBox.getBottom() + halfHeight);
                                    // 矩形の下半分
                                    rect2.setTop(viewBox.getTop() - halfHeight);
                                }
                                break;
                            case "1":
                                {
                                    // 半分の幅
                                    float halfWidth = size.getWidth() / 2;
                                    // 矩形の左半分
                                    rect1.setRight(viewBox.getRight() - halfWidth);
                                    // 矩形の右半分
                                    rect2.setLeft(viewBox.getLeft() + halfWidth);
                                }
                                break;
                            }
                            // 追加されたページの表示矩形を設定
                            pageNew1.setViewBox(rect1);
                            pageNew2.setViewBox(rect2);
                        }
                    }
                }
            }

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

    public static void removeLinkAction(PtlPage page) throws PtlException, Exception, Error
    {
        if (!page.hasAnnots()) {
            return;
        }
        try (PtlAnnots annots = page.getAnnots())
        {
            int numAnnots = annots.getCount();
            for(int i=0; i < numAnnots; ++i) {
                try (PtlAnnot annot = annots.get(i))
                {
                    switch (annot.getType()) {
                    case TYPE_LINK:
                        {
                            PtlAnnotLink annotLink = (PtlAnnotLink)annot;
                            try (PtlAction action = annotLink.getAction())
                            {
                                switch (action.getType()) {
                                case TYPE_GOTO:
                                    annotLink.removeAction();
                                    break;
                                default:
                                    break;
                                }
                            }
                        }
                        break;
                    default:
                        break;
                    }
                }
            }
        }
    }
}

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

	概要:ページ分割

	Copyright 2015-2021 Antenna House, Inc.
*/

using System;
using PdfTkNet;

namespace DividePage
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 3)
            {
                Console.WriteLine("usage: DividePage.exe in-pdf-file out-pdf-file 分割方向\n");
                Console.WriteLine("分割方向\n0 : ページを上下に分割  1 : ページを左右に分割\n");
                return;
            }
            String direction = args[2];
            switch (direction)
            {
                case "0":
                case "1":
                    break;
                default:
                    Console.WriteLine("usage: DividePage.exe in-pdf-file out-pdf-file 分割方向\n");
                Console.WriteLine("分割方向\n0 : ページを上下に分割  1 : ページを左右に分割\n");
                    return;
            }

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

                    // ページ数の取得
                    int numPages = doc.getPageCount();
                    Console.WriteLine("ページ数:" + numPages);

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

                        for (int i = 0; i < numPages; i++)
                        {
                            // 読み込んだページの取得
                            using (PtlPage page = pages.get(i))
                            using (PtlSize size = page.getSize())
                            using (PtlRect viewBox = page.getViewBox()) // 読み込んだページの表示矩形取得
                            {
                                float width = size.getWidth();
                                float height = size.getHeight();

                                // ページを分割していくので注釈のLinkアクションを削除しておく
                                removeLinkAction(page);

                                // 読み込んだページを新規PDFに2ページ分追加
			                    pagesNew.append(page, PtlPages.INSERT_OPTION.OPTION_NONE);
                                pagesNew.append(page, PtlPages.INSERT_OPTION.OPTION_NONE);

                                int numPagesNew = pagesNew.getCount();
                                using (PtlPage pageNew1 = pagesNew.get(numPagesNew - 2)) // 追加されたページを取得
                                using (PtlPage pageNew2 = pagesNew.get(numPagesNew - 1)) // 追加されたページを取得
                                using (PtlRect rect1 = new PtlRect(viewBox)) // 追加されたページに設定する表示矩形
                                using (PtlRect rect2 = new PtlRect(viewBox)) // 追加されたページに設定する表示矩形
                                {
                                    switch (direction)
                                    {
                                        case "0":
                                            {
                                                // 半分の高さ
                                                float halfHeight = size.getHeight() / 2;
                                                // 矩形の上半分
                                                rect1.setBottom(viewBox.getBottom() + halfHeight);
                                                // 矩形の下半分
                                                rect2.setTop(viewBox.getTop() - halfHeight);
                                            }
                                            break;
                                        case "1":
                                            {
                                                // 半分の幅
                                                float halfWidth = size.getWidth() / 2;
                                                // 矩形の左半分
                                                rect1.setRight(viewBox.getRight() - halfWidth);
                                                // 矩形の右半分
                                                rect2.setLeft(viewBox.getLeft() + halfWidth);
                                            }
                                            break;
                                    }
                                    // 追加されたページの表示矩形を設定
                                    pageNew1.setViewBox(rect1);
                                    pageNew2.setViewBox(rect2);
                                }
                            }
                        }
                    }

                    // 別のファイルに保存します。
                    docNew.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 removeLinkAction(PtlPage page)
        {
            if (!page.hasAnnots())
            {
                return;
            }
            PtlAnnots annots = page.getAnnots();
            int numAnnots = annots.getCount();
            for (int i = 0; i < numAnnots; ++i)
            {
                PtlAnnot annot = annots.get(i);
                switch (annot.getType())
                {
                    case PtlAnnot.ANNOT_TYPE.TYPE_LINK:
                        {
                            PtlAnnotLink annotLink = (PtlAnnotLink)annot;
                            PtlAction action = annotLink.getAction();
                            switch (action.getType())
                            {
                                case PtlAction.ACTION_TYPE.TYPE_GOTO:
                                    annotLink.removeAction();
                                    break;
                                default:
                                    break;
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
        }
    }
}

            

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

実行例

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

DividePage.exe C:\in\in.pdf C:\sav\outDividePage.pdf 1
ページ数:8
完了!
java -jar DividePage.jar C:\in\in.pdf C:\sav\outDividePage.pdf 1
ページ数:8
-- 完了 --
DividePage.exe C:\in\in.pdf C:\sav\outDividePage.pdf 1
ページ数:8
-- 完了 --

出力結果イメージ

実行例では引数で左右に分割を指定したので出力されたPDFではページの左側と右側とが別のページとして出力されています。

出力イメージ

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

サンプルコード

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