Aspose.Pdf

Use Image from Memory

Aspose.Pdf provides pliability to developers to also use images from memory for embedding in the PDF documents. ImageInfo object (that keeps information about the image) is encapsulated in Image class and serves the purpose to use any Aspose.Pdf supported image from memory.

 

Please follow the steps below to use an image from memory:

 

 

Code Snippet

 

[C#]

 

//set the license for Aspose.Pdf

Aspose.Pdf.License license = new Aspose.Pdf.License();

license.SetLicense("Aspose.Custom.lic");

 

  //Create a memory stream object

System.IO.MemoryStream mstream = new System.IO.MemoryStream();

 

//Instantiate a Pdf object

Pdf pdf1 = new Pdf();

 

//Create a new section in the Pdf document

Section sec1 = new Section(pdf1);

 

//Add the section in the sections collection of the Pdf document

pdf1.Sections.Add(sec1);

 

//Create an image object

Aspose.Pdf.Image image1 = new Aspose.Pdf.Image(sec1);

 

//Add the image into paragraphs collection of the section

sec1.Paragraphs.Add(image1);

 

image1.ImageInfo.ImageFileType = ImageFileType.Bmp;

 

//Set the ImageStream to a MemoryStream object

image1.ImageInfo.ImageStream = mstream ;

 

//Set desired the image scale

image1.ImageScale = 0.5F;

 

//Save the Pdf

pdf1.Save(...);

 

[VB.NET]

 

'Create a memory stream object

Dim mstream As System.IO.MemoryStream = New System.IO.MemoryStream()

 

'Instantiate a Pdf object and set the license for Aspose.Pdf

Dim pdf1 As Pdf = New Pdf("e:\projects\CSharp\customer\Aspose.Pdf.lic")

 

'Create a new section in the Pdf document

Dim sec1 As Section = New Section(pdf1)

 

'Add the section in the sections collection of the Pdf document

pdf1.Sections.Add(sec1)

 

'Create an image object

Dim image1 As Aspose.Pdf.Image = New Aspose.Pdf.Image(sec1)

 

'Add the image into paragraphs collection of the section

sec1.Paragraphs.Add(image1)

 

image1.ImageInfo.ImageFileType = ImageFileType.Bmp

 

'Set the ImageSteam to a MemoryStream object

image1.ImageInfo.ImageStream = mstream

 

'Set desired the image scale

image1.ImageScale = 0.5F

 

'Save the Pdf

pdf1.Save(...)

 

[JAVA]

 

//Instantiate a Pdf object by calling its empty constructor

Pdf pdf1 = new Pdf();

 

//Create a new section in the Pdf document

Section sec1 = new Section(pdf1);

 

//Add the section in the sections collection of the Pdf document

pdf1.getSections().add(sec1);

 

//Create an image object in the section

com.aspose.pdf.elements.Image img1 = new com.aspose.pdf.elements.Image(sec1);

 

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

sec1.getParagraphs().add(img1);

 

//Set the path of image file

img1.getImageInfo().setMemoryData(...);

 

//Save the Pdf

FileOutputStream out = new FileOutputStream(new File(...));

pdf1.save(out);