Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to Convert Memory Stream Image to PDF inside Android Applications

sheraz786sheraz786 Posts: 13Member

This technical tip shows how developers can convert Memory Stream 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.

The following code snippet shows you how convert Memory Stream image to PDF file. `` try {

         // Create PDF document
         Pdf pdf = new Pdf();

         // Add a section into the PDF document
         Section sec1 = pdf.getSections().add();

         byte[] fileArray = null;

         // Get the Image contents into Byte Array

         try {
                fileArray = getBytesFromFile(new File("/mnt/sdcard/Aspose.jpg"));
             } catch (IOException e) {

         // Create an image object in the section
         aspose.pdf.Image img1 = new aspose.pdf.Image(sec1);

         // Add image object into the Paragraphs collection of the section

         // Set the Image file type

         // Create a BinayFileStream Object to hold byte array
         BinaryFileStream bstream = new BinaryFileStream(fileArray);
         // Set the image object to use BinaySgtream object

         // Save the PDF file"/mnt/sdcard/Image2PDF.pdf");

    }catch( ioe){
              }catch(Exception e){

            // Method Returns the contents of file in a byte array

            private static byte[] getBytesFromFile(File file) throws IOException {

            InputStream is = new FileInputStream(file);

            // Get the size of the file
            long length = file.length();

         * Ensure that file is not loarger than Integer.MAX_VALUE;

            if (length > Integer.MAX_VALUE) {
                System.out.println("File is too large to process");
                return null;

            // Create the byte array to hold the data
            byte[] bytes = new byte[(int)length];

            // Read in the bytes
            int offset = 0;
            int numRead = 0;
            while ( (offset < bytes.length)
                    ( (, offset, bytes.length-offset)) >= 0) ) {

                offset += numRead;


            // Ensure all the bytes have been read in
            if (offset < bytes.length) {
                throw new IOException("Could not completely read file " + file.getName());

            return bytes;



Sign In or Register to comment.