0
0

JavaでPDF を印刷する

Posted at

PDF形式は、異なるプラットフォームやアプリケーションで一貫した表示が可能であり、さまざまな文書を簡単かつ正確に共有するための便利な形式です。PDF印刷では、用紙サイズ、品質、グレースケールなどのさまざまなオプションを設定し、印刷前にドキュメントをプレビューすることができます。同様に、プログラミングによってさまざまな方法でPDF印刷を実現することができます。以下はコードの説明とサンプルコードです。

ツール

Jarファイルの導入

  • Free Spire.PDF for Javaをダウンロードして解凍します。
  • IDEAで新しいプロジェクトを作成します。
  • 「File」>「Project Structure」>「Modules」>「Dependencies」
  • 「+」への「JARs or Directories」
  • 「Spire.Pdf.jar」を見つけてプロジェクトにインポートします。

デフォルトのプリンター

説明:

PrinterJobクラスのオブジェクトを作成し、PdfDocumentクラスのオブジェクトを作成してPdfDocument.LoadFromFile()メソッドを使用してPDFドキュメントを読み込みます。次に、PrinterJob.setPrintable()メソッドを使用して設定された書式でドキュメントのページをレンダリングします。最後に、PrinterJob.print()メソッドを使用してPDFのページを印刷します。

コード

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("sample.pdf");

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

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

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

説明:

PrinterJobクラスのオブジェクトを作成し、カスタムのfindPrintService()メソッドを使用して適切なプリンタを見つけ、次にPrinterJob.setPrintService()メソッドを使ってそのプリンタを印刷に使用するよう指定します。PdfDocumentクラスのオブジェクトを作成し、PdfDocument.LoadFromFile()メソッドを使用してPDFドキュメントを読み込みます。設定された書式でドキュメントのページをレンダリングするために、PrinterJob.setPrintable()メソッドを使用します。PrintRequestAttributeSetクラスのオブジェクトを作成し、印刷するページ範囲を設定します。最後に、PrinterJob.print()メソッドを使用してPDFのページを印刷します。

コード

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("sample.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を印刷ダイアログで印刷する

説明:

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

コード

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("sample.pdf");

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

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

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

説明:

PrinterJobクラスのオブジェクトを作成し、PdfDocumentクラスのオブジェクトを作成してPdfDocument.LoadFromFile()メソッドを使用してPDFドキュメントを読み込みます。設定された書式でドキュメントのページをレンダリングするために、PrinterJob.setPrintable()メソッドを使用します。PrintRequestAttributeSetクラスのオブジェクトを作成し、印刷モードを両面印刷に設定します。最後に、PrinterJob.print()メソッドを使用してPDFのページを印刷します。

コード

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("sample.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 ファイルをカスタム用紙サイズで印刷する

説明:

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

コード

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("sample.pdf");

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

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

その他

ところで、Free Spire.PDF for JavaはPDFからWordへの変換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