We have written a java code where we are trying to convert PDF to Bytearray.
But the problem is when we try to convert and try to print the converted output we get only 8 to 10 characters only .why is it so ? when i covert the whole pdf it has to be a large no of characters .
Here is my code:
public static void main(String[] args)
{
FileInputStream in = new FileInputStream(new File("C:\\test\\P12.pdf"));
FileOutputStream out = new FileOutputStream(new File("C:\\test\\pdfoutput.xml"));
byte[] buffer = new byte[1024];
ByteArrayOutputStream bs = new ByteArrayOutputStream();
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1)
{
bs.write(buffer, 0, bytesRead);
}
System.out.println(in);
byte[] bytes = bs.toByteArray();
System.out.println(bs.toString());
out.write(bytes);
}
A PDF is binary data. So a
toStringwill probably just output the so called PDF signature, PDF + version + some intentionally non-ASCII chars.As XML is even less likely.
There exists for instance the itext library for reading a PDF.
BTW
in.close()would be a good idea too.