Printing Documents in Aspose.Words for Java

If you’re looking to print documents using Aspose.Words for Java, you’re in the right place. In this step-by-step guide, we’ll walk you through the process of printing documents with Aspose.Words for Java using the provided source code.

Introduction

Printing documents is a common task in many applications. Aspose.Words for Java provides a powerful API to work with Word documents, including the ability to print them. In this tutorial, we’ll guide you through the process of printing a Word document step by step.

Setting up Your Environment

Before we dive into the code, make sure you have the following prerequisites in place:

  • Java Development Kit (JDK) installed
  • Aspose.Words for Java library downloaded and added to your project

Loading the Document

To get started, you’ll need to load the Word document you want to print. Replace "Your Document Directory" with the path to your document and "Your Output Directory" with the desired output directory.

string dataDir = "Your Document Directory";
string outPath = "Your Output Directory";
Document doc = new Document(dataDir + "Rendering.docx");

Creating a Print Job

Next, we’ll create a print job to print our loaded document. The code snippet below initializes a print job and sets the desired printer settings.

// Create a print job to print our document with.
PrinterJob pj = PrinterJob.getPrinterJob();
// Initialize an attribute set with the number of pages in the document.
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(new PageRanges(1, doc.getPageCount()));
// Pass the printer settings along with the other parameters to the print document.
MultipagePrintDocument awPrintDoc = new MultipagePrintDocument(doc, 4, true, attributes);

Printing the Document

Now that we’ve set up our print job, it’s time to print the document. The following code snippet associates the document with the print job and initiates the printing process.

// Pass the document to be printed using the print job.
pj.setPrintable(awPrintDoc);
pj.print();

Complete Source Code

string dataDir = "Your Document Directory";
Document doc = new Document(dataDir + "Rendering.docx");
// Create a print job to print our document with.
PrinterJob pj = PrinterJob.getPrinterJob();
// Initialize an attribute set with the number of pages in the document.
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(new PageRanges(1, doc.getPageCount()));
// Pass the printer settings along with the other parameters to the print document.
MultipagePrintDocument awPrintDoc = new MultipagePrintDocument(doc, 4, true, attributes);
// Pass the document to be printed using the print job.
pj.setPrintable(awPrintDoc);
pj.print();

Source code of MultipagePrintDocument

