アイコン 株式会社トラスト・ソフトウェア・システム
電話:03-5316-3375info@trustss.co.jp
電話:03-5316-3375info@trustss.co.jp

開発者向けPDFライブラリ - Pdftools SDK

Marge & Split PDF(PDFの合成と分割)機能

PDFの合成と分割機能

複数のPDFと画像からページを組み立て新たなPDFを作成します。
ニーズに合わせて構成できる複製オプションを提供することで生成するPDFを完全に制御できます。

価格見積もり

 NOTE:
この機能は「Pdftools SDK」ライブラリの一部です。
Pdftools SDKの全機能は無償で試用できます。

APIリファレンス

APIリファレンス(英文)はこちらです。

PDFを合成 機能

Pdftools SDK の DocumentAssemblerクラス(C#C/C++)を使用して複数のPDFファイルと画像を出力PDFに結合します。
たとえば、顧客がアップロードしたドキュメントと画像などをさらに処理したり保存したりするのに適したPDFに変換します。

この処理ではフォントなどの重複したリソースが適切に結合され、結果PDFのサイズが大幅に削減されます。
DocumentAssemblerクラスはフォームなどの名前付き要素間の競合を自動的に解決します。

PDFを分割 機能

Pdftools SDK の DocumentAssemblerクラス(C#C/C++)を使用して、単一のPDFを複数のPDFと画像に分割します。
たとえば、各ページが異なる顧客の請求書を表すPDFを顧客ごとのPDFにまとめることができます。

この処理では各ページに必要なリソースだけがそのページを含む出力ドキュメントにコピーされるます。そのため、結果のPDFに冗長な情報や機密情報が含まれる可能性がなくなります。

PDFの結合と分割の組み合わせる

複数の入力PDFを多数の出力PDFに分割および再結合できますので、PDFを効率的に再構成できます。

サンプル

C#のサンプルプロジェクトではPdftools SDKライブラリ(DLL)をNuGetから自動でダウンロードします。
CのサンプルプロジェクトにはPdftools SDKライブラリ(DLL)が含まれています。

ライセンスキー無し(無償)で試用できます。ただし、結果に「透かし」が入ります。
「透かし」の削除をご希望の場合は問い合わせページまたはメールでお問い合わせください。

License Agreement(利用許諾契約書)が含まれていますので必ず確認してください。

PDF文書を合成

複数の PDF文書を一つの出力ドキュメントに結合します。


サンプル・プロジェクト(C)をダウンロード ReadMeを開く
サンプルプロジェクト(C#)をダウンロード ReadMeを開く
サンプルプロジェクト(Python)をダウンロード ReadMeを開く
サンプル・プロジェクトをダウンロード
サンプル・プロジェクトの実行手順を参照してください
// 出力ストリームを生成
pOutStream = _tfopen(szOutPath, _T("wb+"));
GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutStream, _T("Failed to create output file \"%s\" for writing.\n"), szOutPath);
TPdfToolsSys_StreamDescriptor outDesc;
PdfToolsSysCreateFILEStreamDescriptor(&outDesc, pOutStream, 0);
pAssembler = PdfToolsDocumentAssembly_DocumentAssembler_New(&outDesc, NULL, NULL);

for (int i = 1; i < argc - 1; i++)
{
    szInPath = argv[i];
    // 入力PDFファイルを開く
    pInStream = _tfopen(szInPath, _T("rb"));
    GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pInStream, _T("Failed to open the input file \"%s\" for reading.\n"),
                                     szInPath);
    TPdfToolsSys_StreamDescriptor inDesc;
    PdfToolsSysCreateFILEStreamDescriptor(&inDesc, pInStream, 0);
    pInDoc = PdfToolsPdf_Document_Open(&inDesc, _T(""));
    GOTO_CLEANUP_IF_NULL_PRINT_ERROR(
        pInDoc, _T("Failed to create a document from the input file \"%s\". %s (ErrorCode: 0x%08x).\n"), szInPath,
        szErrorBuff, PdfTools_GetLastError());

    // 入力PDF文書の内容を出力文書に追加
    GOTO_CLEANUP_IF_FALSE_PRINT_ERROR(
        PdfToolsDocumentAssembly_DocumentAssembler_Append(pAssembler, pInDoc, NULL, NULL, NULL, NULL),
        _T("Failed to append a page. (ErrorCode: 0x%08x).\n"), PdfTools_GetLastError());

    PdfToolsPdf_Document_Close(pInDoc);
    fclose(pInStream);
    pInDoc    = NULL;
    pInStream = NULL;
}
// 入力文書を出力文書に結合
pOutDoc = PdfToolsDocumentAssembly_DocumentAssembler_Assemble(pAssembler);

GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutDoc, _T("The processing has failed. (ErrorCode: 0x%08x).\n"),
                                 PdfTools_GetLastError());
サンプル・プロジェクトの実行手順を参照してください
 サンプルで使うクラスなどの日本語APIリファレンス
private static void Merge(IEnumerable<string> inPaths, string outPath)
{
    // 出力ストリームを生成
    using var outStream = File.Create(outPath);
    using var docAssembler = new PdfTools.DocumentAssembly.DocumentAssembler(outStream);

    foreach (var inPath in inPaths)
    {
        using var inStream = File.OpenRead(inPath);
        using var inDoc = PdfTools.Pdf.Document.Open(inStream);
        // 入力PDF文書の内容を出力文書に追加
        docAssembler.Append(inDoc);
    }

    // 入力文書を出力文書に結合
    docAssembler.Assemble();
}			
サンプル・プロジェクトの実行手順を参照してください
def merge(input_paths: str, output_path: str):
    # 出力ストリームを生成
    with io.FileIO(output_path, 'wb+') as output_stream:
        with DocumentAssembler(output_stream, None, None) as assembler:

            for input_path in input_paths:
                with open(input_path, 'rb') as input_stream:
                    with Document.open(input_stream) as input_document:
                        # 入力PDF文書の内容を出力文書に追加
                        assembler.append(input_document)

            # 入力文書を出力文書に結合
            assembler.assemble()			
