Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [platform-swt-dev] Using Acrobat ActiveX control in SWT

The code below works for me.

Notes:
1) Until you load a document, Acrobat does not display anything (in my 
example, click on the load file button and select a pdf file).
2) Acrobat will not load a file successfully until after you have in place 
activated the control.  You were using OLE.OLEIVERB_UIACTIVATE which puts you into a state where it will display 
its content (if it has some) but will not respond to the user.  Different 
controls respond differently to the DoVerb command but most accept 
OLE.OLEIVERB_INPLACEACTIVATE. 
3) A successful return from doVerb is OLE.S_OK not OLE.OK.

        public static void main(String[] args) {
                Display display = new Display();
                final Shell shell = new Shell(display);
                shell.setLayout(new GridLayout());
                OleFrame pdfFrame = new OleFrame(shell, SWT.NONE);
                pdfFrame.setLayoutData(new GridData(GridData.FILL_BOTH));
                try {
                        final OleControlSite pdfSite = new OleControlSite(pdfFrame, SWT.NONE, "PDF.PdfCtrl.5");
                        pdfSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
                        Button b = new Button(shell, SWT.PUSH);
                        b.setText("Load file");
                        b.addSelectionListener(new SelectionAdapter() {
                                public void widgetSelected(SelectionEvent e) {
                                        FileDialog dialog = new FileDialog(shell, SWT.OPEN);
                                        dialog.setFilterExtensions(new String[] { "*.pdf" });
                                        dialog.setFilterNames(new String[] { "PDF Documents (*.pdf)" });
                                        String fileName = dialog.open();
                                        if (fileName == null)
                                                return;

                                        OleAutomation acrobat = new OleAutomation(pdfSite);
                                        int[] rgdispid = acrobat.getIDsOfNames(new String[] {"LoadFile"});
                                        Variant[] varArgs = new Variant[1];
                                        varArgs[0] = new Variant(fileName);
                                        acrobat.invoke(rgdispid[0], 
varArgs);
                                        acrobat.dispose();
                                }
                        });
                        shell.open();

                        while (!shell.isDisposed()) {
                                if (!display.readAndDispatch())
                                        display.sleep();
                        }

                } catch (SWTException ex) {
                        ex.printStackTrace();
                        // Creation may have failed because control is not installed on machine

                        System.out.println("Error: Could Not Create PDF Control");

                }
        }



Back to the top