We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Create PDF file from an array of images- each image in the array being a byte[]

Hello,


I would like to take an array of images, say a List<byte[]> Images and then create a pdf file using this array of images.

What would be the best way to create the PDF File and then get the byte[] of that PDF file to pass back to the calling application.

This should be done in the best way possible for memory management, I would like to create and dispose of a memory stream for each page as it goes to keep memory usage down :

[Psudo Code C#]
var PDFFile = Aspose.Pdf…
foreach(byte[] imageByteArray in ArrayOfImageByteArrays)
{
using(MemoryStream imageMS = new MemoryStream(imageByteArray))
{

//Add image to PDF file as a page here

}
}

//Done, so save complete PDF File to another memory stream so we can get the byte array
using(MemoryStream fileMS = new MemoryStream())
{
pdfFile.Save(fileMS);
ByteArrayToReturn = fileMS.toArray();
}

Hi Chad,


Thanks for your inquiry. You can create a new PDF from image and save into memory steam using DOM approach of Aspose.Pdf. Please check following code snippet for the purpose. It will help you to accomplish your requirements.

string inFile =
myDir + “TestImage.bmp”;<o:p></o:p>

MemoryStream ms = new MemoryStream();

Document doc = new Document();

Page page = doc.Pages.Add();

FileStream fs = new FileStream(inFile, FileMode.Open, FileAccess.Read);

byte[] tmpBytes = new byte[fs.Length];

fs.Read(tmpBytes, 0, Convert.ToInt32(fs.Length));

MemoryStream mystream = new MemoryStream(tmpBytes);

// Set margins so image will fit, etc.

page.PageInfo.Margin.Bottom = 0;

page.PageInfo.Margin.Top = 0;

page.PageInfo.Margin.Left = 0;

page.PageInfo.Margin.Right = 0;

page.PageInfo.Height = Aspose.Pdf.PageSize.A4.Height;

page.PageInfo.Width = Aspose.Pdf.PageSize.A4.Width;

//Create an image object

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

//Add the image into paragraphs collection of the section

page.Paragraphs.Add(image1);

//Set the ImageStream to a MemoryStream object

image1.ImageStream = mystream;

doc.Save(ms);

Please feel free to contact us for any further assistance.


Best Regards,

Thank you for the answer.


My current code looks like this: ImageFileMetaData is a class i created, ImageFileSet.Value contains the byte[] image data.

byte[] Returnval = new byte[] { 0 };
Aspose.Pdf.Document newPDFDoc = new Document();
newPDFDoc.OptimizeSize = true;
using (MemoryStream finalPDF = new MemoryStream())
{
foreach (KeyValuePair<ImageFileMetaData, byte[]> ImageFileSet in ImageFiles)
{
using (MemoryStream pageMS = new MemoryStream(ImageFileSet.Value))
{
Page newPage = newPDFDoc.Pages.Add();
newPage.PageInfo.Margin.Bottom = 0;
newPage.PageInfo.Margin.Top = 0;
newPage.PageInfo.Margin.Left = 0;
newPage.PageInfo.Margin.Right = 0;
newPage.PageInfo.Height = Aspose.Pdf.PageSize.A4.Height;
newPage.PageInfo.Width = Aspose.Pdf.PageSize.A4.Width;
newPage.PageInfo.IsLandscape = (ImageFileSet.Key.ImageProperties.Width > ImageFileSet.Key.ImageProperties.Height) ? true : false;

//Add the image to the paragraph
Aspose.Pdf.Image newImage = new Aspose.Pdf.Image();
newImage.ImageStream = pageMS;
newImage.ImageScale = 1.0F;
newPage.Paragraphs.Add(newImage);
newPDFDoc.Save(finalPDF);
}
}

Returnval = finalPDF.ToArray();
}

My problem is that this method does not keep the image format of the images I am looping through. Prior to iterating this array of images, I optimize each image according to our requirements. After creating a PDF using this method above, the resulting PDF is quite large. Is there a way to tell the library what kind of image and what format it is? Looking for a way to keep the image format and compression intact and just place it in the PDF.

Hi Chad,


Thanks for your feedback. API doesn’t change image type by default, it insert it at it is. However if you want to optimize your output file then please consider following documentation link for the purpose. You may change image type to PNG (recommended format to avoid transparency issue), resolution and image quality. Hopefully it will help you accomplish the task.


Please feel free to contact us for any further assistance.

Best Regards,