Java 如何在VAADIN中查看PDF文档

Java 如何在VAADIN中查看PDF文档,java,vaadin,document-view,Java,Vaadin,Document View,我想建立一个视图,在其中可以在Vaadin中显示PDF、Word、纯文本文档。有办法做到这一点吗?谢谢。您可以使用Vaadin文档中包含的样本 (见11.6.3.打印PDF) 其他有用的例子: package com.vaadin.book.examples.advanced; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; im

我想建立一个视图,在其中可以在Vaadin中显示PDF、Word、纯文本文档。有办法做到这一点吗?谢谢。

您可以使用Vaadin文档中包含的样本

(见11.6.3.打印PDF)

其他有用的例子:

        package com.vaadin.book.examples.advanced;

        import java.io.ByteArrayInputStream;
        import java.io.ByteArrayOutputStream;
        import java.io.InputStream;
        import java.util.Date;

        import javax.xml.transform.Result;
        import javax.xml.transform.Source;
        import javax.xml.transform.Transformer;
        import javax.xml.transform.TransformerFactory;
        import javax.xml.transform.sax.SAXResult;

        import org.apache.fop.apps.FOUserAgent;
        import org.apache.fop.apps.Fop;
        import org.apache.fop.apps.FopFactory;
        import org.apache.fop.apps.MimeConstants;

        import com.vaadin.book.examples.BookExampleBundle;
        import com.vaadin.terminal.ExternalResource;
        import com.vaadin.terminal.StreamResource;
        import com.vaadin.terminal.StreamResource.StreamSource;
        import com.vaadin.ui.Button;
        import com.vaadin.ui.Button.ClickEvent;
        import com.vaadin.ui.Button.ClickListener;
        import com.vaadin.ui.CustomComponent;
        import com.vaadin.ui.Label;
        import com.vaadin.ui.TextField;
        import com.vaadin.ui.VerticalLayout;
        import com.vaadin.ui.Window;

        public class PrintingExample extends CustomComponent implements BookExampleBundle {
            private static final long serialVersionUID = 97529549237L;

            public void init(String context) {
                VerticalLayout layout = new VerticalLayout();

                if ("this".equals(context))
                    printThisPage();
                else if ("open".equals(context))
                    printOpenedPage();
                else if ("nonblocking".equals(context))
                    printNonblockingPage();
                else if ("pdfgeneration".equals(context))
                    pdfgeneration(layout);
                else
                    setCompositionRoot(new Label("Invalid Context"));

                if (getCompositionRoot() == null)
                    setCompositionRoot(layout);
            }

            void printThisPage () {
                // BEGIN-EXAMPLE: advanced.printing.this
                final Button print = new Button("Print This Page");
                print.addListener(new ClickListener() {
                    private static final long serialVersionUID = 15335453452L;

                    public void buttonClick(ClickEvent event) {
                        print.getWindow().executeJavaScript("print();");
                    }
                });
                // END-EXAMPLE: advanced.printing.this

                setCompositionRoot(print);
            }

            void printOpenedPage () {
                // BEGIN-EXAMPLE: advanced.printing.open
                // A button to open the printer-friendly page.
                Button print = new Button("Click to Print");

                print.addListener(new Button.ClickListener() {
                    private static final long serialVersionUID = 6588417468637527327L;

                    public void buttonClick(ClickEvent event) {
                        // Create a window that contains what you want to print
                        Window window = new Window("Window to Print");

                        // Have some content to print
                        window.addComponent(new Label(
                                "<h1>Here's some dynamic content</h1>\n" +
                                "<p>This is to be printed to the printer.</p>",
                                Label.CONTENT_XHTML));

                        // Add the printing window as a new application-level
                        // window
                        getApplication().addWindow(window);

                        // Open it as a popup window with no decorations
                        getWindow().open(new ExternalResource(window.getURL()),
                                "_blank", 500, 200,  // Width and height 
                                Window.BORDER_NONE); // No decorations

                        // Print automatically when the window opens.
                        // This call will block until the print dialog exits!
                        window.executeJavaScript("print();");

                        // Close the window automatically after printing
                        window.executeJavaScript("self.close();");
                    }
                });
                // END-EXAMPLE: advanced.printing.open

                setCompositionRoot(print);
            }

            // TODO: This actually blocks also.
            void printNonblockingPage () {
                // A button to open the printer-friendly page.
                final Button print = new Button("Click to Print");

                print.addListener(new Button.ClickListener() {
                    private static final long serialVersionUID = 349852897523897L;

                    public void buttonClick(ClickEvent event) {
                        // Content to be printed. Must double-quote newlines.
                        String content = "<h1>Stuff to Print</h1>\\n" +
                                         "<p>Important stuff</p>\\n";

                        // The code to print and close the window
                        content += "<SCRIPT language=\"JavaScript\">" +
                                   "  print();" +
                                   "  close();" +
                                   "</SCRIPT>";

                        // Open the print window
                        String js = "popup = window.open('', 'mywindow','status=1,width=350,height=150');\n" +
                                    "popup.document.write('"+content+"');\n";
                        print.getWindow().executeJavaScript(js);
                    }
                });

                setCompositionRoot(print);
            }

            public final static String pdfgenerationDescription =
                "<h1>Generating a Printable PDF</h1>" +
                "<p>You can generate a PDF file dynamically using a <b>StreamResource</b>. The following example does it using the Apache FOP.</p>";

            // BEGIN-EXAMPLE: advanced.printing.pdfgeneration
            /** Generates the PDF dynamically when requested by HTTP. */
            class MyPdfSource implements StreamSource {
                private static final long serialVersionUID = 6580720404794033932L;

                String name; // A trivial content data model

                /** Constructor gets a content data model as parameter */
                public MyPdfSource(String name) {
                    this.name = name;
                }

                @Override
                public InputStream getStream() {
                    // Generate the FO content. You could use the Java DOM API
                    // here as well and pass the DOM to the transformer.
                    String fo = "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
                    "<fo:root xmlns:fo='http://www.w3.org/1999/XSL/Format'>\n"+
                    "<fo:layout-master-set>"+
                    "  <fo:simple-page-master master-name='A4' margin='2cm'>"+
                    "    <fo:region-body />"+
                    "  </fo:simple-page-master>"+
                    "</fo:layout-master-set>"+
                    "<fo:page-sequence master-reference='A4'>"+
                    "    <fo:flow flow-name='xsl-region-body'>"+
                    "    <fo:block text-align='center'>"+
                    "Hello There, "+ name + "!</fo:block>"+
                    "  </fo:flow>"+
                    "</fo:page-sequence>"+
                    "</fo:root>\n";
                    ByteArrayInputStream foStream =
                        new ByteArrayInputStream(fo.getBytes());

                    // Basic FOP configuration. You could create this object
                    // just once and keep it.
                    FopFactory fopFactory = FopFactory.newInstance();
                    fopFactory.setStrictValidation(false); // For an example

                    // Configuration for this PDF document - mainly metadata
                    FOUserAgent userAgent = fopFactory.newFOUserAgent();
                    userAgent.setProducer("My Vaadin Application");
                    userAgent.setCreator("Me, Myself and I");
                    userAgent.setAuthor("Da Author");
                    userAgent.setCreationDate(new Date());
                    userAgent.setTitle("Hello to " + name);
                    userAgent.setKeywords("PDF Vaadin example");
                    userAgent.setTargetResolution(300); // DPI

                    // Transform to PDF
                    ByteArrayOutputStream fopOut = new ByteArrayOutputStream();
                    try {
                        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF,
                                userAgent, fopOut);
                        TransformerFactory factory =
                            TransformerFactory.newInstance();
                        Transformer transformer = factory.newTransformer();
                        Source src = new
                            javax.xml.transform.stream.StreamSource(foStream);
                        Result res = new SAXResult(fop.getDefaultHandler());
                        transformer.transform(src, res);
                        fopOut.close();
                        return new ByteArrayInputStream(fopOut.toByteArray());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    return null;
                }
            }

            void pdfgeneration(VerticalLayout layout) {
                // A user interface for a data model from which
                // the PDF is generated.
                final TextField name = new TextField("Name");
                name.setValue("Slartibartfast");

                Button print = new Button("Open PDF");
                print.addListener(new ClickListener() {
                    private static final long serialVersionUID = 1269425538593656695L;

                    @Override
                    public void buttonClick(ClickEvent event) {
                        // Create the PDF source and pass the data model to it
                        StreamSource source =
                            new MyPdfSource((String) name.getValue());

                        // Create the stream resource and give it a file name
                        String filename = "pdf_printing_example.pdf";
                        StreamResource resource = new StreamResource(source,
                                filename, getApplication());

                        // These settings are not usually necessary. MIME type
                        // is detected automatically from the file name, but
                        // setting it explicitly may be necessary if the file
                        // suffix is not ".pdf".
                        resource.setMIMEType("application/pdf");
                        resource.getStream().setParameter("Content-Disposition",
                                "attachment; filename="+filename);

                        // Open it in this window - this will either launch
                        // PDF viewer or let the user download the file. Could
                        // use "_blank" target to open in another window, but
                        // may not be necessary.
                        getWindow().open(resource);
                    }
                });

                layout.addComponent(name);
                layout.addComponent(print);
            }
            // END-EXAMPLE: advanced.printing.pdfgeneration
        }
