-3

I am reading an existing PDF (INPUT) using iText:

PdfReader reader = new PdfReader(INPUT);

I am using this reader instance to manipulate the PDF, but the end result needs to be Base64 encoded (I need to insert it into a db2 database as a text BLOB). How can I make sure that iText's output is Base64 encoded?

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
Vins
  • 3
  • 1
  • 3
  • You'll have to clarify, because right now your question doesn't make sense. `PdfReader` doesn't produce PDF, so one way to interpret your question is: how can I make `PdfReader` accept INPUT that is encoded using base64. This question is easy to answer. `PdfReader` is used in combination with a writer class (`PdfCopy`, `PdfStamper`,...) that produces PDF. Another way to interpret your question is that you want to produce a PDF encoded in base64. That's also easy to achieve, but it's a totally different question than the first interpretation. All in all, the quality of this question is low. – Bruno Lowagie Apr 15 '16 at 09:02
  • I read a PDF with PdfReader , and I must to convert it to base64 to insert in a table in db2 – Vins Apr 15 '16 at 09:25
  • Then why didn't you say so in the first place? Please read my second answer. Also: if you don't need to change the PDF, then why are you using iText to encode the PDF??? I rewrote your question, but there's a chance you don't even need iText. – Bruno Lowagie Apr 15 '16 at 09:30
  • because if I use inputStream/outStream the pdf is not recreated well – Vins Apr 15 '16 at 09:34
  • So you don't need iText. You just want to encode a file to a Base64 string. I'll see if I can close the question. – Bruno Lowagie Apr 15 '16 at 09:35

1 Answers1

1

Your question is unclear, or at least ambiguous.

Asking How can I convert PdfReader pdfReader in Base64? doesn't make any sense, and that's why your question is unclear. Your problem is probably that you either have input that is encoded using Base64, or that you want output that you want to have encoded using Base64. That makes your question ambiguous.

If INPUT is String that represents a PDF file encoded using Base64, then you can decode it like this:

import com.itextpdf.text.pdf.codec.Base64;
...
PdfReader reader = new PdfReader(Base64.decode(INPUT));

If INPUT is (the path to) a PDF file that you want to manipulate with as result a PDF that is encoded as a Base64 String, then you can do this like this:

import com.itextpdf.text.pdf.codec.Base64;
PdfReader reader = new PdfReader(INPUT);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfStamper stamper = new PdfStamper(reader, baos);
// do stuff with stamper
stamper.close();
String base64 = Base64.encode(baos.toByteArray());

There may be other ways to produce the Base64 output, e.g. using some kind of Base64OutputStream, but I preferred to use the Base64 class that is shipped with iText.

If you don't need to manipulate the PDF, you don't even need iText. You can simply use the answer to this question: Out of memory when encoding file to base64

UPDATE:

in a comment to this answer, you wrote:

I have this:

byte[] bdata = blob.getBytes(1, (int) blob.length());
InputStream inputStream = blob.getBinaryStream();
String recuperataDaDb = convertStreamToString(inputStream);
byte[] decompilata = Base64.decode(recuperataDaDb);

I want write this "decompilata " in pdf file whit itext.jar

You have two options:

[1.] You don't need iText. You can simply write the byte[] to a file as described here: byte[] to file in Java

FileOutputStream stream = new FileOutputStream(path);
try {
    stream.write(bytes);
} finally {
    stream.close();
}

[2.] You read the answer to this question:

PdfReader reader = new PdfReader(decompilata);
FileOutputStream fos = new FileOutputStream(pathToFile);
PdfStamper stamper = new PdfStamper(reader, fos);
stamper.close();

I am very surprised by your eagerness to use iText. You really don't need iText to meet your requirement. All you need is an education on how to write Java code to perform some simple I/O.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Once more you are asking a question that doesn't make sense. What is the content of `byte[]`? If you don't answer that question, you shouldn't expect an answer. Maybe it's as simple as what is explained here: http://stackoverflow.com/questions/4350084/byte-to-file-in-java But of course, if `byte[]` isn't an actual PDF file (e.g. it's Base64 encoded), then the answer will be different. – Bruno Lowagie Apr 15 '16 at 09:56