Adding svg image in table cell

Hi,


I am using licensed Aspose 11.0.0 java.

I am trying to insert an svg image in table using following code.

private static Table getImage() {
Image img = new Image();
img.setFile( svgFilePath );
img.setFileType( ImageFileType.Svg );
BorderInfo info = new BorderInfo( BorderSide.All, Color.getAqua() );
Table table = new Table();
table.setDefaultCellBorder( info );
Row row = table.getRows().add();
row.getCells().add( “Just some text” );
Cell cell = new Cell();
cell.setBorder( info );
cell.getParagraphs().add( img );
row.getCells().add( cell );
table.setColumnWidths( “200 50” );
return table;
}

But no matter what I do the svg image is going out of the table cell.
Is there something wrong with the code that I am doing?


Hi Saurabh,

Thanks for using our API’s.

The reason SVG image appears outside of table cell is because it has large margin around all corners. However when I have tested the scenario using one of my sample SVG images, the image appears properly in table cell.

[Java]

com.aspose.pdf.Document doc = new com.aspose.pdf.Document();

// Add a page to the document
doc.getPages().add();

// Define border information
BorderInfo info = new BorderInfo(BorderSide.All, com.aspose.pdf.Color.getAqua());

// Create a table and set default cell border
Table table = new Table();
table.setDefaultCellBorder(info);

// Add a row to the table
com.aspose.pdf.Row row = table.getRows().add();

// Add a cell with text
row.getCells().add("Just some text");

// Create a cell to hold an image
Cell cell = new Cell();
cell.setBorder(info);

// Create and configure the image
com.aspose.pdf.Image img = new com.aspose.pdf.Image();
img.setFile("C:\\pdftest\\svg\\test.svg");
img.setFileType(com.aspose.pdf.ImageFileType.Svg);

// Add the image to the cell
cell.getParagraphs().add(img);

// Add the cell to the row
row.getCells().add(cell);

// Set the column widths for the table
table.setColumnWidths("200 200");

// Add the table to the page
doc.getPages().get_Item(1).getParagraphs().add(table);

// Save the document
doc.save("c:/pdftest/TableBorderIssue.pdf");

Hi,


Thanks for your previous reply.
I don’t know much about SVG so it may be the case.

But shouldn’t the cell grow bigger according to the image ?

According to our system SVG’s are generated with very high resolution say 1920 x 1080.
Now if I insert this image it will never fit in the PDF.

As the SVG’s string is generated by the application I cannot change the SVG string.
Please can you state a way using which I can scale down the image maintaining its ratio ?

I tried setting table column width and image.fixedWidth/Height but nothing seems to work.


Hi Saurabh,


Thanks for sharing the details.

In case of Raster images, we set Table cell dimensions equal to loaded/input image dimensions, so that its properly rendered inside it. Now concerning to your scenario where Image is not properly being accommodated inside table cell, I am working on figuring out some solution and will get back to you soon.

Hi Saurabh,


Thanks for sharing the details.

Currently Aspose.Pdf for Java does not support the feature to auto-re-size/adjust dimensions of large SVG files when placing them inside PDF document. For the sake of correction, I have logged it as PDFNEWJAVA-35601 in our issue tracking system. We will further look into the details of this problem and will keep you posted on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.

Hi,

Is this issue fixed now? If it is which version of ASPOSE PDF API should I use?

Thanks,
Balakumar

@BalakumarSeethapathy,
The linked ticket ID PDFJAVA-35601 is not resolved yet and dependent on an internal enhancement. It is a complex task and can take more time than usual. We will notify you once it is fixed.

Best Regards,
Imran Rafique

Thanks Imran, What is usal turn around in fixing such bugs? Above bug has not fixed for more than one and half year. we are exploring feasibility to use Aspose in our application, and this is key feature we are looking for.

@mitulraul,
The linked ticket ID PDFJAVA-35601 is dependent on an internal feature. We have recorded your concern of the time span. We would like to add here that we do our best to incorporate the bug fixes reported by the clients as soon as possible, but in fewer cases it becomes difficult for us due to the complexity of the scenario. We will let you know once an update is available.

@SaurabhMayekar, @BalakumarSeethapathy, @mitulraul

Thanks for your patience.

We are pleased to inform you that earlier logged issue PDFJAVA-35601 has been resolved in Aspose.PDF for Java 18.1. In order to set fixed height/width of the image, please use following code snippet:

img.setFixWidth(250);  // or any other value
img.setFixHeight(250); // or any other value

Furthermore, please note that in some cases the above code is not necessary as the cells normally grow bigger if the image fits the page. And only if it is not, the image should be scaled. The snippet below demonstrates how (with using Aspose.Imaging library) can be determined, whether there is a need for scaling or not.

com.aspose.pdf.Image img = new com.aspose.pdf.Image();
img.setFile("C:\\pdftest\\geomap.svg");
         
com.aspose.imaging.fileformats.svg.SvgImage svg = (com.aspose.imaging.fileformats.svg.SvgImage) com.aspose.imaging.Image.load(img.setFile("C:\\pdftest\\geomap.svg");
         
if (svg.getWidth() > (page.getPageInfo().getWidth() / 2)) {
       double scaleFactor =  svg.getWidth() / (page.getPageInfo().getWidth() / 2 );          
       img.setFixHeight(svg.getWidth()  / scaleFactor);
       img.setFixWidth(svg.getHeight()  / scaleFactor);           
}

svg.close();

Please try using latest version of the API and in case you still face any issue, please feel free to let us know.