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.