Alleni,
Thanks for the idea. I am working with several PDFs and need to edit,
order, and concatenate into a final PDF and then store as a BLOB back to teh
database without generating any physical files. For each PDF, I am actually
reading the BLOB into a BinaryStream, converting to a byte[], and then into
a PdfReader. I create a PdfReader[] so that I can reorder as needed and
work with each re-constituted PDF individually. When finished, I create a
PdfCopy(document, ByteArrayOutputStream) as my writer and read each
PdfReader adding ImportedPages one-by-one.
ImportedPage page = writer.getImportedPage(readers[j], k);
writer.addPage(page);
I convert my BinaryArrayOutputStream to a byte[] which I insert into the
database as a BLOB as follows...
//**************** get BLOB into byte[] ******************//
public static byte[] myGetBlob(ResultSet result, String columnName) throws
java.sql.SQLException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try{
oracle.sql.BLOB blob =
((oracle.jdbc.OracleResultSet)result).getBLOB(columnName);
InputStream inputStream = blob.getBinaryStream();
int bytesRead = 0;
while((bytesRead = inputStream.read()) != -1){
outputStream.write(bytesRead); }// End while loop
inputStream.close();
outputStream.close();
}catch(java.io.IOException e){ throw new
java.sql.SQLException(e.getMessage()); }// End try-catch
return outputStream.toByteArray();
}// End myGetOracleBlob
//************* write byte[] back to BLOB after editing & manipulating
**************//
...
preparedStatement = conn.prepareStatement("SELECT FID, BLOB_CONTENT "
+ "FROM MY_TABLE_NAME WHERE FID = ? FOR UPDATE");
preparedStatement.setString(1, getFID()+"");
resultSet = preparedStatement.executeQuery();
if(resultSet.next()) {
...
ByteArrayInputStream inputStream = new ByteArrayInputStream(data);//<--
data is byte[]
oracle.sql.BLOB blob =
((oracle.jdbc.OracleResultSet)resultSet).getBLOB(order);
OutputStream outputStream = blob.getBinaryOutputStream();
writer = new PdfCopy(FinalDocumentPDFFile, outputStream);
int bytesRead = 0;
// Read input stream of BLOB data
while((bytesRead = inputStream.read()) != -1){
outputStream.write(bytesRead); }// End while loop
...
//**************************************//
I hope these snippets can help someone else as well.
Ed
//************ The End ? **************************//
Alleni wrote:
>
> Your input stream and output stream cannot be from the same blob. Just
> like you cannot use the same file. As you could end up overwriting
> what you are reading.
>
> Try creating a temporary blob for writing (use BLOB.createTemporary
> method) and save this back to the database.
>
>
>
> On Mon, Sep 15, 2008 at 3:34 PM, Ed M <[EMAIL PROTECTED]> wrote:
>>
>> I have PDF documents stored in an Oracle database as a BLOB which I need
>> to
>> generate a new consolidated PDF from and store as new record in Oracle
>> Database as a new Blob. I have searched the forums for a similar
>> situation
>> and I have the iText book, but I find that I still could use some
>> assistance
>> and/or direction.
>>
>> I am new to iText, Blobs, and Input/Output Stream programming so please
>> excuse my ignorance if my code contains errors. One unique requirement
>> is
>> that I do NOT generate physical files on the server. Currently, I
>> generate
>> an iText Document for the "output" stream using a PdfWriter without
>> error.
>> When I attempt to open (read) a stored PDF (Blob) from the database using
>> an
>> Input Stream, I encounter an IO Exception that the "PDF header signature
>> not
>> found" constructing the PdfReader. Essentially, I want to read multiple
>> PDF
>> documents stored as Blobs and append them to a new PDF document and then
>> store it as a Blob without generating any physical files on the server.
>> Here is the Reader's Digest version of my Java source code.
>>
>>
>> ...
>> Document finalDocument = new Document(PageSize.A5, 36, 36, 108, 108);
>>
>> ... // query database to populate OracleResultSet with contents of BLOB
>> column
>> OracleResultSet blobDetails = (OracleResultSet) stmt.executeQuery("Select
>> BLOB_CONTENT from table");
>> if(blobDetails.next()){
>> InputStream input = blobDetails.getBLOB(1).getBinaryStream();
>> long mylong = blobDetails.getBLOB(1).length();
>> OutputStream fos = blobDetails.getBLOB(1).setBinaryStream(mylong);
>> PdfWriter writer = PdfWriter.getInstance(finalDocument, fos); // <--
>> this
>> is where the error is thrown
>> finalDocument.open();
>>
>> // get input stream for file to append
>> blobDetails.next();
>> InputStream inputAppend = blobDetails.getBLOB(1).getBinaryStream();
>> PdfReader otherPDF = new PdfReader(inputAppend);
>> // add page by page
>> for(int x=1; x<=reader.getNumberOfPages(); x++){
>> PdfImportedPage page = writer.getImportedPage(reader,x);
>> Image image = Image.getInstance(page);
>> finalDocument.add(image);
>> }
>>
>> // save document back to database
>> if(finalDocument.isOpen()){
>> try{
>> byte[] data = new byte[writer.getCurrentDocumentSize()];
>> Statement stmt = (OraclePreparedStatement)
>> conn.prepareStatement("Insert into upload_table(BLOB_CONTENT) values
>> (?)");
>> stmt.setObject(1,data);
>> stmt.executeUpdate();
>> }catch(Exception e){e.printStackTrace();}
>> }
>>
>> // close document
>> finalDocument.close();
>> --
>> View this message in context:
>> http://www.nabble.com/Editing-PDF-Stored-as-a-BLOB-without-Re-Constituting-File-tp19498541p19498541.html
>> Sent from the iText - General mailing list archive at Nabble.com.
>>
>>
>> -------------------------------------------------------------------------
>> This SF.Net email is sponsored by the Moblin Your Move Developer's
>> challenge
>> Build the coolest Linux based applications with Moblin SDK & win great
>> prizes
>> Grand prize is a trip for two to an Open Source event anywhere in the
>> world
>> http://moblin-contest.org/redirect.php?banner_id=100&url=/
>> _______________________________________________
>> iText-questions mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/itext-questions
>>
>> Buy the iText book: http://www.1t3xt.com/docs/book.php
>>
>
> -------------------------------------------------------------------------
> This SF.Net email is sponsored by the Moblin Your Move Developer's
> challenge
> Build the coolest Linux based applications with Moblin SDK & win great
> prizes
> Grand prize is a trip for two to an Open Source event anywhere in the
> world
> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> _______________________________________________
> iText-questions mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/itext-questions
>
> Buy the iText book: http://www.1t3xt.com/docs/book.php
>
>
--
View this message in context:
http://www.nabble.com/Editing-PDF-Stored-as-a-BLOB-without-Re-Constituting-File-tp19498541p19561351.html
Sent from the iText - General mailing list archive at Nabble.com.
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://www.1t3xt.com/docs/book.php