複数のPDFと画像からページを組み立て新たなPDFを作成します。
ニーズに合わせて構成できる複製オプションを提供することで生成するPDFを完全に制御できます。
APIリファレンス(英文)はこちらです。
Pdftools SDK の DocumentAssemblerクラス(C#、C/C++)を使用して複数のPDFファイルと画像を出力PDFに結合します。
たとえば、顧客がアップロードしたドキュメントと画像などをさらに処理したり保存したりするのに適したPDFに変換します。
この処理ではフォントなどの重複したリソースが適切に結合され、結果PDFのサイズが大幅に削減されます。
DocumentAssemblerクラスはフォームなどの名前付き要素間の競合を自動的に解決します。
Pdftools SDK の DocumentAssemblerクラス(C#、C/C++)を使用して、単一のPDFを複数のPDFと画像に分割します。
たとえば、各ページが異なる顧客の請求書を表すPDFを顧客ごとのPDFにまとめることができます。
この処理では各ページに必要なリソースだけがそのページを含む出力ドキュメントにコピーされるます。そのため、結果のPDFに冗長な情報や機密情報が含まれる可能性がなくなります。
複数の入力PDFを多数の出力PDFに分割および再結合できますので、PDFを効率的に再構成できます。
C#のサンプルプロジェクトではPdftools SDKライブラリ(DLL)をNuGetから自動でダウンロードします。
CのサンプルプロジェクトにはPdftools SDKライブラリ(DLL)が含まれています。
ライセンスキー無し(無償)で試用できます。ただし、結果に「透かし」が入ります。
「透かし」の削除をご希望の場合は問い合わせページまたはメールでお問い合わせください。
License Agreement(利用許諾契約書)が含まれていますので必ず確認してください。
複数の PDF文書を一つの出力ドキュメントに結合します。
// 出力ストリームを生成 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());
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ファイルを開く 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()); }
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あてにお送りください。
ご購入前の技術的質問も無償で対応します。サポート受付ページからお願いします。