cancel
Showing results for 
Search instead for 
Did you mean: 

Displaying pdf file in interactive form

Former Member
0 Kudos

Hi all,

I want to display a pdf file in interactive form. The property pdfSource is bind to a context element of the same name. When I am trying to run my app I get a message that file was corrupted. I think that the problem is with the conversion string to byte[]. I am not very familiar with java so can you help me? Here is my code:

File file = new File("report.pdf");

FileReader fileReader = new FileReader(file);

BufferedReader bufferReader = new BufferedReader(fileReader);

String string = "", line = "";

while ((line = (String) bufferReader.readLine()) != null) string += line;

byte[] bytes = string.getBytes();

wdContext.currentContextElement().setPdfSource(bytes);

Thank you

Ivo

Accepted Solutions (1)

Accepted Solutions (1)

roberto_tagliento
Active Contributor
0 Kudos

PDF isn't a normal TEXT file.

It is a binary file!

Your error is

BufferedReader bufferReader = new BufferedReader(fileReader);

String string = "", line = "";

while ((line = (String) bufferReader.readLine()) != null) string += line;

roberto_tagliento
Active Contributor
0 Kudos

// Returns the contents of the file in a byte array.

public static byte[] getBytesFromFile(File file) throws IOException {

InputStream is = new FileInputStream(file);

// Get the size of the file

long length = file.length();

// You cannot create an array using a long type.

// It needs to be an int type.

// Before converting to an int type, check

// to ensure that file is not larger than Integer.MAX_VALUE.

if (length > Integer.MAX_VALUE) {

// File is too large

}

// Create the byte array to hold the data

byte[] bytes = new byte[(int)length];

// Read in the bytes

int offset = 0;

int numRead = 0;

while (offset < bytes.length

&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {

offset += numRead;

}

// Ensure all the bytes have been read in

if (offset < bytes.length) {

throw new IOException("Could not completely read file "+file.getName());

}

// Close the input stream and return bytes

is.close();

return bytes;

}

Answers (2)

Answers (2)

BeGanz
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Ivo,

is your application running on NW04 or NW04s? In both cases you can use the InteractiveForm UI element to display a <i>non-interactive</i> PDF resource in it by setting the mode property to USE_PDF. I described a solution, based on using a Modifiable Binary Type" (NW04) in this thread:

Regards, Bertram

Former Member
0 Kudos

Thanks to you all,

I have done it as Roberto said. Its working now.

Bertram: I have seen this thread and I was following your advice but I had a problem with the conversion to byte[].

Former Member
0 Kudos

Bertram: I have one more problem with Interactive form. When I want to close (or just change view using fire plug) a view in which interactive form with pdf document is, the browser freezes and I have to shoot it down. Do I have to close the PDF somehow or is the problem somewhere else?

Former Member
0 Kudos

Hi,

Let me know what your expecting to do ?

U are mapping only context node as pdfsource binary type.

can you brief me your requirement again.

Thanks

Lohi.