1

这是我第一次尝试通过Vaadin组件打开使用iReport制作的 pdf。我在很多论坛上看到,但我无法理解。

现在我将尝试解释我的问题:

  1. 我用iReport创建了文件 (.jasper)
  2. 当我点击Vaadin按钮时,我想打开这个文件

你有什么可以帮助我的吗?

4

1 回答 1

1

更新:

您生成的 pdf 格式是 pdf 格式还是流格式?Firefox/Chrome:此浏览器有自己的“应用程序/pdf”查看器

Internet Explorer:为了在 IE 上查看 PDF,我使用的是 Adob​​e Acrobat Reader。Adobe Acrobat Reader 为浏览器安装插件。此插件检测应用程序/pdf 内容并在 Internet Explorer 中显示自己的查看器。

这是在 Vaadin 中制作的示例:

private void viewDocument() 
{
    final String retrievalName = "222.pdf"; 

    Window window = new Window();
    window.setCaption("View PDF");
    window.getContent().setSizeFull();

    final StreamResource resource = new StreamResource(new StreamResource.StreamSource()
    {
        public InputStream getStream()
        {
            try
            {
                byte[] DocContent = null;
                DocContent = getFileBytes("C:\\Temp\\222.pdf");
                return new ByteArrayInputStream(DocContent);
            }
            catch (Exception e1)
            {
                e1.printStackTrace();
                return null;
            }
        }
    }, retrievalName, getMainWindow().getApplication());

    Embedded c = new Embedded("", resource);
    c.setSizeFull();
    resource.setMIMEType("application/pdf");
    c.setType(Embedded.TYPE_BROWSER);
    window.addComponent(c);

    window.setModal(true);
    window.setWidth("90%");
    window.setHeight("90%");

    getMainWindow().addWindow(window);
}

/**
 * getFileBytes
 * 
 * @author NBochkarev
 * 
 * @param fileOut
 * @return
 * @throws IOException
 */
public static byte[] getFileBytes(String fileName) throws IOException
{
    ByteArrayOutputStream ous = null;
    InputStream ios = null;
    try
    {
        byte[] buffer = new byte[4096];
        ous = new ByteArrayOutputStream();
        ios = new FileInputStream(new java.io.File(fileName));
        int read = 0;
        while ((read = ios.read(buffer)) != -1)
            ous.write(buffer, 0, read);
    }
    finally
    {
        try
        {
            if (ous != null)
                ous.close();
        }
        catch (IOException e)
        {
            // swallow, since not that important
        }
        try
        {
            if (ios != null)
                ios.close();
        }
        catch (IOException e)
        {
            // swallow, since not that important
        }
    }
    return ous.toByteArray();
}
于 2013-07-10T10:28:20.553 回答