merge(input_paths, output_path)			

PDF文書を分割

一つのPDF文書を複数のPDF文書に分割します。


サンプル・プロジェクト(C)をダウンロード ReadMeを開く
サンプル・プロジェクト(C#)をダウンロード ReadMeを開く
サンプル・プロジェクト(Python)をダウンロード ReadMeを開く
サンプル・プロジェクトをダウンロード
サンプル・プロジェクトの実行手順を参照してください
// 入力PDFファイルを開く
pInStream = _tfopen(szInPath, _T("rb"));
GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pInStream, _T("Failed to open the input file \"%s\" for reading.\n"), szInPath);
TPdfToolsSys_StreamDescriptor inDesc;
PdfToolsSysCreateFILEStreamDescriptor(&inDesc, pInStream, 0);
pInDoc = PdfToolsPdf_Document_Open(&inDesc, _T(""));

GOTO_CLEANUP_IF_NULL_PRINT_ERROR(
    pInDoc, _T("Failed to create a document from the input file \"%s\". %s (ErrorCode: 0x%08x).\n"), szInPath,
    szErrorBuff, PdfTools_GetLastError());

// 入力PDF文書をページごとに分割し、それぞれを一つの出力文書PDFとして生成
int   nPageCount = PdfToolsPdf_Document_GetPageCount(pInDoc);
TCHAR szPageFileName[256];
for (int i = 1; i <= nPageCount; i++)
{
    CreateOutputFileName(szPageFileName, szOutPath, i);

    // 入力PDF文書の各ページを出力するストリームを作成
    pOutStream = _tfopen(szPageFileName, _T("wb+"));
    GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutStream, _T("Failed to open the input file \"%s\" for reading.\n"),
                                     szPageFileName);
    TPdfToolsSys_StreamDescriptor outDesc;
    PdfToolsSysCreateFILEStreamDescriptor(&outDesc, pOutStream, 0);

    // PDFをページに分割
    pAssembler = PdfToolsDocumentAssembly_DocumentAssembler_New(&outDesc, NULL, NULL);
    GOTO_CLEANUP_IF_FALSE_PRINT_ERROR(
        PdfToolsDocumentAssembly_DocumentAssembler_Append(pAssembler, pInDoc, &i, &i, NULL, NULL),
        _T("Failed to append a page. (ErrorCode: 0x%08x).\n"), PdfTools_GetLastError());
    pOutDoc = PdfToolsDocumentAssembly_DocumentAssembler_Assemble(pAssembler);
    PdfToolsDocumentAssembly_DocumentAssembler_Close(pAssembler);

    if (pOutDoc)
        PdfToolsPdf_Document_Close(pOutDoc);
    if (pOutStream)
        fclose(pOutStream);
    GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutDoc, _T("The processing has failed. (ErrorCode: 0x%08x).\n"),
                                     PdfTools_GetLastError());
}
			
サンプル・プロジェクトの実行手順を参照してください
 サンプルで使うクラスなどの日本語APIリファレンス
private static void Split(string inPath, string outPathPrefix)
{
    // 入力PDFファイルを開く
    using var inStream = File.OpenRead(inPath);
    using var inDoc = PdfTools.Pdf.Document.Open(inStream);

    // 入力文書をページごとに分割
    for (int i = 1; i <= inDoc.PageCount; ++i)
    {
        using var outStream = File.Create(outPathPrefix + "_page_" + i + ".pdf");
        using var docAssembler = new PdfTools.DocumentAssembly.DocumentAssembler(outStream);
        docAssembler.Append(inDoc, i, i);
        docAssembler.Assemble();
    }
}			
サンプル・プロジェクトの実行手順を参照してください
def split_pdf(input_file_path: str, output_file_path: str):
    # 入力PDFファイルを開く
    with open(input_file_path, 'rb') as input_stream:
        with Document.open(input_stream) as input_document:
            # 入力文書をページごとに分割
            for i in range(1, input_document.page_count + 1):
                current_out_file = construct_file_name(output_file_path, i)
                with open(current_out_file, 'wb+') as output_stream:
                    with DocumentAssembler(output_stream, None, None) as assembler:
                        assembler.append(input_document, i, i)
                        assembler.assemble()			
# 入力パスと入力ドキュメントのページ番号からファイル名を構築
def construct_file_name(input_file: str, page_number: int):
    # 入力パスからディレクトリとファイル名を分割
    directory, basename = os.path.split(input_file)

    # ファイルのベース名と拡張子を分割
    base, extension = os.path.splitext(basename)

    return os.path.join(directory, f"{base}_page_{page_number}{extension}")			
split_pdf(input_path, output_path)			

他の機能サンプルを参照してください。

お問い合わせ、ご質問、技術サポート

質問のページからお送りいただくようお願いします。
または、メールでsupport@trustss.co.jpあてにお送りください。


ご購入前の技術的質問も無償で対応します。サポート受付ページからお願いします。

> PDF Structure (PDF構成)

> PDF Imager-LP (画像化)

> PDF Stamper (電子印鑑)

> Pdftools SDK

- サンプル・コード
- Pdftools SDKサンプルの利用手順
- Toolbox Add-on
- Toolbox Add-onサンプルの利用手順
> Pdftools SDK APIリファレンス
- その他のAPI及びコマンドラインツール
> PDF SDK オープンソースと有償ライブラリ