class MultipagePrintDocument implements Printable
{
    private final Document mDocument;
    private final int mPagesPerSheet;
    private final boolean mPrintPageBorders;
    private final AttributeSet mAttributeSet;
    /// <summary>
    /// The constructor of the custom PrintDocument class.
    /// </summary> 
    public MultipagePrintDocument(Document document, int pagesPerSheet, boolean printPageBorders,
                                  AttributeSet attributes) {
        if (document == null)
            throw new IllegalArgumentException("document");
        mDocument = document;
        mPagesPerSheet = pagesPerSheet;
        mPrintPageBorders = printPageBorders;
        mAttributeSet = attributes;
    }
    public int print(Graphics g, PageFormat pf, int page) {
        // The page start and end indices as defined in the attribute set.
        int[][] pageRanges = ((PageRanges) mAttributeSet.get(PageRanges.class)).getMembers();
        int fromPage = pageRanges[0][0] - 1;
        int toPage = pageRanges[0][1] - 1;
        Dimension thumbCount = getThumbCount(mPagesPerSheet, pf);
        // Calculate the page index which is to be rendered next.
        int pagesOnCurrentSheet = (int) (page * (thumbCount.getWidth() * thumbCount.getHeight()));
        // If the page index is more than the total page range then there is nothing
        // more to render.
        if (pagesOnCurrentSheet > (toPage - fromPage))
            return Printable.NO_SUCH_PAGE;
        // Calculate the size of each thumbnail placeholder in points.
        Point2D.Float thumbSize = new Point2D.Float((float) (pf.getImageableWidth() / thumbCount.getWidth()),
                (float) (pf.getImageableHeight() / thumbCount.getHeight()));
        // Calculate the number of the first page to be printed on this sheet of paper.
        int startPage = pagesOnCurrentSheet + fromPage;
        // Select the number of the last page to be printed on this sheet of paper.
        int pageTo = Math.max(startPage + mPagesPerSheet - 1, toPage);
        // Loop through the selected pages from the stored current page to calculated
        // last page.
        for (int pageIndex = startPage; pageIndex <= pageTo; pageIndex++) {
            // Calculate the column and row indices.
            int rowIdx = (int) Math.floor((pageIndex - startPage) / thumbCount.getWidth());
            int columnIdx = (int) Math.floor((pageIndex - startPage) % thumbCount.getWidth());
            // Define the thumbnail location in world coordinates (points in this case).
            float thumbLeft = columnIdx * thumbSize.x;
            float thumbTop = rowIdx * thumbSize.y;
            try {
                // Calculate the left and top starting positions.
                int leftPos = (int) (thumbLeft + pf.getImageableX());
                int topPos = (int) (thumbTop + pf.getImageableY());
                // Render the document page to the Graphics object using calculated coordinates
                // and thumbnail placeholder size.
                // The useful return value is the scale at which the page was rendered.
                float scale = mDocument.renderToSize(pageIndex, (Graphics2D) g, leftPos, topPos, (int) thumbSize.x,
                        (int) thumbSize.y);
                // Draw the page borders (the page thumbnail could be smaller than the thumbnail
                // placeholder size).
                if (mPrintPageBorders) {
                    // Get the real 100% size of the page in points.
                    Point2D.Float pageSize = mDocument.getPageInfo(pageIndex).getSizeInPoints();
                    // Draw the border around the scaled page using the known scale factor.
                    g.setColor(Color.black);
                    g.drawRect(leftPos, topPos, (int) (pageSize.x * scale), (int) (pageSize.y * scale));
                    // Draw the border around the thumbnail placeholder.
                    g.setColor(Color.red);
                    g.drawRect(leftPos, topPos, (int) thumbSize.x, (int) thumbSize.y);
                }
            } catch (Exception e) {
                // If there are any errors that occur during rendering then do nothing.
                // This will draw a blank page if there are any errors during rendering.
            }
        }
        return Printable.PAGE_EXISTS;
    }
    private Dimension getThumbCount(int pagesPerSheet, PageFormat pf) {
        Dimension size;
        // Define the number of the columns and rows on the sheet for the
        // Landscape-oriented paper.
        switch (pagesPerSheet) {
            case 16:
                size = new Dimension(4, 4);
                break;
            case 9:
                size = new Dimension(3, 3);
                break;
            case 8:
                size = new Dimension(4, 2);
                break;
            case 6:
                size = new Dimension(3, 2);
                break;
            case 4:
                size = new Dimension(2, 2);
                break;
            case 2:
                size = new Dimension(2, 1);
                break;
            default:
                size = new Dimension(1, 1);
                break;
        }
        // Swap the width and height if the paper is in the Portrait orientation.
        if ((pf.getWidth() - pf.getImageableX()) < (pf.getHeight() - pf.getImageableY()))
            return new Dimension((int) size.getHeight(), (int) size.getWidth());
        return size;
	}
}

Conclusion

Congratulations! You’ve successfully printed a Word document using Aspose.Words for Java. This step-by-step guide should help you integrate document printing into your Java applications seamlessly.

FAQs

Q1: Can I print specific pages of a document using Aspose.Words for Java?

Yes, you can specify the page range when printing a document. In the code example, we used attributes.add(new PageRanges(1, doc.getPageCount())) to print all pages. You can adjust the page range as needed.

Q2: Is Aspose.Words for Java suitable for batch printing?

Absolutely! Aspose.Words for Java is well-suited for batch printing tasks. You can iterate through a list of documents and print them one by one using similar code.

Q3: How can I handle printing errors or exceptions?

You should handle any potential exceptions that may occur during the printing process. Check the Aspose.Words for Java documentation for information on handling exceptions.

Q4: Can I customize the print settings further?

Yes, you can customize the print settings to meet your specific requirements. Explore the Aspose.Words for Java documentation to learn more about available print options.

Q5: Where can I get more help and support for Aspose.Words for Java?

For additional support and assistance, you can visit the Aspose.Words for Java forum.


Now that you have successfully learned how to print documents using Aspose.Words for Java, you can start implementing this functionality in your Java applications. Happy coding!