Friday, 9 April 2021

Print Word documents in Java using a free API

This tutorial will introduce two methods to print word documents using Java codes. In the process of printing documents, you can set the paper size, copies and whether to show print dialog.

I used a free third-party library called Free Spire.Doc for Java in this sample. Before running codes, you need to add Spire.Doc.jar in the library to IDEA. Download the package from the link, and manually add it to IDEA or directly refer to it using the following configuration in the Maven pom.xml.

<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

Print Word file

import com.spire.doc.Document;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

public class PrintFile {
public static void main(String[] args) {
//load a Word document that you want to print
Document document = new Document();
document.loadFromFile(
"C:\\Users\\Administrator\\Desktop\\DocoumentToPrint.docx");

//Create a PrinterJob object
PrinterJob printerJob = PrinterJob.getPrinterJob();

//Create a PageFormat object and set it to the default size and orientation
PageFormat pageFormat = printerJob.defaultPage();

//Return a copy of the Paper object associated with this PageFormat
Paper paper = pageFormat.getPaper();

//Set the imageable area of this Paper
paper.setImageableArea(0, 0, pageFormat.getWidth(), pageFormat.getHeight());

//Set the number of copies
printerJob.setCopies(1);

//Set the Paper object for this PageFormat
pageFormat.setPaper(paper);

//Call painter to render the pages in the specified format
printerJob.setPrintable(document, pageFormat);

//Execute print
try {
printerJob.print();
}
catch (PrinterException e) {
e.printStackTrace();
}
}
}

New method to print Word

import com.spire.doc.*;
import java.awt.print.*;
public class NewMethodToPrint {
public static void main(String[] args) {
//Load the sample 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 copies
loPrinterJob.setCopies(1);
loPrinterJob.setPrintable(doc, loPageFormat);
//set the print dialog
if (loPrinterJob.printDialog()) {
//print the word document
try {
loPrinterJob.print();
} catch (PrinterException e)

{
e.printStackTrace();
}
}
}
}

No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...