Print MS-Word Documents in Java
Tags: java, print, msword
Published: 2019-06-19 07:07:19

---
title: Print MS-Word Documents in Java
published: true
description: This article demonstrates how to print Word document using a java Word component.
tags: java,print,ms-word
---

Printing Word documents from java code is often required by java developers. In this article, I’ll introduce how to send a Word document to a network connected physical printer or a virtual printer like Microsoft Print to PDF, by using [Spire.Doc for Java](https://www.e-iceblue.com/Introduce/doc-for-java.html).

There are two important classes involved in printing, namely PrinterSettings and PrintDocument class. Check below table to learn about their definitions. 

| Class    | Definition |
| :---------------- |-----:|
| PrinterSettings | Specifies information about how a document is printed, including the printer that prints it.|
| PrintDocument |Defines a reusable object that sends document to a printer. |   
	
##Example 1: print to physical printer

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

public class PrintWord {

    public static void main(String[] args) {

        //load a Word document
        Document document = new Document();
        document.loadFromFile("C:\\Users\\Administrator\\Desktop\\DocoumentToPrint.docx");

        //create a PrinterSettings object
        PrinterSettings printerSettings = new PrinterSettings();

        //specify printer name
        printerSettings.setPrinterName("\\\\192.168.1.104\\HP LaserJet P1007");

        //set copies to print
        printerSettings.setCopies((short) 1);

        //set the page range to print
        printerSettings.setFromPage(2);
        printerSettings.setToPage(4);

        //get PrintDocument object
        PrintDocument printDocument = document.getPrintDocument();
        
        //apply printer settings
        printDocument .setPrinterSettings(printerSettings);

        //execute print
        printDocument .print();
    }
}
```
##Example 2: print to virtual printer
```java
import com.spire.doc.Document;
import com.spire.ms.System.Drawing.Printing.PrintDocument;
import com.spire.ms.System.Drawing.Printing.PrinterSettings;

public class PrintToPdf {

    public static void main(String[] args) {

        //load a Word document
        Document document = new Document();
        document.loadFromFile("C:\\Users\\Administrator\\Desktop\\DocumentToPrint.docx");

        //create a PrinterSettings object
        PrinterSettings printerSettings = new PrinterSettings();

        //specify virtual printer name
        printerSettings.setPrinterName("Microsoft Print to PDF");

        //print to file
        printerSettings.setPrintToFile(true);

        //specify path and name of the printed file
        printerSettings.setPrintFileName("output/PrintToPDF.pdf");

        //get PrintDocument object
        PrintDocument printDocument = document.getPrintDocument();

        //apply printer settings
        printDocument.setPrinterSettings(printerSettings);

        //execute print
        printDocument.print();
    }
}
```