How to Convert MemoryStream Image to PDF





1
Date Submitted Thu. Mar. 27th, 2014 7:27 AM
Revision 1 of 1
Helper johansonkatherine
Tags Convert | Image | MemoryStream | pdf | to
Comments 0 comments
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.


try {

     // Create PDF document
Pdfpdf = 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) {
e.printStackTrace();
         }

     // 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
sec1.getParagraphs().add(img1);

     // Set the Image file type
img1.getImageInfo().setImageFileType(ImageFileType.Jpeg);

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

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

}catch(java.io.IOExceptionioe){
                                System.out.println(ioe.getMessage());
        }catch(Exception e){
                                System.out.println(e.getMessage());
              }
}


        //=====================================================//
        // 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;
intnumRead = 0;
while ( (offset <bytes.length)
&&
( (numRead=is.read(bytes, 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());
        }

is.close();
return bytes;

        }

 

Katherine Johanson

Comments

There are currently no comments for this snippet.

Voting

Votes Down