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

PDF Tool APIサンプルコード:ページ抽出

機能イメージ

PDF文書から指定した範囲のページを抽出して 新しいPDF文書として保存します。

概要

サンプルコードの概要

PDF文書の指定されたページを新しいPDF文書として出力します。

  • PtlPDFDocument.getDocProperty() :文書プロパティの取得
  • PtlDocProperty :PDFの文書プロパティを表現したクラス
  • PtlDocProperty.getDocInfo() :文書情報の取得
  • PtlDocInfo :PDFの文書情報を表現したクラス
  • PtlDocInfo.getTitle/setTitle(), getAuthor/setAuthor()など :タイトル、作成者などを出力側にコピー
  • PtlPDFDocument.getPages() :ページコンテナの取得
  • PtlPages :ページのコンテナを表現するクラス
  • PtlPages.append() :ページの追加

サンプルコード

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

	概要:ページ抽出

	Copyright 2013-2021 Antenna House, Inc.
*/

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

using namespace PdfTk;

int main(int argc, char* argv[])
{
	if (argc < 3) {
		printf("usage: ExtractPage.exe in-pdf-file out-folder\n");
		return 1;
	}
	try
	{
		const int MAX_PATH = 260;
		char outputfile[MAX_PATH];

		PtlParamString inputfile(argv[1]);		// 抽出元となるファイル名
		PtlParamString outputfolder(argv[2]);	// 出力するフォルダ名

		PtlParamInput input(inputfile);
		PtlPDFDocument doc;

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

		// 文書プロパティの取得
		PtlDocProperty& docproperty = doc.getDocProperty();

		// 文書情報の取得
		PtlDocInfo& docinfo = docproperty.getDocInfo();

		for(int i=0; i < doc.getPageCount(); i++)
		{
			PtlPDFDocument doc_ext;
			PtlPages& pages = doc_ext.getPages();

			// 文書プロパティの取得
			PtlDocProperty& docproperty_ext = doc_ext.getDocProperty();

			// 文書情報の取得
			PtlDocInfo& docinfo_ext = docproperty_ext.getDocInfo();

			// タイトルをコピー
			docinfo_ext.setTitle(docinfo.getTitle());

			// 著者をコピー
			docinfo_ext.setAuthor(docinfo.getAuthor());

			// サブジェクトをコピー
			docinfo_ext.setSubject(docinfo.getSubject());

			// キーワードをコピー
			docinfo_ext.setKeywords(docinfo.getKeywords());

			// クリエータをコピー
			docinfo_ext.setCreator(docinfo.getCreator());

			// プロデューサをコピー
			docinfo_ext.setProducer(docinfo.getProducer());

			// 作成日付をコピー
			docinfo_ext.setCreationDate(docinfo.getCreationDate());

			// 更新日付をコピー
			docinfo_ext.setModDate(docinfo.getModDate());

			// ページ挿入オプション
			// OPTION_COPY_OUTLINES = 0x00000004 ページ挿入時にあわせてしおりをコピーします。
			// OPTION_COPY_ATTACHEDFILES	= 0x00000008ページ挿入時にあわせて添付ファイルをコピーします。
			int insertoption = (PtlPages::OPTION_COPY_OUTLINES | PtlPages::OPTION_COPY_ATTACHEDFILES);

			// ページの追加(iページから1P)
			pages.append(doc, i, 1, insertoption);

			// 出力ファイル名
		#ifdef WIN32
			sprintf_s(outputfile, "%s\\output_%d.pdf", outputfolder.c_str(), i);
		#else
			sprintf_s(outputfile, "%s/output_%d.pdf", outputfolder.c_str(), i);
		#endif
			PtlParamOutput output(outputfile);

			// ファイルに保存します。
			doc_ext.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 V7.0
    Java Interface sample program

    概要:ページ抽出

    Copyright 2015-2021 Antenna House, Inc.
*/

package Sample;

import jp.co.antenna.ptl.*;

public class ExtractPage {

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

        try (PtlParamInput inputFile = new PtlParamInput(args[0]);
             PtlPDFDocument doc = new PtlPDFDocument())
        {
            // PDFファイルをロード
            doc.load(inputFile);

            try (PtlDocProperty docproperty = doc.getDocProperty();// 文書プロパティの取得
                 PtlDocInfo docinfo = docproperty.getDocInfo())    // 文書情報の取得
            {
                for (int i = 0; i < doc.getPageCount(); i++)
                {
                    try (PtlPDFDocument doc_ext = new PtlPDFDocument();
                         PtlDocProperty docproperty_ext = doc_ext.getDocProperty(); // 文書プロパティの取得
                         PtlDocInfo docinfo_ext = docproperty_ext.getDocInfo();     // 文書情報の取得
                         PtlPages pages = doc_ext.getPages())
                    {
                        // タイトルをコピー
                        docinfo_ext.setTitle(docinfo.getTitle());

                        // 著者をコピー
                        docinfo_ext.setAuthor(docinfo.getAuthor());

                        // サブジェクトをコピー
                        docinfo_ext.setSubject(docinfo.getSubject());

                        // キーワードをコピー
                        docinfo_ext.setKeywords(docinfo.getKeywords());

                        // クリエータをコピー
                        docinfo_ext.setCreator(docinfo.getCreator());

                        // プロデューサをコピー
                        docinfo_ext.setProducer(docinfo.getProducer());

                        // 作成日付をコピー
                        try (PtlDate dateCreate = docinfo.getCreationDate())
                        {
                            docinfo_ext.setCreationDate(dateCreate);
                        }

                        // 更新日付をコピー
                        try (PtlDate dateMod = docinfo.getModDate())
                        {
                            docinfo_ext.setModDate(dateMod);
                        }

                        // ページ挿入オプション
                        // OPTION_COPY_OUTLINES = 0x00000004 ページ挿入時にあわせてしおりをコピーします。
                        // OPTION_COPY_ATTACHEDFILES    = 0x00000008ページ挿入時にあわせて添付ファイルをコピーします。
                        int insertoption = PtlPages.OPTION_COPY_OUTLINES | PtlPages.OPTION_COPY_ATTACHEDFILES;

                        // ページの追加(iページから1P)
                        pages.append(doc, i, 1, insertoption);

                        // 出力ファイル名
                        String outputfile = (String)args[1] + "\\output_" + i + ".pdf";

                        try (PtlParamOutput outputFile = new PtlParamOutput(outputfile))
                        {
                            // ファイルに保存します。
                            doc_ext.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 2013-2021 Antenna House, Inc.
*/

using System;
using PdfTkNet;

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

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

                    // 文書プロパティの取得
                    using (PtlDocProperty docproperty = doc.getDocProperty())
                    {
                        // 文書情報の取得
                        PtlDocInfo docinfo = docproperty.getDocInfo();

                        for (int i = 0; i < doc.getPageCount(); i++)
                        {
                            // 出力ファイル名
                            string outputfile = (string)args[1] + @"\output_" + i + @".pdf";

                            using (PtlPDFDocument doc_ext = new PtlPDFDocument())
                            using (PtlPages pages = doc_ext.getPages())
                            using (PtlParamOutput output = new PtlParamOutput(outputfile))
                            using (PtlDocProperty docproperty_ext = doc_ext.getDocProperty())
                            using (PtlDocInfo docinfo_ext = docproperty_ext.getDocInfo())
                            {
                                // タイトルをコピー
                                docinfo_ext.setTitle(docinfo.getTitle());

                                // 著者をコピー
                                docinfo_ext.setAuthor(docinfo.getAuthor());

                                // サブジェクトをコピー
                                docinfo_ext.setSubject(docinfo.getSubject());

                                // キーワードをコピー
                                docinfo_ext.setKeywords(docinfo.getKeywords());

                                // クリエータをコピー
                                docinfo_ext.setCreator(docinfo.getCreator());

                                // プロデューサをコピー
                                docinfo_ext.setProducer(docinfo.getProducer());

                                // 作成日付をコピー
                                docinfo_ext.setCreationDate(docinfo.getCreationDate());

                                // 更新日付をコピー
                                docinfo_ext.setModDate(docinfo.getModDate());

                                // ページ挿入オプション
                                //OPTION_COPY_OUTLINES = 0x00000004 ページ挿入時にあわせてしおりをコピーします。
                                //OPTION_COPY_ATTACHEDFILES	= 0x00000008ページ挿入時にあわせて添付ファイルをコピーします。
                                PtlPages.INSERT_OPTION insertoption = (PtlPages.INSERT_OPTION.OPTION_COPY_OUTLINES
                                                    | PtlPages.INSERT_OPTION.OPTION_COPY_ATTACHEDFILES);

                                // ページの追加(iページから1P)
                                pages.append(doc, i, 1, insertoption);

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

            
AHPDFToolCmd70.exe -pageExtract 0,1,2,3,4,5,6,7 -divideFile -copyInfo –d c:\in\in.pdf -o c:\sav\outExtractPage\output.pdf

            

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

実行例

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

ExtractPage.exe C:\in\in.pdf C:\sav\outExtractPage
完了!
java -jar ExtractPage.jar C:\in\in.pdf C:\sav\outExtractPage
-- 完了 --
ExtractPage.exe C:\in\in.pdf C:\sav\outExtractPage
-- 完了 --
AHPDFToolCmd70.exe -pageExtract 0,1,2,3,4,5,6,7 -divideFile -copyInfo -d c:\in\in.pdf -o c:\sav\outExtractPage\output.pdf
 use time 0.112000s

出力結果イメージ

このサンプルでは1ページずつ別のPDFとして出力されます。

出力イメージ

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

サンプルコード

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