Java 如何在SWT shell中以只读模式打开word文档

Java 如何在SWT shell中以只读模式打开word文档,java,file,swt,Java,File,Swt,我有一个文件名列表和一个用于显示Word文件的组合。 以下代码将在屏幕中打开所选文件, 但是,我想以只读模式打开文件, 请任何人帮帮我 public class openDatafile { public void open_file(OleClientSite clientSite, OleFrame frame,String fname,String fpath) { String fileName=fname; String filePath=fp

我有一个文件名列表和一个用于显示Word文件的组合。 以下代码将在屏幕中打开所选文件, 但是,我想以只读模式打开文件, 请任何人帮帮我

public class openDatafile
{

public void open_file(OleClientSite clientSite, OleFrame frame,String fname,String fpath)
    {
        String fileName=fname;
        String filePath=fpath;
        String fullpath=filePath+"/"+fileName;

            if (fullpath != null) {

                clientSite.dispose();
                clientSite = new OleClientSite(frame, SWT.NONE, "Word.Document",new File(fullpath));
                clientSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
            }
            frame.redraw();
    }
}
请帮我做以下工作:- 1.以只读模式打开文件 2.关闭打开的文件


任何人请帮助我……

看看下面的代码。 由于
Application.ActiveDocument.ReadOnly
是不可写的,所以我使用了
Application.ActiveDocument.Final
,这非常有效

“返回或设置一个布尔值,该布尔值指示文档是否为最终文档。读/写。”

非常重要的一点是,您必须在
OleClientSite.doVerb()
之后调用它,否则
Application.ActiveDocument
不会初始化,也不会发生任何事情

/**
 * Sets a boolean that indicates whether a document is final (read only)<br/>
 * http://msdn.microsoft.com/en-us/library/office/ff838930(v=office.15).aspx<br/>
 * <br/>
 * IMPORTANT: Call after OleClientSite.doVerb(), otherwise Application.ActiveDocument is not initialized
 *
 * @param clientSite
 * @param readOnly
 */
public static void setFinal(OleClientSite clientSite, boolean readOnly) {
    OleAutomation oleAutomation = new OleAutomation(clientSite);
    int[] ids = oleAutomation.getIDsOfNames(new String[] { "Application" }); //$NON-NLS-1$
    if (ids != null) {
        Variant variant = oleAutomation.getProperty(ids[0]);
        if (variant != null) {
            OleAutomation application = variant.getAutomation();
            ids = application.getIDsOfNames(new String[] { "ActiveDocument" }); //$NON-NLS-1$
            if (ids != null) {
                variant = application.getProperty(ids[0]);
                if (variant != null) {
                    OleAutomation activeDocument = variant.getAutomation();
                    ids = activeDocument.getIDsOfNames(new String[] { "Final" }); //$NON-NLS-1$
                    if (ids != null) {
                        activeDocument.setProperty(ids[0], new Variant(readOnly));
                    }
                    activeDocument.dispose();
                }
            }
            application.dispose();
        }
    }
    oleAutomation.dispose();
}
/**
*设置一个布尔值,指示文档是否为最终文档(只读)
* http://msdn.microsoft.com/en-us/library/office/ff838930(v=office.15)。aspx
*
*重要提示:在OleClientSite.doVerb()之后调用,否则不会初始化Application.ActiveDocument * *@param clientSite *@param只读 */ 公共静态void setFinal(OleClientSite客户端站点,布尔只读){ OleAutomation OleAutomation=新的OleAutomation(clientSite); int[]ids=oleAutomation.getIDsOfNames(新字符串[]{“应用程序”});//$NON-NLS-1$ 如果(id!=null){ Variant=oleAutomation.getProperty(id[0]); if(变量!=null){ OleAutomation应用程序=variant.getAutomation(); ids=application.getIDsOfNames(新字符串[]{“ActiveDocument”});//$NON-NLS-1$ 如果(id!=null){ variant=application.getProperty(id[0]); if(变量!=null){ OleAutomation activeDocument=variant.getAutomation(); ids=activeDocument.getIDsOfNames(新字符串[]{“Final”});//$NON-NLS-1$ 如果(id!=null){ setProperty(ID[0],新变量(只读)); } activeDocument.dispose(); } } application.dispose(); } } oleAutomation.dispose(); }