cancel
Showing results for 
Search instead for 
Did you mean: 

Display PDF in different window by reading Binary file in MIME folder.

Former Member
0 Kudos

Hi all,

I've kept binary file in MIME folder and I'm trying to read the contents of it. and I'm trying to display contents in pdf format in separate window.

For this I was able to read the file by using FileInputStream

byte[] bytes = new byte[1000];
	try
    	{
	    	//File file = new File(WDURLGenerator.getResourcePath(wdComponentAPI.getDeployableObjectPart(),"data.bin"));
			File file = new File(WDURLGenerator.getResourcePath("test.bin"));
			FileInputStream is = new FileInputStream(file);
	    	long length = file.length();
			//Create the byte array to hold the data
			byte[] bytesA = new byte[(int)length];
			//Read in the bytes
			 
			 int offset = 0;
			 int numRead = 0;
			 while (offset < bytesA.length&& (numRead=is.read(bytesA, offset, bytesA.length-offset)) >= 0)
			 {
				 offset += numRead;
			 }
				
				is.close();
		    	
			bytes = bytesA;
			wdContext.currentContextElement().setPdfSource(bytes);
    	}
   		catch(Exception e)
   		{
			wdComponentAPI.getMessageManager().reportException("Error Reading File:"+e.getCause(),true);
			wdComponentAPI.getMessageManager().reportException("Error Reading File To String:"+e.toString(),true);
   		}

I've to show this in external window. I created attribute "pdfResource" of type binary

and declared in the source

private ISimpleType myPdfDoc;

After that I added this bit of code in Init Method

myPdfDoc = wdContext.getNodeInfo().getAttribute("pdfResource").getModifiableSimpleType();
((IWDModifiableBinaryType)myPdfDoc).setMimeType(WDWebResourceType.PDF);
((IWDModifiableBinaryType)myPdfDoc).setFileName("PDF");

After this I made an action handler which has following code.

final byte[] documentContent = bytes
final String docUrl = myPdfDoc.format( bytes );
String docUrl = myPdfDoc.format(bytes);

IWDWindow window = wdComponentAPI.getWindowManager().createExternalWindow(docURL,"Resource", true);
window.open();

But it is throwing Null Pointer exception for Init Method and also while displaying.

Where is that I'm faltering. Any Pointers will be help ful

Thanks

Srikant

Accepted Solutions (0)

Answers (2)

Answers (2)

Udo_Ahle
Explorer
0 Kudos

Hello,

is it possible to use the solution from

Former Member
0 Kudos

Check if the attribute name is "pdfResource"...Your setter (copied from your code & highlighted below) says that it is "pdfSource".


wdContext.currentContextElement().setPdfSource(bytes);

Former Member
0 Kudos

Hi Bala

I've tried that yet I get null pointer exception

Thanks

Srikant

Former Member
0 Kudos

First of all why do you need those three lines of code in your wdDoInit() ?

Simply, you may use this:

IWDCachedWebResource cachedResource = null;
 String url = null;
 byte[] pdf = wdContext.currentContextElement().getPdfSource();
 if (pdf != null) {
   cachedResource = WDWebResource.getWebResource(pdf, WDWebResourceType.PDF);
   url = cachedResource.getURL(); 
   }

Refer <a href="http://help.sap.com/saphelp_nw04s/helpdata/en/4a/fb8342a7d30d53e10000000a155106/content.htm">Utilizing the IWDCachedWebResource-API</a> for further assistance.

/* Changed the code */

Message was edited by: Bala Krishnan

Former Member
0 Kudos

Hi Bala

Sorry for getting back to you after long time. Well your piece of code has helped me finishing my requirement (95%),

Even i have tried getting the url information using InputStream (by passing the FileInputStream info into InputStream)

And in both these cases, a separate window opens but the adobe document doesn't open. do i need to install anything in my machine to view them.

Thanks

Srikant