0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JavaでPDFファイルを複数のPDFファイルに分割する方法

Posted at

PDFファイルでは、文書全体ではなく、特定のページや部分のみを共有する必要がある場合があります。PDFファイルを分割することで、必要な情報を簡単に抽出し、他の人と共有することができ、残りのファイルは非公開にすることができます。今回は、無料のFree Spire.PDF for Javaを使って、PDFファイルを複数のPDFファイルに分割する方法を紹介します。

【依存関係の追加】

この方法は、無料のFree Spire.PDF for Javaが必要ですので、先にjarファイルをインポートしてください。

1. Maven

Maven を使用している場合、プロジェクトの pom.xml ファイルに以下のコードを追加することで、簡単にアプリケーションに JAR ファイルをインポートすることができます。

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf.free</artifactId>
        <version>5.1.0</version>
    </dependency>
</dependencies>

2. 公式サイトよりJarファイルをダウンロード

まず、Free Spire.PDF for Javaの公式サイトよりzipファイルをダウンロードします。zipファイルを解凍し、libフォルダの下にあるSpire.Pdf.jarファイルを依存関係としてプロジェクトにインポートしてください。

PDFファイルをページごとに分割する

PdfDocument.split(String destFilePattern, int startNumber) メソッドを使用すると、PDFファイルを各ページごとに分割できます。分割後は、PDFファイル内の各ページが別のPDFファイルとして保存されます。

以下の手順で、PDFファイルをページ単位で分割する方法を説明します。

  • PdfDocument のインスタンスを作成します。
  • PdfDocument.loadFromFile() メソッドを使用して PDF ファイルを読み込みます。
  • PdfDocument.split(String destFilePattern, int startNumber) メソッドを使用して、PDFファイルの各ページを別のファイルに分割します。

Java

import com.spire.pdf.PdfDocument;

public class splitPDFByPage {
    public static void main(String[] args) {

        //PdfDocumentのインスタンスを作成する
        PdfDocument pdf = new PdfDocument();

        //PDFファイルを読み込む
        pdf.loadFromFile("C:/恐竜は物語である.pdf");

        //PDFの各ページを1つのファイルに分割する
        pdf.split("Output/分割-{0}.pdf", 1);
        pdf.close();
    }
}

PDFファイルをページごとに分割する

PDFファイルをページ範囲ごとに分割する

Free Spire.PDF for Javaは、PdfDocument.insertPageRange(PdfDocument doc, int startIndex, int endIndex) メソッドを提供し、PDFファイルから別のPDFファイルへページ範囲を取り込むことを可能にします。このメソッドを使用すると、簡単にページ範囲によってPDFファイルを分割することができます。

詳細な手順は、2つのページ範囲によってPDFファイルを分割する方法を示しています。

  • PdfDocument のインスタンスを作成します。
  • PdfDocument.loadFromFile() で PDF ファイルを読み込みます。
  • 別の PdfDocument のインスタンスを作成します。
  • PdfDocument.insertPageRange(PdfDocument doc, int startIndex, int endIndex) メソッドを使用して、ソースPDFファイルの1ページ目と2ページ目を新しいPDFファイルに取り込みます。
  • PdfDocument.SaveToFile() メソッドを使用して、新しいPDFファイルを保存します。
  • PdfDocument の別のインスタンスを作成します。
  • PdfDocument.insertPageRange(PdfDocument doc, int startIndex, int endIndex) メソッドを使用して、ソースPDFファイルの3、4、5ページ目を新しいPDFファイルに取り込みます。
  • PdfDocument.SaveToFile() メソッドを使用して、新しいPDFファイルを保存します。

Java

import com.spire.pdf.PdfDocument;

public class splitPDFByPageRange {
    public static void main(String[] args) {

        //PdfDocumentのインスタンスを作成する
        PdfDocument pdf = new PdfDocument();

        //PDFファイルを読み込む
        pdf.loadFromFile("C:/恐竜は物語である.pdf");

        //PdfDocumentの別のインスタンスを作成する
        PdfDocument newDoc1 = new PdfDocument();
        //ソースPDFの1~2ページを新しいPDFに取り込む
        newDoc1.insertPageRange(pdf, 0, 1);
        //ドキュメントを保存する
        newDoc1.saveToFile("ファイル1.pdf");

        //PdfDocumentの別のインスタンスを作成する
        PdfDocument newDoc2 = new PdfDocument();
        //元PDFの3~5ページを新PDFに取り込む
        newDoc2.insertPageRange(pdf, 2, 4);
        //ドキュメントを保存する
        newDoc2.saveToFile("ファイル2.pdf");
    }
}

PDFファイルをページ範囲ごとに分割する

PDFのページを複数のページに分割する

PDFページを複数ページに分割する方法は、ページサイズを小さくした新しいPDFファイルを作成し、PdfPageBase.createTemplate().draw() メソッドを使用してソースPDFページ上のコンテンツの異なる部分を新しいPDFファイルの異なるページに描画する方法です。

次のステップは、PDFページを2つのページに分割する方法を示しています。

  • PdfDocument のインスタンスを作成します。
  • PdfDocument.loadFromFile() メソッドを使用して PDF ファイルを読み込みます。
  • PdfDocument.getPages().get() メソッドを使用して、特定のページを取得します。
  • PdfDocument クラスのインスタンスを作成して新しいPDFファイルを作成し、そのページの余白を0に設定します。
    新しいPDFのページサイズを、ソースPDFの半分または数分の1に設定します。
  • PdfDocument.getPages().add() メソッドを使用して、新しいPDFファイルにページを追加します。
  • PdfPageBase.createTemplate().draw() メソッドを使用して、ソース PDF の特定のページの内容を、新しい - PDFの新しく追加されたページ上に描画します。
  • PdfDocument.saveToFile() メソッドを使用してドキュメントを保存します。

Java

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfLayoutBreakType;
import com.spire.pdf.graphics.PdfLayoutType;
import com.spire.pdf.graphics.PdfTextLayout;

import java.awt.geom.Point2D;

public class splitPDFPage {
    public static void main(String[] args) {

        //PdfDocumentのインスタンスを作成する
        PdfDocument pdf = new PdfDocument();

        //ソースPDFファイルを読み込む
        pdf.loadFromFile("C:/恐竜は物語である.pdf");

        //最初のページを取得する
        PdfPageBase page = pdf.getPages().get(0);

        //PdfDocumentの別のインスタンスを作成する
        PdfDocument newPdf = new PdfDocument();

        //ページの余白をなくす
        newPdf.getPageSettings().getMargins().setAll(0);

        //新しいPDFのページ幅を、ソースPDFのページ幅と同じにする
        newPdf.getPageSettings().setWidth((float) page.getSize().getWidth());

        //新しいPDFのページ高をソースPDFの1/2に設定する
        newPdf.getPageSettings().setHeight((float) page.getSize().getHeight()/2);

        //新しいPDFにページを追加する
        PdfPageBase newPage = newPdf.getPages().add();

        //PdfLayoutTypeをPaginateに設定し、コンテンツを自動的にページ分割するようにする
        PdfTextLayout layout = new PdfTextLayout();
        layout.setBreak(PdfLayoutBreakType.Fit_Page);
        layout.setLayout(PdfLayoutType.Paginate);

        //ソースページの内容を新しいPDFのページ上に描画する
        page.createTemplate().draw(newPage, new Point2D.Float(0, 0), layout);

        //新しいPDFファイルを保存する
        newPdf.saveToFile("PDFページ分割.pdf");
        newPdf.close();
    }
}

PDFのページを複数のページに分割する

以上、PDFファイルをページ単位、ページ範囲単位で分割する方法と、PDFのページを分割する方法について説明しました。Free Spire.PDF for Javaは、ページの回転、空白ページの削除、ページサイズの変更、ページの再配置など、PDFページ上で多くの他の操作を行うためにも使用することができます。これらの詳細については、Spire.PDF for Javaチュートリアルに移動します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?