Read the BLOB (PDF content) from the database, edit and display the PDF file edited without creating a physical file

I use an Oracle database and save the contents of the PDF in a BLOB field.

I want to read the contents of the BLOB, and then edit and display the edited content.

I need to do an edit:

  • add title above blob content
  • add a watermark on each page
  • add footer to each page

Then I need to output the file without creating a physical file that is in the response stream.

I tried to achieve this using itext, but never reached anywhere. I'm stuck and not sure where to start.

In addition, sometimes I may need to combine the contents of the blob into one, but this is what will happen. Once upon a time in a million. So now this is not a problem ...

How can I fulfill my basic requirements for the above three steps using in java? Is this possible with Itext ?? Or is some other library available?

Database: Oracle 10g Release 2

OS: Linux Fedora / Redhat

Interface: Java / Servlet / JSP

EDIT

Here is what I tried to do

oracle.sql.BLOB blob = (BLOB) rs.getBlob("MYPDF");
byte[] bytes = blob.getBytes(1, (int) blob.length());
InputStream is = blob.getBinaryStream();
Document document=new Document();
ServletOutputStream servletOutputStream = response.getOutputStream();
PdfWriter writer=PdfWriter.getInstance(document, servletOutputStream);
document.open();
document.add(new Paragraph("Some title"));
document.add(new Paragraph("Some title"));
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=output.pdf");
servletOutputStream.write(bytes, 0, bytes.length);
servletOutputStream.flush();
servletOutputStream.close();
document.close();

The program displays the PDF content in the BLOB field in the database and without a header.

and when I change the bit in the code (change the order of the last few lines) to:

document.close();
servletOutputStream.flush();
servletOutputStream.close();

I get a document with the title content in it and without the PDF content of the BLOB field. His first thing (servletoutputstreamstream / document), which is closed, was selected as an output.

, blob :

document.close();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=output.pdf");
servletOutputStream.write(bytes, 0, bytes.length);
servletOutputStream.flush();
servletOutputStream.close();

, - :

%PDF-1.4 %     2 0 obj <>stream x + r  26S 00SI 2P 5  14  sSJ2KrR5C  *P B 5 +  k)&  endstream endobj 4 0 obj <<<>>>/MediaBox[0 0 595 842]>> endobj 1 0 obj <> endobj 3 0 obj <> endobj 5 0 obj <> endobj 6 0 obj <> endobj xref 0 7 0000000000 65535 f 0000000304 00000 n 0000000015 00000 n 0000000392 00000 n 0000000147 00000 n 0000000443 00000 n 0000000488 00000 n trailer <]/Info 6 0 R/Size 7>> startxref 620 %%EOF 

, pdf .

, ...

UPDATE (, BLOB):

Document document = new Document(PageSize.A4, 108, 72, 30, 72);
PdfWriter writer = PdfWriter.getInstance(document, outputstream);

document.open();

///-----Added Some Title----///

rs = stmt.executeQuery(queryToGetBLOBCONTENT);

if (rs.next()) {


response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=watermark.pdf");
oracle.sql.BLOB blob = (BLOB) rs.getBlob("MYPDF");
byte[] bytes = blob.getBytes(1, (int) blob.length());
InputStream is = blob.getBinaryStream();
PdfReader pdfReader = new PdfReader(is, bytes);
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
PdfContentByte cb = writer.getDirectContent(); // Holds the PDF
PdfImportedPage page;
int currentPageNumber = 0;
int pageOfCurrentReaderPDF = 0;
while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
    if (pageOfCurrentReaderPDF > 0) {
        document.newPage();
    }
    pageOfCurrentReaderPDF++;
    currentPageNumber++;
    page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
    cb.addTemplate(page, 0, 0);
}
pageOfCurrentReaderPDF = 0;
outputstream.flush();
document.close();
outputstream.close();

}

, BLOB , - .

, , PDfreader, , (.. document.close(), )

? , .

+5
2

, servletOutputStream, :

+2

, PDF InputStream iText , , OutputStream . , ( " " ), :

InputStream input = resultSet.getBinaryStream("columnname");
PdfReader reader = new PdfReader(input);
// ...

OutputStream output = response.getOutputStream();
PdfWriter pdfWriter = PdfWriter.getInstance(document, output);
// ...

ByteArrayOutputStream.


:

  • , PdfWriter.

    response.setContentType("application/pdf");
    response.setHeader("Content-Disposition", "attachment; filename=output.pdf");
    PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
    // ...
    
  • . .

    byte[] bytes = blob.getBytes(1, (int) blob.length());
    servletOutputStream.write(bytes, 0, bytes.length);
    servletOutputStream.flush();
    servletOutputStream.close();
    

    PdfWriter . .

+2

All Articles