package com.vaadin.book.examples.advanced;
导入java.io.ByteArrayInputStream;
导入java.io.ByteArrayOutputStream;
导入java.io.InputStream;
导入java.util.Date;
导入javax.xml.transform.Result;
导入javax.xml.transform.Source;
导入javax.xml.transform.Transformer;
导入javax.xml.transform.TransformerFactory;
导入javax.xml.transform.sax.SAXResult;
导入org.apache.fop.apps.FOUserAgent;
导入org.apache.fop.apps.fop;
导入org.apache.fop.apps.FopFactory;
导入org.apache.fop.apps.MimeConstants;
导入com.vaadin.book.examples.BookExampleBundle;
导入com.vaadin.terminal.ExternalResource;
导入com.vaadin.terminal.StreamResource;
导入com.vaadin.terminal.StreamResource.StreamSource;
导入com.vaadin.ui.Button;
导入com.vaadin.ui.Button.ClickEvent;
导入com.vaadin.ui.Button.ClickListener;
导入com.vaadin.ui.CustomComponent;
导入com.vaadin.ui.Label;
导入com.vaadin.ui.TextField;
导入com.vaadin.ui.VerticalLayout;
导入com.vaadin.ui.Window;
公共类PrintingExample扩展CustomComponent实现BookExampleBundle{
私有静态最终长serialVersionUID=97529549237L;
公共void init(字符串上下文){
VerticalLayout布局=新建VerticalLayout();
如果(“this”.equals(上下文))
打印此页();
else if(“打开”。等于(上下文))
printOpenedPage();
else if(“非阻塞”。等于(上下文))
打印非阻塞页面();
else if(“pdfgeneration”.equals(上下文))
PDF发电(布局);
其他的
setCompositionRoot(新标签(“无效上下文”);
如果(getCompositionRoot()==null)
setCompositionRoot(布局);
}
作废打印此页(){
//BEGIN-EXAMPLE:advanced.printing.this
最终按钮打印=新按钮(“打印此页面”);
print.addListener(新建ClickListener(){
私有静态最终长serialVersionUID=15335453452L;
公共作废按钮单击(单击事件){
print.getWindow().executeJavaScript(“print();”);
}
});
//结束示例:advanced.printing.this
setCompositionRoot(打印);
}
作废打印打开页(){
//BEGIN-EXAMPLE:advanced.printing.open
//打开打印机友好页面的按钮。
按钮打印=新按钮(“点击打印”);
print.addListener(新建按钮。单击Listener()){
私有静态最终长serialVersionUID=6588417468637527327L;
公共作废按钮单击(单击事件){
//创建包含要打印内容的窗口
窗口=新窗口(“打印窗口”);
//有一些内容要打印吗
window.addComponent(新标签(
“这里有一些动态内容\n”+
“这将被打印到打印机上。

”, Label.CONTENT(XHTML)); //将打印窗口添加为新的应用程序级别 //窗口 getApplication().addWindow(窗口); //将其作为弹出窗口打开,不带任何装饰 getWindow().open(新的外部资源(window.getURL()), “_blank”,500,200,//宽度和高度 窗口。边框(无);//无装饰 //窗口打开时自动打印。 //此调用将被阻止,直到打印对话框退出! executeJavaScript(“print();”); //打印后自动关闭窗口 executeJavaScript(“self.close();”); } }); //结束示例:advanced.printing.open setCompositionRoot(打印); } //TODO:这实际上也会阻塞。 无效打印非阻止页(){ //打开打印机友好页面的按钮。 最终按钮打印=新按钮(“点击打印”); print.addListener(新建按钮。单击Listener()){ 私有静态最终长serialVersionUID=349852897523897L; 公共作废按钮单击(单击事件){ //要打印的内容。必须双引号换行。 String content=“要打印的内容\\n”+ “重要的东西

\\n”; //用于打印和关闭窗口的代码 内容+=“”+ “打印();”+ “关闭();”+ ""; //打开打印窗口 String js=“popup=window.open(“”,'mywindow','status=1,width=350,height=150');\n”+ “popup.document.write(“+content+”);\n”; print.getWindow().executeJavaScript(js);
        package com.vaadin.book.examples.advanced;

        import java.io.ByteArrayInputStream;
        import java.io.ByteArrayOutputStream;
        import java.io.InputStream;
        import java.util.Date;

        import javax.xml.transform.Result;
        import javax.xml.transform.Source;
        import javax.xml.transform.Transformer;
        import javax.xml.transform.TransformerFactory;
        import javax.xml.transform.sax.SAXResult;

        import org.apache.fop.apps.FOUserAgent;
        import org.apache.fop.apps.Fop;
        import org.apache.fop.apps.FopFactory;
        import org.apache.fop.apps.MimeConstants;

        import com.vaadin.book.examples.BookExampleBundle;
        import com.vaadin.terminal.ExternalResource;
        import com.vaadin.terminal.StreamResource;
        import com.vaadin.terminal.StreamResource.StreamSource;
        import com.vaadin.ui.Button;
        import com.vaadin.ui.Button.ClickEvent;
        import com.vaadin.ui.Button.ClickListener;
        import com.vaadin.ui.CustomComponent;
        import com.vaadin.ui.Label;
        import com.vaadin.ui.TextField;
        import com.vaadin.ui.VerticalLayout;
        import com.vaadin.ui.Window;

        public class PrintingExample extends CustomComponent implements BookExampleBundle {
            private static final long serialVersionUID = 97529549237L;

            public void init(String context) {
                VerticalLayout layout = new VerticalLayout();

                if ("this".equals(context))
                    printThisPage();
                else if ("open".equals(context))
                    printOpenedPage();
                else if ("nonblocking".equals(context))
                    printNonblockingPage();
                else if ("pdfgeneration".equals(context))
                    pdfgeneration(layout);
                else
                    setCompositionRoot(new Label("Invalid Context"));

                if (getCompositionRoot() == null)
                    setCompositionRoot(layout);
            }

            void printThisPage () {
                // BEGIN-EXAMPLE: advanced.printing.this
                final Button print = new Button("Print This Page");
                print.addListener(new ClickListener() {
                    private static final long serialVersionUID = 15335453452L;

                    public void buttonClick(ClickEvent event) {
                        print.getWindow().executeJavaScript("print();");
                    }
                });
                // END-EXAMPLE: advanced.printing.this

                setCompositionRoot(print);
            }

            void printOpenedPage () {
                // BEGIN-EXAMPLE: advanced.printing.open
                // A button to open the printer-friendly page.
                Button print = new Button("Click to Print");

                print.addListener(new Button.ClickListener() {
                    private static final long serialVersionUID = 6588417468637527327L;

                    public void buttonClick(ClickEvent event) {
                        // Create a window that contains what you want to print
                        Window window = new Window("Window to Print");

                        // Have some content to print
                        window.addComponent(new Label(
                                "<h1>Here's some dynamic content</h1>\n" +
                                "<p>This is to be printed to the printer.</p>",
                                Label.CONTENT_XHTML));

                        // Add the printing window as a new application-level
                        // window
                        getApplication().addWindow(window);

                        // Open it as a popup window with no decorations
                        getWindow().open(new ExternalResource(window.getURL()),
                                "_blank", 500, 200,  // Width and height 
                                Window.BORDER_NONE); // No decorations

                        // Print automatically when the window opens.
                        // This call will block until the print dialog exits!
                        window.executeJavaScript("print();");

                        // Close the window automatically after printing
                        window.executeJavaScript("self.close();");
                    }
                });
                // END-EXAMPLE: advanced.printing.open

                setCompositionRoot(print);
            }

            // TODO: This actually blocks also.
            void printNonblockingPage () {
                // A button to open the printer-friendly page.
                final Button print = new Button("Click to Print");

                print.addListener(new Button.ClickListener() {
                    private static final long serialVersionUID = 349852897523897L;

                    public void buttonClick(ClickEvent event) {
                        // Content to be printed. Must double-quote newlines.
                        String content = "<h1>Stuff to Print</h1>\\n" +
                                         "<p>Important stuff</p>\\n";

                        // The code to print and close the window
                        content += "<SCRIPT language=\"JavaScript\">" +
                                   "  print();" +
                                   "  close();" +
                                   "</SCRIPT>";

                        // Open the print window
                        String js = "popup = window.open('', 'mywindow','status=1,width=350,height=150');\n" +
                                    "popup.document.write('"+content+"');\n";
                        print.getWindow().executeJavaScript(js);
                    }
                });

                setCompositionRoot(print);
            }

            public final static String pdfgenerationDescription =
                "<h1>Generating a Printable PDF</h1>" +
                "<p>You can generate a PDF file dynamically using a <b>StreamResource</b>. The following example does it using the Apache FOP.</p>";

            // BEGIN-EXAMPLE: advanced.printing.pdfgeneration
            /** Generates the PDF dynamically when requested by HTTP. */
            class MyPdfSource implements StreamSource {
                private static final long serialVersionUID = 6580720404794033932L;

                String name; // A trivial content data model

                /** Constructor gets a content data model as parameter */
                public MyPdfSource(String name) {
                    this.name = name;
                }

                @Override
                public InputStream getStream() {
                    // Generate the FO content. You could use the Java DOM API
                    // here as well and pass the DOM to the transformer.
                    String fo = "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
                    "<fo:root xmlns:fo='http://www.w3.org/1999/XSL/Format'>\n"+
                    "<fo:layout-master-set>"+
                    "  <fo:simple-page-master master-name='A4' margin='2cm'>"+
                    "    <fo:region-body />"+
                    "  </fo:simple-page-master>"+
                    "</fo:layout-master-set>"+
                    "<fo:page-sequence master-reference='A4'>"+
                    "    <fo:flow flow-name='xsl-region-body'>"+
                    "    <fo:block text-align='center'>"+
                    "Hello There, "+ name + "!</fo:block>"+
                    "  </fo:flow>"+
                    "</fo:page-sequence>"+
                    "</fo:root>\n";
                    ByteArrayInputStream foStream =
                        new ByteArrayInputStream(fo.getBytes());

                    // Basic FOP configuration. You could create this object
                    // just once and keep it.
                    FopFactory fopFactory = FopFactory.newInstance();
                    fopFactory.setStrictValidation(false); // For an example

                    // Configuration for this PDF document - mainly metadata
                    FOUserAgent userAgent = fopFactory.newFOUserAgent();
                    userAgent.setProducer("My Vaadin Application");
                    userAgent.setCreator("Me, Myself and I");
                    userAgent.setAuthor("Da Author");
                    userAgent.setCreationDate(new Date());
                    userAgent.setTitle("Hello to " + name);
                    userAgent.setKeywords("PDF Vaadin example");
                    userAgent.setTargetResolution(300); // DPI

                    // Transform to PDF
                    ByteArrayOutputStream fopOut = new ByteArrayOutputStream();
                    try {
                        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF,
                                userAgent, fopOut);
                        TransformerFactory factory =
                            TransformerFactory.newInstance();
                        Transformer transformer = factory.newTransformer();
                        Source src = new
                            javax.xml.transform.stream.StreamSource(foStream);
                        Result res = new SAXResult(fop.getDefaultHandler());
                        transformer.transform(src, res);
                        fopOut.close();
                        return new ByteArrayInputStream(fopOut.toByteArray());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    return null;
                }
            }

            void pdfgeneration(VerticalLayout layout) {
                // A user interface for a data model from which
                // the PDF is generated.
                final TextField name = new TextField("Name");
                name.setValue("Slartibartfast");

                Button print = new Button("Open PDF");
                print.addListener(new ClickListener() {
                    private static final long serialVersionUID = 1269425538593656695L;

                    @Override
                    public void buttonClick(ClickEvent event) {
                        // Create the PDF source and pass the data model to it
                        StreamSource source =
                            new MyPdfSource((String) name.getValue());

                        // Create the stream resource and give it a file name
                        String filename = "pdf_printing_example.pdf";
                        StreamResource resource = new StreamResource(source,
                                filename, getApplication());

                        // These settings are not usually necessary. MIME type
                        // is detected automatically from the file name, but
                        // setting it explicitly may be necessary if the file
                        // suffix is not ".pdf".
                        resource.setMIMEType("application/pdf");
                        resource.getStream().setParameter("Content-Disposition",
                                "attachment; filename="+filename);

                        // Open it in this window - this will either launch
                        // PDF viewer or let the user download the file. Could
                        // use "_blank" target to open in another window, but
                        // may not be necessary.
                        getWindow().open(resource);
                    }
                });

                layout.addComponent(name);
                layout.addComponent(print);
            }
            // END-EXAMPLE: advanced.printing.pdfgeneration
        }
Window window = new Window();
window.setWidth("90%");
window.setHeight("90%");
BrowserFrame e = new BrowserFrame("PDF File", new ExternalResource("http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_open_parameters.pdf"));
e.setWidth("100%");
e.setHeight("100%");
window.setContent(e);
window.center();
window.setModal(true);
UI.getCurrent().addWindow(window);