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

Free Support Forum - aspose.com

Add Image to Table Cell from ImageStream to PDF

I am trying to add an image to a table cell from a stream and then I save the PDF through a memory stream to the download folder.

The only way I have been able to add an image to my PDF at all is with the following code:

Stream mystream = new MemoryStream(imageByteArray);
Aspose.Pdf.Rectangle logoRect = new Aspose.Pdf.Rectangle(350, 350, 75, 75);
psiPage.AddImage(mystream, logoRect);

The problem is I need to add the image to a table because the information above the table is not of a fixed size so the table could be moved up or down depending on the length of the information above.

When I try to do the code below I get a blank page and it doesn’t trigger the download of the PDF.

Image img = new Image();
img.ImageStream = mystream;
myRow.Cells.Add().Paragraphs.Add(img);

The website is hosting in Azure so I don’t have the option to save anything to the server hard drive and I only have a license to Aspose.PDF with a .dll before Mar 2, 2016. :frowning:

Can someone PLEASE help!!

@jmdoyle

You can please use following code snippet to add image inside table cell:

// Instantiate Document object
Document doc = new Document();
// Create an image instance
Aspose.Pdf.Image img = new Aspose.Pdf.Image();
// Path for source file 
img.File = dataDir + "aspose-logo.jpg"; // OR use stream
// Set width for image instance
img.FixWidth = 60;
// Set height for image instance
img.FixHeight = 60;
// Create table instance
Aspose.Pdf.Table table = new Aspose.Pdf.Table();
// Set width for table cells
table.ColumnWidths = "60 60";
table.DefaultCellBorder = new BorderInfo(Aspose.Pdf.BorderSide.All, 1F);
// Create row object and add it to table instance
Aspose.Pdf.Row row = table.Rows.Add();
// Create cell object and add it to row instance
Aspose.Pdf.Cell cell = row.Cells.Add();
// Add textfragment to paragraphs collection of cell object
cell.Paragraphs.Add(new TextFragment("First cell"));
// Add another cell to row object
cell = row.Cells.Add();
// Add image to paragraphs collection of recently added cell instance
cell.Paragraphs.Add(img);
// Create page object and add it to pages collection of document instance
Aspose.Pdf.Page page = doc.Pages.Add();
// Add table to paragraphs collection of page object
page.Paragraphs.Add(table);
dataDir = dataDir + "ImageCell_18.11.pdf";
// Save PDF file
doc.Save(dataDir);

Please also note that you are using quite older version of the API and we always recommend to use latest version. Also, the support is provided on the usage basis of the latest version. We request you to please upgrade your API and in case you face any issue with latest version, please let us know.