wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes Problem in writing concatenated byteArray to a PDF file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Problem in writing concatenated byteArray to a PDF file" Watch "Problem in writing concatenated byteArray to a PDF file" New topic
Author

Problem in writing concatenated byteArray to a PDF file

S.R.K.Vivek Raju
Ranch Hand

Joined: Sep 23, 2004
Posts: 58
Hi All,

I have to two byte Array. I want to write them to one pdf file.
But the pdf displays only the second byte array content , i know its writing both because i concatenated the byte Arrays and written it as one byte array to the OutputStream.

my code is as follows
<code>
serverData = remoteServiceObject.doRemoteRequestProcess(clientRequest);
System.out.println("server data in tbq reports is" + serverData);
byte[] pdfReportByteArr = (byte[]) (serverData.get(0));
byte[] pdfReportByteArrDetailed = (byte[]) (serverData.get(1));

byte[] completeReport = concatenate(pdfReportByteArr,pdfReportByteArrDetailed);

System.err.println(pdfReportByteArr.length);
System.err.println(pdfReportByteArrDetailed.length);
System.err.println(completeReport.length);

response.setContentLength(completeReport.length);
response.setContentType("application/pdf");
response.setHeader("Expires", "0");
response.setHeader("Cache-Control",
"must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");

ServletOutputStream out = response.getOutputStream();
try {
out.write(completeReport);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (Exception e) {
}
}
}

protected byte[] concatenate(byte[] a, byte[] b) {
byte[] complete = new byte[a.length + b.length];
System.arraycopy(a, 0, complete , 0, a.length);
System.arraycopy(b, 0, complete , a.length, b.length);
return complete ;
}
</code>

Any help appreciated.

Thanks,
Regards,
S.R.K.Vivek Raju.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41601
    
  55
Does each array contain a complete PDF file? There's more to concatenating PDF pages into a document than appending their respective bytes. The iText library comes with a tool that can concatenate individual PDF files, and which can be used programmatically.

By the way, UBB code tags use square brackets, not angle brackets.
[ January 12, 2008: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
S.R.K.Vivek Raju
Ranch Hand

Joined: Sep 23, 2004
Posts: 58
Hi Ulf,

Yes each array contains a complete PDF file.
How could i append them as a file without appending the bytes.

Please give some details.

Thanks,

Regards,
Vivek Raju.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41601
    
  55
iText's com.lowagie.tools.concat_pdf class can do this. Have a look at the source code or the javadocs for more information about it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in writing concatenated byteArray to a PDF file