Free Support Forum - aspose.com

Align image in cell of a table

Hi,


i want to align image in table cell .Please suggest how this can be done.i am trying below way but the same is not working.

Image exclaimImg = new Image();
exclaimImg.setHorizontalAlignment(HorizontalAlignment.Center);
exclaimImg.setFile(“C:\WebContent\images\exclaimation.png”);

Cell cell1=row1.getCells().add();
cell1.setVerticalAlignment(VerticalAlignment.Center);
cell1.getParagraphs().add(exclaimImg);

Hi Ramesh,


Thanks for contacting support.

I have tested the scenario and have observed that HorizontalAlignment for cell contents is not being applied. For testing purposes, I have used Aspose.Pdf for Java 9.5.2 with following code snippet. For
the sake of correction, I have logged it in our issue tracking system as PDFNEWJAVA-34547. We will
investigate this issue in details and will keep you updated on the status of a
correction. We
apologize for your inconvenience.

In case you have any other observations, please share some details so we may investigate accordingly.


[Java]

com.aspose.pdf.Document pdfDocument1 =
new
com.aspose.pdf.Document();<o:p></o:p>

pdfDocument1.getPages().add();

// create table instance

com.aspose.pdf.Table table1 = new com.aspose.pdf.Table();

// set width of table columns

table1.setColumnWidths("100 300");

table1.setBorder(new com.aspose.pdf.BorderInfo(com.aspose.pdf.BorderSide.All));

table1.setDefaultCellBorder(new com.aspose.pdf.BorderInfo(com.aspose.pdf.BorderSide.All));

// add row to table instance

com.aspose.pdf.Row row1 = table1.getRows().add();

// set row height so that we can see alignment settting

row1.setFixedRowHeight(250);

// create cell instance

com.aspose.pdf.Cell cell1 = row1.getCells().add();

// create TextFragment for cell1 instance

com.aspose.pdf.TextFragment textfragment = new com.aspose.pdf.TextFragment();

// create first segment instance

com.aspose.pdf.TextSegment seg1 = new com.aspose.pdf.TextSegment("hi ");

// set foreground color for first segment

seg1.getTextState().setForegroundColor(com.aspose.pdf.Color.getRed());

// add first segment to TextFragment instance

textfragment.getSegments().add(seg1);

//create first segment instance

com.aspose.pdf.TextSegment seg2 = new com.aspose.pdf.TextSegment("Aspose!!");

//set foreground color for first segment

seg2.getTextState().setForegroundColor(com.aspose.pdf.Color.getGreen());

//add first segment to TextFragment instance

textfragment.getSegments().add(seg2);

cell1.getParagraphs().add(textfragment);

com.aspose.pdf.Image exclaimImg = new com.aspose.pdf.Image();

//exclaimImg.setHorizontalAlignment(HorizontalAlignment.Center);

exclaimImg.setFile("C:\\pdftest\\myImage.png");

com.aspose.pdf.Cell cell2=row1.getCells().add();

// set horizontal alignment for cell contents

cell2.setAlignment(com.aspose.pdf.HorizontalAlignment.Center);

// set vertical alignment for cell contents as center aligned

cell2.setVerticalAlignment(com.aspose.pdf.VerticalAlignment.Center);

// add image to paragraphs collection of cell instnace

cell2.getParagraphs().add(exclaimImg);

// add table to paragraphs collection of first page of document

pdfDocument1.getPages().get_Item(1).getParagraphs().add(table1);

// save PDF file

pdfDocument1.save("c:/pdftest/TableWithTwo_Fragments.pdf");

The issues you have found earlier (filed as PDFNEWJAVA-34547) have been fixed in Aspose.Pdf for Java 10.4.1.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.