Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Displaying content (e.g. PDF, excel, word) in an IFRAME

From time to time people as ask this question, or similar or related ones, on the list. So, I'll try to summarize the answer I gave on the list...

...

So, this class instead of implementing ILinkListener implements IResourceListener and generates an URL for the src attribute of IFRAME that points back to itself. The class receives an IResourceListener as an attribute on the constructor and delegates the production of the IFRAME contents to this IResourceListener. If you want to know which of the implementations of IResourceListener you need jsut just open it with your favorite IDE and search for all classes implementing it (on eclipse ctrEclipse you can easily do that typing Ctr-T)(wink).

Now that we have our main class let's use it to display a PDF.

...

Code Block
package some.example;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.wicket.markup.html.DynamicWebResource;

/**
 * @author Ernesto Reinaldo
 *
 */
public class MyPdfResource extends DynamicWebResource {

	private static final long serialVersionUID = 1L;

	static int BUFFER_SIZE = 10*1024;
	
	/**
	 * 
	 */
	public MyPdfResource() {
	}

	/* (non-Javadoc)
	 * @see org.apache.wicket.markup.html.DynamicWebResource#getResourceState()
	 */
	@Override
	protected ResourceState getResourceState() {
		return new ResourceState() {
			
			@Override
			public String getContentType() {
				return "application/pdf";
			}
			
			@Override
			public byte[] getData() {
				try {
					return bytes(MyPdfResource.class.getResourceAsStream("test.pdf"));
				} catch (Exception e) {
					return null;
				}
			}
		};
	}
	
	public static  byte[] bytes(InputStream is) throws IOException {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		copy(is, out);
		return out.toByteArray();
	}
	
	public static void copy(InputStream is, OutputStream os) throws IOException {
		byte[] buf = new byte[BUFFER_SIZE];
		while (true) {
			int tam = is.read(buf);
			if (tam == -1) {
				return;
			}
			os.write(buf, 0, tam);
		}
	}
}

This class just reads a PDF named test.pdf from the same package some.example. So, to make the example work take you favorite PDF file and drop it on that folder and rename it to test.pdf.

Then lets show this PDF in a Wicket panel:

Code Block

package some.example;
import org.apache.wicket.markup.html.panel.Panel;

public class MyPdfPanel extends Panel {

	private static final long serialVersionUID = 1L;

	public MyPdfPanel(String id) {
		super(id);
		
		setRenderBodyOnly(true);
		add(new DocumentInlineFrame("mypdf", new MyPdfResource()));
	}
}

and

Code Block

<html>
    <wicket:panel>
        <iframe wicket:id="mypdf" width="90%" height="300px"></iframe>
    </wicket:panel>
</html>

If you now include previous panel in a Wicket page you will be able to see your test PDF embedded on that page!