1
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 3 years have passed since last update.

Java Word文書を印刷する方法

Posted at

今日は、JavaでWord文書を印刷する方法を紹介していただきます。Spire.doc for Javaで三つのやり方があって、すなわち:

  1. PrinterJobクラスで印刷
  2. 物理プリンターに印刷
  3. 仮想プリンターに印

というものになります。

では、今それぞれを紹介させていただきましょう。

 下準備

1.E-iceblueの公式サイトからFree Spire.doc for Java無料版をダウンロードしてください。

f:id:lendoris:20201216121839p:plain

2.IDEを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいSpire.doc.jarを参照に追加してください。

f:id:lendoris:20201216121911p:plain

PrinterJobクラスで印刷

```JAVA import com.spire.doc.*; import java.awt.print.*; public class WordPrint {
public static void main(String[] args) throws Exception {
    // Documentをロードします。
    Document doc = new Document();
    doc.loadFromFile("Sample.docx");

    PrinterJob loPrinterJob = PrinterJob.getPrinterJob();
    PageFormat loPageFormat = loPrinterJob.defaultPage();

    //用紙サイズを設定します。 
    Paper loPaper = loPageFormat.getPaper();
    loPaper.setSize(600, 500);
    loPageFormat.setPaper(loPaper);

    //デフォルトのマージンを削除します。
    loPaper.setImageableArea(0, 0, loPageFormat.getWidth(), loPageFormat.getHeight());
    //印刷部数を設定します。
    loPrinterJob.setCopies(1);
    loPrinterJob.setPrintable(doc, loPageFormat);
    //ダイアログボックスを設定します。
    if (loPrinterJob.printDialog()) {
        //印刷します。
        try {
            loPrinterJob.print();
        } catch (PrinterException e)

        {
            e.printStackTrace();
        }
    }
}

}


<h4>物理プリンターに印刷</h4>
```JAVA
import com.spire.doc.Document;
import com.spire.ms.System.Drawing.Printing.PrinterSettings;

public class PrintWord {

    public static void main(String[] args) {

        //Wordをロードします。
        Document document = new Document();
        document.loadFromFile("C:\\Users\\Administrator\\Desktop\\DocoumentToPrint.docx");

        //PrinterSettings objectを作成します。
        PrinterSettings printerSettings = new PrinterSettings();

        //物理プリンターの名前を設定します。
        printerSettings.setPrinterName("\\\\192.168.1.104\\HP LaserJet P1007");

        //印刷部数を設定します。
        printerSettings.setCopies((short) 1);

        //印刷範囲を設定します。
        printerSettings.setFromPage(2);
        printerSettings.setToPage(4);

        //設定を適用します。
        document.getPrintDocument().setPrinterSettings(printerSettings);

        //印刷します。
        document.getPrintDocument().print();
    }
}

仮想プリンターに印刷

```JAVA import com.spire.doc.Document; import com.spire.ms.System.Drawing.Printing.PrinterSettings;

public class PrintWord {

public static void main(String[] args) {

    //Wordをロードします。
    Document document = new Document();
    document.loadFromFile("C:\\Users\\Administrator\\Desktop\\DocumentToPrint.docx");

    //PrinterSettingsオブジェクトを作成します。
    PrinterSettings printerSettings = new PrinterSettings();

    //仮想プリンターを設定します。
    printerSettings.setPrinterName("Microsoft Print to PDF");

    //ファイルに印刷します。
    printerSettings.setPrintToFile(true);

    //ファイルの保存場所と名前を設定します。
    printerSettings.setPrintFileName("output/PrintToPDF.pdf");

    //設定を適用します。
    document.getPrintDocument().setPrinterSettings(printerSettings);

    //印刷します。
    document.getPrintDocument().print();
}

}


<p><img src="https://cdn-ak.f.st-hatena.com/images/fotolife/l/lendoris/20201216/20201216122015.png" alt="f:id:lendoris:20201216122015p:plain" title="" class="hatena-fotolife" itemprop="image" /></p>
<p><br /><br /></p>
<p> </p>
1
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
1
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?