freemansoft / jacob-project

JACOB is a JAVA-COM Bridge that allows you to call COM Automation components from Java. It uses JNI to make native calls to the COM libraries. JACOB runs on x86 and x64 environments supporting 32 bit and 64 bit JVMs. This repository was migrated from Sourceforge 2020 Sep
GNU Lesser General Public License v2.1
530 stars 119 forks source link

Ask for help, print .docx file to physical printer by jacob (JAVA-COM Bridge) as Monochrome printing #51

Open yeminv opened 11 months ago

yeminv commented 11 months ago

I want to print .docx file to a physical printer by jacob. And it's work as below.

       <dependency>
            <groupId>com.jacob</groupId>
            <artifactId>jacob</artifactId>
            <version>1.20</version>
        </dependency>
public class JacobPrint {
    static void print( String filePath){// filePath="D:\\1.doc";
        ComThread.InitSTA();
        ActiveXComponent word=new ActiveXComponent("Word.Application");
        Dispatch doc=null;
        Dispatch.put(word, "Visible", new Variant(false));
        Dispatch docs=word.getProperty("Documents").toDispatch();
        doc=Dispatch.call(docs, "Open", filePath).toDispatch();

        try {
            Dispatch.call(doc, "PrintOut");
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                if(doc!=null){
                    Dispatch.call(doc, "Close",new Variant(0));
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
            ComThread.Release();
        }
}

But it is color printing. How do I control it to print in color or monochrome?

I have reference to Microsoft's official documentation: Printer.ColorMode property (Access)

But it seems to only work in the Access app, not in Word.

Thanks a lot!

EJP286CRSKW commented 10 months ago

This isn't a Jacob issue. Jacob will execute whatever calls you need to make. It's a question about the Microsoft COM API for Word.