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
Author
8 person written this
eiceblue
open
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();
}
}
msword Article's
13 articles in total
How to Disable Removal of Formatting When Pasting Text in Microsoft Word for Mac
read article
highlight tất cả các đoạn văn nằm giữa dấu ngoặc đơn, ngoặc vuông, ngoặc nhọn
read article
Giảm dung lượng file word chứa nhiều ảnh
read article
Giảm dung lượng file word chứa nhiều ảnh
read article
code block trong MSWord
read article
MSWord Styles
read article
MSWord / Table / Repeat Header Row
read article
MSWord Heading
read article
Java - How to Add a Background Color, Picture or Texture to Word Documents
read article
Java - How to Compare Two Word Documents to Get Differences
read article
Java – How to Add or Remove Bookmarks in a Word Document
read article
Print MS-Word Documents in Java
currently reading
Embed Fonts in Word Documents in Java
read article
Featured ones: