Skip to main content
Interwork Corporation
IDR Solutions Product Support Portal
PDF開発用語集 モードの切替 ダーク/ライト/自動 モードの切替 ダーク/ライト/自動 モードの切替 ダーク/ライト/自動

PDFファイルの結合

v2025.05

JPedal は、PDFを簡単に1つのファイルにマージするいくつかの方法を提供します。このツールは、指定されたファイルのすべてのページを含む1つのファイルを作成します。元のファイルはこのプロセスで変更されません。

コマンドラインまたは他の言語からPDFを1つのファイルにマージする

PdfMergeは、コマンドから複数のファイルのページを新しい1つのファイルにマージするために使用できます。引数には、入力ファイル、オプション、出力ファイルが含まれます。 入力ファイルとオプションは任意の順序で指定でき、ファイルは引数に表示される順序でマージされます。 最後の引数は常にすべてのマージされたページの出力ファイルでなければなりません。

java -cp jpedal.jar org.jpedal.tools.PdfMerge [mergeOptions] inputFile1.pdf inputFile2.pdf... outputFile.pdf

Javaでのマージ

PdfMergeには、マージを実行するための一連の静的な便利なメソッドがあります。 これらのメソッドは、ツールの使用を迅速かつ簡単にするPdfMergeの一般的なユースケースをカバーしています。

2つのファイルのマージ これらのメソッドは2つのFileオブジェクトを受け取り、それらのPDFのページを1つのファイルにマージします。一部のメソッドではパスワードを指定したり、マージの異なる側面を制御するオプションを指定したりできます。

//Merge two files into a single file
PdfMerge.mergeFiles(new File("inputFile1.pdf"), new File("inputFile2.pdf"), new File("outputFile.pdf"));

//Merge two files with passwords into a single file
PdfMerge.mergeFiles(new File("inputFile1.pdf"), "inputFile1_Password", new File("inputFile2.pdf"), "inputFile2_Password", new File("outputFile.pdf"));

//Create an array of Merge Options
final ArrayList<MergeOption> options = new ArrayList<>();
//options.add(MergeOption.INTERLEAVE); //Example showing use of interleave option

//Merge two files into a single file with additional options
PdfMerge.mergeFiles(new File("inputFile1.pdf"), new File("inputFile2.pdf"), new File("outputFile.pdf"), options);

//Merge two files with passwords into a single file with additional options
PdfMerge.mergeFiles(new File("inputFile1.pdf"), "inputFile1_Password", new File("inputFile2.pdf"), "inputFile2_Password", new File("outputFile.pdf"), options);

ファイル配列のマージ これらのメソッドは、ファイル配列を受け取り、それらのPDFのページを1つのファイルにマージします。一部のメソッドでは、入力ファイル配列の長さと一致する文字列配列としてパスワードを指定できます。また、マージオプションを提供し、マージの異なる側面を制御できるメソッドもあります。

//Merge an array of PDFs into a single file
PdfMerge.mergeFiles(new File[]{new File("inputFile.pdf"), ...}, new File("outputFile.pdf"));

//Merge an array of PDFs with passwords into a single file
//Input File array and Password array must be the same length
PdfMerge.mergeFiles(new File[]{new File("inputFile.pdf"), ...}, new String[]{"inputFile_password", ...}, new File("outputFile.pdf"));

//Create an array of Merge Options
final ArrayList<MergeOption> options = new ArrayList<>();
//options.add(MergeOption.INTERLEAVE); //Example showing use of interleave option

//Merge an array of PDFs into a single file
PdfMerge.mergeFiles(new File[]{new File("inputFile.pdf"), ...}, new File("outputFile.pdf"), options);

//Merge an array of PDFs with passwords into a single file
//Input File array and Password array must be the same length
PdfMerge.mergeFiles(new File[]{new File("inputFile.pdf"), ...}, new String[]{"inputFile_password", ...}, new File("outputFile.pdf"), options);

PdfMergeのマージオプション

PdfMergeには、マージの異なる側面を制御できるいくつかのオプションがあります。これらは、マージメソッドに渡すか、コマンドラインで次のように設定できます。

区切りページの追加

PdfMergeは、最後の入力ドキュメント以外の各入力ドキュメントのページの後に空白ページを追加します。新しい黒ページは、その前のページのサイズと一致します。このオプションは、インターリーブと互換性がなく、インターリーブが選択された場合は何も行いません。

コマンドライン引数

--add_dividing_page

マージオプションの値

MergeOption.ADD_DIVIDING_PAGE

PdfMergeは、元のページのコンテンツの下に拡張するフッターを追加します。このフッターには、元のファイル名とそのページのページ番号が表示され、PdfMergeAttributesを使用してカスタマイズできます。

コマンドライン引数

--add_footer

マージオプションの値

MergeOption.ADD_FOOTER

インターリーブ

デフォルトでは、PdfMergeは最初のファイルからページを追加し、その後に続く各ファイルのすべてのページを追加します。インターリーブオプションを使用すると、ページは各ファイルから1ページずつ追加されます。 これにより、各ファイルの最初のページが順番に追加され、その後2ページ目が追加されます。マージするページがなくなったファイルがある場合、残りのファイルは同じルールに従って続けてマージされます。

コマンドライン引数

--interleave

マージオプションの値

MergeOption.INTERLEAVE

ページサイズの正規化

PdfMergeは、マージされるすべてのページのサイズを最初のドキュメントの最初のページに合わせて調整します。

コマンドライン引数

--normalise_page_size_to_match_first

マージオプションの値

MergeOption.NORMALISE_PAGE_SIZE_TO_MATCH_FIRST

ページ回転の正規化

PdfMergeは、マージされるすべてのページの回転を最初のドキュメントの最初のページに合わせて調整します。

コマンドライン引数

--normalise_rotation_to_match_first

マージオプションの値

MergeOption.NORMALISE_ROTATION_TO_MATCH_FIRST

PDFマージ属性の使用

PdfMergeには、結果のファイルに新しいコンテンツを作成するオプションがあります。出力の外観をより詳細に制御するために、マージ時に使用する属性を設定できます。これらの属性は、以下に詳述するキーと値のペアをコマンドラインに含めるか、PdfMergeAttributesオブジェクトをPdfMergeのインスタンスに渡すことで定義できます。

コマンドラインでの属性の追加

java -cp jpedal.jar org.jpedal.tools.PdfMerge [mergeOptions] [attributeKey aatributeValue...] inputFile1.pdf inputFile2.pdf... outputFile.pdf

#For Example, setting the font to use for any new text content
java -cp jpedal.jar org.jpedal.tools.PdfMerge [mergeOptions] --font_name Helevetica inputFile1.pdf inputFile2.pdf... outputFile.pdf

Javaでの属性の設定

final File[] inputFiles = //create your input file array here
final byte[][] passwords = //create your input file password array here
final File outputFile = //create your output file object here

final PdfMergeAttributes attributes = new PdfMergeAttributes();
attributes.setFontName("Helvetica");
attributes.setFontSize(16);

final PdfMerge pdfMerger = new PdfMerge();
pdfMerger.setPdfMergeAttributes(attributes);
pdfMerger.merge(inputFiles, outputFile, passwords);

PDFマージ属性

以下は、PdfMergeによって生成される新しいコンテンツをカスタマイズするために使用できる属性のリストです。このリストの各エントリには、属性の使用法、コマンドライン引数、およびPdfMergeAttributesオブジェクトで値を設定するメソッドが詳述されています。

フォント名

フォント名は、マージ時に生成される新しいテキストコンテンツで使用されるフォントを指定します。この属性の値は、14のPDF基本フォントのいずれかに限定されます。

コマンドライン引数 キー: –font_name : [Courier, Courier-Bold, Courier-Oblique, Courier-BoldOblique, Helvetica, Helvetica-Bold, Helvetica-Oblique, Helvetica-BoldOblique, Times-Roman, Times-Bold, Times-Italic, Times-BoldItalic, Symbol, ZapfDingbats] コマンドライン例:

--font_name Helvetica

PdfMergeAttribute メソッド

PdfMergeAttributes.setFontName(String fontname);

フォントサイズ

フォントサイズは、マージ時に生成される新しいテキストコンテンツのテキストサイズを指定します。この属性の値は、正の整数である必要があります。

コマンドライン例 キー: –font_size : テキストフォントサイズの正の整数 コマンドライン引数:

--font_size 16

PdfMergeAttribute メソッド

PdfMergeAttributes.setFontSize(int fontsize);

フッターの高さは、フッターを追加する際にページの下部に追加される追加スペースを制御します。高さはPDF座標系で、既存のページコンテンツの下に表示されます。

コマンドライン例 キー: –footer_height : フッターの高さの正の整数 コマンドライン引数:

--footer_height 30

PdfMergeAttribute メソッド

PdfMergeAttributes.setFooterHeight(int fontsize);

フッターテンプレートは、add_footerオプションを使用する際に追加されるテキストコンテンツを制御します。この属性の値には、ドキュメントおよびページ固有の値に置き換えられる2つの動的変数があります。%FILENAME - 元のファイル名に置き換えられます。%PAGENUMBER - 元のファイル内のこのページの元のページ番号に置き換えられます。

コマンドライン例 キー: –footer_template : add_footerオプションを使用する場合に各ページのフッターに追加される文字列。コマンドライン引数:

--footer_template "%FILENAME, Page %PAGENUMBER"

PdfMergeAttribute メソッド

PdfMergeAttributes.setFooterTemplate(String template);

テキストの色

テキストの色は、マージ用に生成されるテキストコンテンツの色を設定します。

コマンドライン例 キー: –text_color : RGBカラーを表す3つの浮動小数点値。各値は0から1の間で、赤、緑、青の順 コマンドライン引数:

--text_color 0.0 0.0 0.0

PdfMergeAttribute メソッド

PdfMergeAttributes.setTextColor(float[] color);