チュートリアル

簡単にライブラリーを使用するためのチュートリアルコレクション

チュートリアル»Java»Spire.PDF for Java»印刷»Java:PDF を印刷する方法
2023-02-02

Java:PDF を印刷する方法

java.awt.print パッケージは、一般的な印刷に必要なクラスとメソッドを提供し、プリンタ名の指定、印刷範囲の設定、両面印刷の設定、カスタム用紙サイズの設定など、ドキュメントの種類の指定と印刷オプションの管理を行うことができます。この記事では、Spire.PDF for Java クラスライブラリと java.awt.print パッケージを使用して、PDF ドキュメントを印刷する方法を説明します。

Spire.PDF for Java をインストールします

まず、Spire.PDF for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。

<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</artifactId>
        <version>9.1.4</version>
    </dependency>
</dependencies>

PDF ファイルをデフォルトのプリンターで印刷する

印刷時にプリンター名が指定されていない場合、プログラムはデフォルトのプリンターを使用してドキュメントを印刷します。以下は、java.awt.print と Spire.PDF for Java を使用して、PDF ドキュメントをデフォルトのプリンターで印刷する手順です。

  • PrinterJob クラスのオブジェクトを作成します。
  • PdfDocument クラスのオブジェクトを作成し、PdfDocument.LoadFromFile() メソッドを使用してPDFドキュメントを読み込みます。
  • 設定された書式でドキュメントのページをレンダリングするために、PrinterJob.setPrintable() メソッドを使用します。
  • PrinterJob.print() メソッドを使用して、PDFのページを印刷します。
  • Java
import com.spire.pdf.PdfDocument;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

public class printWithDefaultPrinter {

    public static void main(String[] args) {

        //PrinterJobクラスのオブジェクトを作成する
        PrinterJob printerJob = PrinterJob.getPrinterJob();

        //PageFormatクラスのオブジェクトを作成し、ページをデフォルトのサイズと向きに設定する
        PageFormat pageFormat = printerJob.defaultPage();

        //Paperクラスのオブジェクトを作成し、用紙の設定を取得する
        Paper paper = pageFormat.getPaper();

        //用紙の描画可能範囲を設定する
        paper.setImageableArea(0, 0, pageFormat.getWidth(), pageFormat.getHeight());

        //用紙設定を適用する
        pageFormat.setPaper(paper);

        //PdfDocumentクラスのオブジェクトを作成する
        PdfDocument pdf = new PdfDocument();

        //PDFファイルを読み込む
        pdf.loadFromFile("企画書.pdf");

        //設定された書式でページを描画する
        printerJob.setPrintable(pdf, pageFormat);

        //印刷を実行する
        try {
            printerJob.print();
        } catch (PrinterException e) {
            e.printStackTrace();
        }
    }
}

指定したページ範囲を指定したプリンターで印刷する

指定されたプリンタ上で PDF ドキュメントを印刷する必要がある場合、まず使用したいプリンタを見つけ、それから PrinterJob.setPrintService() メソッドを使用してそのプリンタ上での印刷を指定することができます。 Java.awt.print と Spire.PDF for Java を使用して、PDF ドキュメントの指定した範囲のページを、指定したプリンターで印刷する手順を説明します。

  • PrinterJob クラスのオブジェクトを作成します。
  • カスタム findPrintService() メソッドを使用して、使用するプリンタを見つけ、次に PrinterJob.setPrintService() メソッドを使用して、印刷に使用されるそのプリンタを指定します。
  • PdfDocument クラスのオブジェクトを作成し、PdfDocument.LoadFromFile() メソッドを使用して PDF ドキュメントを読み込みます。
  • 設定された書式でドキュメントのページをレンダリングするために、PrinterJob.setPrintable() メソッドを使用します。
  • PrintRequestAttributeSet クラスのオブジェクトを作成し、印刷するページ範囲を設定します。
  • PrinterJob.print() メソッドを使用して、PDF のページを印刷します。
  • Java
import com.spire.pdf.PdfDocument;
import javax.print.PrintService;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.PageRanges;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

public class printWithSpecifiedPrinter {

    public static void main(String[] args) throws PrinterException {

        //PrinterJobクラスのオブジェクトを作成する
        PrinterJob printerJob = PrinterJob.getPrinterJob();

        //プリンターを指定する
        PrintService myPrintService = findPrintService("\\\\192.168.1.104\\HP LaserJet P1007");
        printerJob.setPrintService(myPrintService);

        //PageFormatクラスのオブジェクトを作成し、ページをデフォルトのサイズと向きに設定する
        PageFormat pageFormat = printerJob.defaultPage();

        //Paperクラスのオブジェクトを作成し、用紙の設定を取得する
        Paper paper = pageFormat.getPaper();

        //用紙の描画可能範囲を設定する
        paper.setImageableArea(0, 0, pageFormat.getWidth(), pageFormat.getHeight());

        //用紙設定を適用する
        pageFormat.setPaper(paper);

        //PdfDocumentクラスのオブジェクトを作成する
        PdfDocument pdf = new PdfDocument();

        //PDFファイルを読み込む
        pdf.loadFromFile("企画書.pdf");

        //指定された書式でページをレンダリングする
        printerJob.setPrintable(pdf, pageFormat);

        //PrintRequestAttributeSetクラスのオブジェクトを作成する
        PrintRequestAttributeSet attributeSet = new HashPrintRequestAttributeSet();

        //印刷範囲を設定する
        attributeSet.add(new PageRanges(1,7));

        //印刷を実行する
        try {
            printerJob.print(attributeSet);
        } catch (PrinterException e) {
            e.printStackTrace();
        }
    }

    //プリントサービスを探す
    private static PrintService findPrintService(String printerName) {

        PrintService[] printServices = PrinterJob.lookupPrintServices();
        for (PrintService printService : printServices) {
            if (printService.getName().equals(printerName)) {

                System.out.print(printService.getName());
                return printService;
            }
        }
        return null;
    }
}

PDF ファイルを印刷ダイアログで印刷する

PDF ドキュメントを印刷する場合、印刷ダイアログで印刷を設定することもできます。 ここでは、java.awt.print と Spire.PDF for Java を使用して、印刷ダイアログから PDF ドキュメントを印刷する手順を説明します。

  • PrinterJob クラスのオブジェクトを作成します。
  • PdfDocument クラスのオブジェクトを作成し、PdfDocument.LoadFromFile() メソッドを使用して PDF ドキュメントを読み込みます。
  • 設定された書式でドキュメントのページをレンダリングするために、PrinterJob.setPrintable() メソッドを使用します。
  • プリントダイアログを表示するには、PrinterJob.printDialog() メソッドを使用します。
  • PrinterJob.print() メソッドを使用して、PDF のページを印刷します。
  • Java
import com.spire.pdf.PdfDocument;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

public class printWithPrintDialog {

    public static void main(String[] args) {

        //PrinterJobクラスのオブジェクトを作成する
        PrinterJob printerJob = PrinterJob.getPrinterJob();

        //PageFormatクラスのオブジェクトを作成し、ページをデフォルトのサイズと向きに設定する
        PageFormat pageFormat = printerJob.defaultPage();

        //Paperクラスのオブジェクトを作成し、用紙の設定を取得する
        Paper paper = pageFormat.getPaper();

        //用紙の描画可能領域を設定する
        paper.setImageableArea(0, 0, pageFormat.getWidth(), pageFormat.getHeight());

        //用紙設定を適用する
        pageFormat.setPaper(paper);

        //PdfDocumentクラスのオブジェクトを作成する
        PdfDocument pdf = new PdfDocument();

        //PDFファイルを読み込む
        pdf.loadFromFile("企画書.pdf");

        //設定された書式でページをレンダリングする
        printerJob.setPrintable(pdf, pageFormat);

        //印刷ダイアログを表示する
        if (printerJob.printDialog()) {
            try {
                printerJob.print();
            } catch (PrinterException e) {
                e.printStackTrace();
            }
        }
    }
}

PDF ファイルを両面印刷モードで印刷する

