HI,

How can we print an MS Word document from Java ???
We can print .txt files, but while printing .doc, it gives junk chars in print out when we set
DocFlavor docFlavor = DocFlavor.INPUT_STREAM.AUTOSENSE;

I tried,
1.DocFlavor docFlavor = new DocFlavor("application/msword", "java.io.InputStream");

2. DocFlavor docFlavor = new DocFlavor("application/vnd.ms-word", "java.io.InputStream");

and got PrintJobFlavorException

It would be helpful if any one could help me out..

Thanks in advance..

Dani AI

Generated

Short answer: sending a Word (.doc/.docx) file’s raw bytes to the Java PrintService will usually produce “junk” or a PrintJobFlavorException because printers and print services expect a rendered page description (PDF/PS/PCL) or plain text — not Microsoft’s binary DOC stream. That is why saw garbage and why was correct that “you can’t” print a .doc by simply streaming its bytes to the printer. (docs.oracle.com)

Practical, reliable approaches (ranked by fidelity / convenience):

  • Convert the Word file to PDF (or PostScript) then print the PDF. Typical server-side pipeline: LibreOffice (headless) or JODConverter to produce a PDF, then send that PDF to the printer using Apache PDFBox (or the OS print command). Example conversion command:

    soffice --headless --convert-to pdf --outdir /tmp input.doc

    and a minimal PDFBox print sketch:

    try (PDDocument pdf = PDDocument.load(new File("out.pdf"))) {
        pdf.silentPrint(); // sends to default printer
    }

    This is widely used because it keeps fidelity without requiring Microsoft Office. (help.libreoffice.org)

Alternatives and trade-offs:

  • Apache POI or docx4j parse Word content and are great for extraction/templating, but they do not provide Word-level WYSIWYG rendering for printing. Use them when you only need text/images, not exact layout. (poi.apache.org)
  • Commercial libraries (Aspose.Words) can load, render and print Word documents from Java with high fidelity (paid). (tutorials.aspose.com)
  • On Windows only: automate Word via COM (JACOB or similar) to have Word render/print — high fidelity but platform‑specific and fragile. (github.com)

Notes/troubleshooting: headless LibreOffice instances should be serialized (or run in a controlled pool) because the UNO engine isn’t safely multi-threaded; fonts and printer drivers can change output, so verify PDF results before sending to production printers. If the goal is server-side automated printing, the LibreOffice → PDF → PDFBox pipeline is the most pragmatic starting point (POI/docx4j for content work, Aspose/JACOB for highest-fidelity/Windows-only needs). As suggested, POI is useful for document parsing; as suspected, converting to PDF first is the usual, practical solution. (jodconverter.github.io)

Recommended Answers

All 3 Replies

Hi everyone,

You can't, trust me i have tried

Richard West

Hi,

Even We have some requirement to print the Word document from JAVA. How can we achieve that?

One way we thought is to convert word to pdf and then pdf to ps to print the document.
Can anyone suggest some other solution or how to convert word to pdf from JAVA

Thanks in advance.

http://jakarta.apache.org and look for POI.
It's not perfect in that it doesn't understand all capabilities of MS Word but it understands pretty much all core capabilities.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.