Logo

dev-resources.site

for different kinds of informations.

Print MS-Word Documents in Java

Published at
6/19/2019
Categories
java
print
msword
Author
eiceblue
Categories
3 categories in total
java
open
print
open
msword
open
Author
8 person written this
eiceblue
open
Print MS-Word Documents in Java

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.

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

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

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();
    }
}

Featured ones: