I'm working on an application where i am getting the data in the form of byte array ,and i need to use this byte array for creating a new PDF file.How can i achieve this in android ?
Asked
Active
Viewed 1.9k times
3 Answers
2
Use FileOutputStream
and its write(byte[])
method.
# Assuming that you have data content which is ready to be writen in PDF.
There are also some pdf writers api available for android.
See:

GoozMeister
- 334
- 2
- 17

jmj
- 237,923
- 42
- 401
- 438
-
Hi Jigar, I tried this code FileOutputStream fileos=new FileOutputStream("Hello.pdf"); but when I am trying to open this file,I am getting an error saying that Adobe Reader could not open Hello.pdf because it is either not supported file type or because the file has been damaged... – Taruni Dec 27 '11 at 12:16
-
_Assuming that you have data content which is ready to be writen in PDF_ , See update – jmj Dec 27 '11 at 12:21
2
Here is one of the way of creating PDF from Byte Array.
File dir = Environment.getExternalStorageDirectory();
File assist = new File("/mnt/sdcard/Sample.pdf");
try {
InputStream fis = new FileInputStream(assist);
long length = assist.length();
if (length > Integer.MAX_VALUE) {
Log.e("MainActivity", "cannnottt readddd");
}
byte[] bytes = new byte[(int) length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead = fis.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
File data = new File(dir, "mydemo.pdf");
OutputStream op = new FileOutputStream(data);
op.write(bytes);
} catch (Exception ex) {
Log.e("MainActivity", "" + ex.getMessage())
}
-
-
When i am trying to open this file i am an error saying that Adobe reader couldnt open the file because it is either not supported file type or because the file has been damaged. – Taruni Dec 28 '11 at 06:45