How to print a Java Word document

Today, I would like to show you how to print a Word document in Java. There are three ways to do it with Spire.doc for Java:

  1. Print with PrinterJob class
  2. Print to physical printer
  3. Mark the virtual printer
It will be

.

Now, let me introduce each of them.

Preparation

1. From the official website of E-iceblue Free Spire.doc for Java Download the free version.

f:id:lendoris:20201216121839p:plain

2. Start the IDE, create a new project, and then add the appropriate Spire.doc.jar for the installed files to your references.

f:id:lendoris:20201216121911p:plain

Print with PrinterJob class

```java import com.spire.doc.*; import java.awt.print.*; public class WordPrint {
public static void main(String[] args) throws Exception {
    //Load the Document.
    Document doc = new Document();
    doc.loadFromFile("Sample.docx");

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

    //Set the paper size.
    Paper loPaper = loPageFormat.getPaper();
    loPaper.setSize(600, 500);
    loPageFormat.setPaper(loPaper);

    //Remove the default margin.
    loPaper.setImageableArea(0, 0, loPageFormat.getWidth(), loPageFormat.getHeight());
    //Set the number of copies to print.
    loPrinterJob.setCopies(1);
    loPrinterJob.setPrintable(doc, loPageFormat);
    //Set the dialog box.
    if (loPrinterJob.printDialog()) {
        //Print.
        try {
            loPrinterJob.print();
        } catch (PrinterException e)

        {
            e.printStackTrace();
        }
    }
}

}


 <h4> Print to a physical printer </h4>
```java
import com.spire.doc.Document;
import com.spire.ms.System.Drawing.Printing.PrinterSettings;

public class PrintWord {

    public static void main(String[] args) {

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

        //Create a PrinterSettings object.
        PrinterSettings printerSettings = new PrinterSettings();

        //Set the name of the physical printer.
        printerSettings.setPrinterName("\\\\192.168.1.104\\HP LaserJet P1007");

        //Set the number of copies to print.
        printerSettings.setCopies((short) 1);

        //Set the print range.
        printerSettings.setFromPage(2);
        printerSettings.setToPage(4);

        //Apply the settings.
        document.getPrintDocument().setPrinterSettings(printerSettings);

        //Print.
        document.getPrintDocument().print();
    }
}

Print to virtual printer

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

public class PrintWord {

public static void main(String[] args) {

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

    //Create a PrinterSettings object.
    PrinterSettings printerSettings = new PrinterSettings();

    //Set up a virtual printer.
    printerSettings.setPrinterName("Microsoft Print to PDF");

    //Print to a file.
    printerSettings.setPrintToFile(true);

    //Set the save location and name of the file.
    printerSettings.setPrintFileName("output/PrintToPDF.pdf");

    //Apply the settings.
    document.getPrintDocument().setPrinterSettings(printerSettings);

    //Print.
    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>


Recommended Posts

How to print a Java Word document
Java creates a Word document
How to make a Java container
[Java] How to create a folder
[Java] Add WordArt to Word document
How to make a Java array
How to make a Java calendar Summary
[Introduction to Java] How to write a Java program
Java creates a table in a Word document
How to make a Discord bot (Java)
How to display a web page in Java
How to convert a solidity contract to a Java contract class
[Java] How to use Map
How to create a Java environment in just 3 seconds
How to lower java version
[Java] How to use Map
How to uninstall Java 8 (Mac)
How to jump from Eclipse Java to a SQL file
Java --How to make JTable
java: How to write a generic type list [Note]
[Java] How to play rock-paper-scissors (equivalent to paiza rank A)
How to write java comments
How to leave a comment
How to use java class
[Java] How to use Optional ②
How to create a data URI (base64) in Java
[Java] How to use removeAll ()
[Java] How to display Wingdings
[Java] How to get a request by HTTP communication
How to use Java Map
[Java] How to execute tasks on a regular basis
How to set Java constants
[Java] How to erase a specific character from a character string
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
How to use Java variables
How to convert Java radix
[Java] How to implement multithreading
[Java] How to use Optional ①
How to initialize Java array
How to insert a video
How to create a method
[Java] How to start a new line with StringBuilder
How to deploy a simple Java Servlet app on Heroku
[Java] How to convert a character string from String type to byte type
How to store a string from ArrayList to String in Java (Personal)
How to deploy a kotlin (java) app on AWS fargate
How to develop and register a Sota app in Java
How to simulate uploading a post-object form to OSS in Java
[jsoup] How to get the full amount of a document
A story about misunderstanding how to use java scanner (memo)
Add multi-letter watermark to Java Word
How to study Java Silver SE 8
How to use Java HttpClient (Get)
How to add columns to a table
Studying Java # 6 (How to write blocks)
[Java] How to update Java on Windows
How to disassemble Java class files
How to use Java HttpClient (Post)
[Java] How to use join method
How to sign a Minecraft MOD