java.awt.print と Spire.PDF for Java を使用して、PDF ドキュメントを両面印刷する手順を説明します。

  • PrinterJob クラスのオブジェクトを作成します。
  • PdfDocument クラスのオブジェクトを作成し、PdfDocument.LoadFromFile() メソッドを使用して PDF ドキュメントを読み込みます。
  • 設定された書式でドキュメントのページをレンダリングするために、PrinterJob.setPrintable() メソッドを使用します。
  • PrintRequestAttributeSet クラスのオブジェクトを作成し、印刷モードを両面印刷に設定します。
  • PrinterJob.print() メソッドを使用して、PDF のページを印刷します。
  • Java
import com.spire.pdf.PdfDocument;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Sides;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

public class printInDuplexMode {

    public static void main(String[] args) {

        //PrinterJobクラスのオブジェクトを作成する
        PrinterJob printerJob = PrinterJob.getPrinterJob();

        //PageFormatクラスのオブジェクトを作成し、ページをデフォルトのサイズと向きに設定する
        PageFormat pageFormat = printerJob.defaultPage();

        //Paperクラスのオブジェクトを作成し、用紙の設定を取得する
        Paper paper = pageFormat.getPaper();

        //用紙の描画可能領域を設定する
        paper.setImageableArea(0, 0, pageFormat.getWidth(), pageFormat.getHeight());

        //用紙設定を適用する
        pageFormat.setPaper(paper);

        //PdfDocumentクラスのオブジェクトを作成する
        PdfDocument pdf = new PdfDocument();

        //PDFファイルを読み込む
        pdf.loadFromFile("企画書.pdf");

        //設定された書式でドキュメントのページをレンダリングする
        printerJob.setPrintable(pdf, pageFormat);

        //PrintRequestAttributedクラスのオブジェクトを作成する
        PrintRequestAttributeSet attributeSet = new HashPrintRequestAttributeSet();

        //印刷モードを両面印刷に設定する
        attributeSet.add(Sides.TWO_SIDED_SHORT_EDGE);

        //印刷を実行する
        try {
            printerJob.print(attributeSet);
        } catch (PrinterException e) {
            e.printStackTrace();
        }
    }
}

PDF ファイルをカスタム用紙サイズで印刷する

Paper クラスのメソッドでは、印刷時の用紙サイズを設定することができます。 用紙サイズは、Paper.setSize() メソッドでカスタマイズすることができます。java.awt.print と Spire.PDF for Java を使用して、PDF ドキュメントをカスタム用紙サイズに印刷する手順を説明します。

  • PrinterJob クラスのオブジェクトを作成します。
  • Paper クラスの Paper.setSize() メソッドで用紙の高さと幅を設定します。
  • PdfDocument クラスのオブジェクトを作成し、PdfDocument.LoadFromFile() メソッドを使用して PDF ドキュメントを読み込みます。
  • 設定された書式でドキュメントのページをレンダリングするために、PrinterJob.setPrintable() メソッドを使用します。
  • PrinterJob.print() メソッドを使用して、PDF のページを印刷します。
  • Java
import com.spire.pdf.PdfDocument;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

public class printInCustomPaperSize {

    public static void main(String[] args) {

        //PrinterJobクラスのオブジェクトを作成する
        PrinterJob printerJob = PrinterJob.getPrinterJob();

        //PageFormatクラスのオブジェクトを作成し、ページをデフォルトのサイズと向きに設定する
        PageFormat pageFormat = printerJob.defaultPage();

        //Paperクラスのオブジェクトを作成し、用紙の設定を取得する
        Paper paper = pageFormat.getPaper();

        //用紙の高さと幅を設定する
        paper.setSize(500,600);

        //用紙設定を適用する
        pageFormat.setPaper(paper);

        //PdfDocumentクラスのオブジェクトを作成する
        PdfDocument pdf = new PdfDocument();

        //PDFファイルを読み込む
        pdf.loadFromFile("企画書.pdf");

        //設定された書式でページをレンダリングする
        printerJob.setPrintable(pdf, pageFormat);

        //印刷を実行する
        try {
            printerJob.print();
        } catch (PrinterException e) {
            e.printStackTrace();
        }
    }
}

一時ライセンスを申請する

結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。

Read 595 times