DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on • Edited on

2 1

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

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay