Important alert: (current site time 8/21/2014 11:43:10 AM EDT)
 

article

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

Email
Submitted on: 2/29/2012 5:28:23 PM
By: aspose_seo 
Level: Intermediate
User Rating: Unrated
Compatibility: Java (JDK 1.4), Java (JDK 1.5)
Views: 5869
 
     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.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				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.
[Java]
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) {
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
 BinaryFileStream bstream = new BinaryFileStream(fileArray);
 //Set the image object to use BinaySgtream object
 img1.getImageInfo().setImageStream(bstream);
 //Save the PDF file
 pdf.save("d:/pdftest/Image2PDF.pdf");
}catch(java.io.IOException ioe){
				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;
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());
}
is.close();
return bytes;
	}
 
More about Aspose.Pdf for Java
- Homepage of Aspose.Pdf is a Java: http://www.aspose.com/categories/java-components/aspose.pdf-for-java/default.aspx 
- Read More Technical Tips by Aspose.Pdf is a Java at: http://docs.aspose.com/display/pdfjava/Technical+Articles 
- Download Aspose.Pdf is a Java: http://www.aspose.com/community/files/72/java-components/aspose.pdf-for-java/default.aspx 
Contact Information
Aspose Pty Ltd, Suite 163, 
79 Longueville Road
Lane Cove, NSW, 2066 
Australia 
http://www.aspose.com/
sales@aspose.com
Phone: 888.277.6734
Fax: 866.810.9465


Other 31 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.