Posted By

johansonkatherine on 03/27/14


Tagged

image as pdf to web document from convert memory stream source MemoryStream MemoryStreamimage ConversionCreate hard-drive PDFconvert PDFAndroid APIspecify


Versions (?)

How to Convert MemoryStream Image to PDF inside Android Applications


 / Published in: Java
 

URL: http://www.aspose.com/docs/display/pdfandroid/How+to+Convert+MemoryStream+Image+to+PDF

This technical tip shows how developers can convert MemoryStream image to PDF file inside their Android application. The Image class in Aspose.Pdf makes it possible to convert an image from various sources into PDF format. This may include an image from particular location over the hard-drive, an image from MemoryStream or an image at a web location. The ImageInfo class has a method named setMemoryData() which is used to specify MemoryStream as the image source.

  1. try {
  2.  
  3. // Create PDF document
  4. Pdfpdf = new Pdf();
  5.  
  6. // Add a section into the PDF document
  7. Section sec1 = pdf.getSections().add();
  8.  
  9. byte[] fileArray = null;
  10.  
  11. //=====================================================//
  12. // Get the Image contents into Byte Array
  13. //=====================================================//
  14.  
  15. try {
  16. fileArray = getBytesFromFile(new File("/mnt/sdcard/Aspose.jpg"));
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. }
  20.  
  21. // Create an image object in the section
  22. aspose.pdf.Image img1 = new aspose.pdf.Image(sec1);
  23.  
  24. // Add image object into the Paragraphs collection of the section
  25. sec1.getParagraphs().add(img1);
  26.  
  27. // Set the Image file type
  28. img1.getImageInfo().setImageFileType(ImageFileType.Jpeg);
  29.  
  30. // Create a BinayFileStream Object to hold byte array
  31. BinaryFileStreambstream = new BinaryFileStream(fileArray);
  32. // Set the image object to use BinaySgtream object
  33. img1.getImageInfo().setImageStream(bstream);
  34.  
  35. // Save the PDF file
  36. pdf.save("/mnt/sdcard/Image2PDF.pdf");
  37.  
  38. }catch(java.io.IOExceptionioe){
  39. System.out.println(ioe.getMessage());
  40. }catch(Exception e){
  41. System.out.println(e.getMessage());
  42. }
  43. }
  44.  
  45.  
  46. //=====================================================//
  47. // Method Returns the contents of file in a byte array
  48. //=====================================================//
  49.  
  50. private static byte[] getBytesFromFile(File file) throws IOException {
  51.  
  52.  
  53. // Get the size of the file
  54. long length = file.length();
  55.  
  56. /*
  57. * Ensure that file is not loarger than Integer.MAX_VALUE;
  58.   */
  59.  
  60. if (length >Integer.MAX_VALUE) {
  61. System.out.println("File is too large to process");
  62. return null;
  63. }
  64.  
  65. // Create the byte array to hold the data
  66. byte[] bytes = new byte[(int)length];
  67.  
  68. // Read in the bytes
  69. int offset = 0;
  70. intnumRead = 0;
  71. while ( (offset <bytes.length)
  72. &&
  73. ( (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) ) {
  74.  
  75. offset += numRead;
  76.  
  77. }
  78.  
  79. // Ensure all the bytes have been read in
  80. if (offset <bytes.length) {
  81. throw new IOException("Could not completely read file " + file.getName());
  82. }
  83.  
  84. is.close();
  85. return bytes;
  86.  
  87. }

Report this snippet  

You need to login to post a comment.