• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Select & Convert Memory Stream Image to PDF File Using Aspose.Pdf

Ranch Hand
Posts: 714
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's New in this Release?

This technical tip shows how to convert Memory Stream Image to PDF using Aspose.Pdf for Java. Image class in Aspose.Pdf offers the capability 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 image placed over some web location. ImageInfo class has a method named setMemoryData() which is used specify MemoryStream as image source.


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("d:/pdftest/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


}catch(java.io.IOException 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)


                ( (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());



        return bytes;


Overview: Aspose.Pdf for Java

Aspose.Pdf is a Java PDF component to create PDF documents without using Adobe Acrobat. It supports Floating box, PDF form field, PDF attachments, security, Foot note & end note, Multiple columns document, Table of Contents, List of Tables, Nested tables, Rich text format, images, hyperlinks, JavaScript, annotation, bookmarks, headers, footers and many more. Now you can create PDF by API, XML and XSL-FO files. It also enables you to converting HTML, XSL-FO and Excel files into PDF.

More about Aspose.Pdf for Java

- Homepage of Aspose.Pdf for Java

- Download Aspose.Pdf for Java

- Read More Technical Tips by Aspose.Pdf is a Java

Contact Information

Aspose Pty Ltd

Suite 163, 79 Longueville Road

Lane Cove, NSW, 2066


Aspose – Your File Format Experts


Phone: 888.277.6734

Fax: 866.810.9465
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic