Solved this problem - here is the key code.
byte[] byteBuffer = output.toByteArray();
PdfReader reader = new PdfReader( byteBuffer );
//add all pages to one document
for ( int page = 1; page <= reader.getNumberOfPages(); page++ )
{
writer.addPage( writer.getImportedPage
( reader, page ) );
}
If anyone has a better way let me know. Thanks.
On Jul 6, 2007, at 9:25 AM, Peter Gershkovich wrote:
What is the best way to concatenate several pdf files into one
document considering that I am getting pdf documents from a database:
Blob report = resultSet2.getBlob( "PdfDocument" );
Then I get binary stream and read it to an output. Below are the
details. I read in the book how one could combine a document using
PdfCopy. Can PdfCopy accept some kind of binary stream?
Below are details of my code. Appreciate your help.
Peter
try
{
String[] reportIdArray = reportId.split( "," );
//get collection of report ids
InputStream input = null;
// PdfCopy copy = new PdfCopy( document,
output );
ByteArrayOutputStream byteArrayOutputStream =
null;
for ( String reportPointer : reportIdArray )
{
String[] pointerArray = reportPointer.split
( ":" );
String sqlSelect2 = "select PdfDocument from
" + pointerArray[0] + " where id = ?";
PreparedStatement prepStatement2 =
conn.prepareStatement( sqlSelect2 );
prepStatement2.setString( 1, pointerArray
[1] );
ResultSet resultSet2 =
prepStatement2.executeQuery();
if ( resultSet2.next() )
{
Blob report = resultSet2.getBlob
( "PdfDocument" );
// setup the streams
input = report.getBinaryStream(); //get
input from blob
ByteArrayOutputStream output = new
ByteArrayOutputStream();
byte[] rb = new byte[1024];
int ch = 0;
// get content of a document
while ( ( ch = input.read( rb ) ) != -1 )
{
output.write( rb, 0, ch );
}
byte[] buf = output.toByteArray();
byteArrayOutputStream = new
ByteArrayOutputStream();
byteArrayOutputStream.write( buf );
input.close();
output.close();
}
resultSet2.close();
}
response.setContentType( "application/pdf" );
ServletOutputStream out =
response.getOutputStream();
byteArrayOutputStream.writeTo( out );
out.flush();
----------------------------------------------------------------------
---
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/
Peter Gershkovich M.D.
Associate Research Scientist
Senior Manager, Information Technology
Yale University School of Medicine
Department of Pathology
Phone: 203-785-2325
Fax: 203-785-7303
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/