Hello,
We are seeing an issue while performing HTML to SVG conversion using Aspose Words Java API where the SVG containing HTML Table renders incorrectly when saved as SVG. This behavior is seen while testing with Aspose Words for Java version 21.7.0. I have attached the sample input HTML file and resulting 2 SVG output files based on different configuration set to match SVG output with HTML.
We are facing few issues during the conversion process which is listed below.
- During Save process the Table is split into 2 Sections - FirstChild is Table and LastChild is paragraph which results in Page break as seen the SVG files attached.
- The Table height in attached SVG files is incorrect when compared to actual HTML file but Table width is matching.
- When setting Table.PreferredWidth to AUTO and fromPoints(PageSetup.PageWidth) the resulting SVG output is different with respect to Column Rendering and Width.
-TestSample1.svg with AUTO, the last 2 columns of the Table is rendered incorrectly
-TestSample2.svg with fromPoints(), table right border is missing and the columns width is incorrect when compared to actual HTML file. - The Document.ViewOptions.ZoomType and Document.ViewOptions.ZoomPercent is provided but it has no effect on the resulting Table within the attached SVG outputs.
Sample Java Code for the HTML to SVG conversion:
double objHeight = 822.0;
double objWidth = 1680.0;
InputStream inputStream = new ByteArrayInputStream(htmlBytes);
FontSettings fontSettings = FontSettings.getDefaultInstance();
fontSettings.setFontsFolder(fontDirectory.getDeployedFontFolder(), true);
//set load options and font settings
HtmlLoadOptions loadOptions = new HtmlLoadOptions();
loadOptions.setLoadFormat(LoadFormat.HTML);
loadOptions.setEncoding(StandardCharsets.UTF_8);
loadOptions.setFontSettings(fontSettings);
//load the HTML document with ASPOSE Document object
Document wordDoc = new Document(inputStream, loadOptions);
DocumentBuilder builder = new DocumentBuilder(wordDoc);
//get the HTML document pageSetup to alter margin and size
PageSetup pageSetup = builder.getPageSetup();
double pageHeight = ConvertUtil.pixelToPoint(objHeight);
double pageWidth = ConvertUtil.pixelToPoint(objWidth);
pageSetup.setPageHeight(pageHeight);
pageSetup.setPageWidth(pageWidth);
pageSetup.setLeftMargin(0);
pageSetup.setRightMargin(0);
pageSetup.setTopMargin(0);
pageSetup.setBottomMargin(0);
Section section = builder.getCurrentSection();
Body body = section.getBody();
Iterator<Node> nodes = body.getChildNodes().iterator();
while(nodes.hasNext()) {
Node node = nodes.next();
int nodeType = node.getNodeType();
//NodeType.TABLE = 5, NodeType.PARAGRAPH = 8
switch(nodeType) {
case NodeType.TABLE:
Table table = Table.class.cast(node);
table.setAllowAutoFit(false);
//Uncomment to get TestSample1.svg output
//table.setPreferredWidth(PreferredWidth.AUTO);
//Uncomment to get TestSample2.svg output.
//table.setPreferredWidth(PreferredWidth.fromPoints(pageWidth));
//This method will also perform re-rendering of table layout
//based on table properties set explicitly.
wordDoc.updateTableLayout();
break;
case NodeType.PARAGRAPH:
System.out.println("I am inside Paragraph");
break;
default:
break;
}
}
//get the SVG SaveOptions to update the output within Document
SvgSaveOptions saveOptions = new SvgSaveOptions();
saveOptions.setShowPageBorder(false);
saveOptions.setOptimizeOutput(true);
wordDoc.save("TestSample.svg", saveOptions);
Also, when I used the WarningCallBack option I got the following error:
Warning Type: MINOR_FORMATTING_LOSS, Warning Source: LAYOUT,
Warning: Table column widths may need to be calculated. Rendered column widths could differ. At Table 1, Section 1.
Environment Details:
Aspose Words for Java 21.7.0
Java version 1.8
Linux 7.
File description in the TestSample.zip (30 KB) attachment contains:
TestSample.html: HTML file that is imported into the new Document per the code above has 30 rows and 17 columns.
TestSample1.svg: SVG file produced from the HTML to SVG conversion code mentioned above with PreferredWidth.AUTO settings.
TestSample2.svg: SVG file produced from the HTML to SVG conversion code mentioned above with PreferredWidth.fromPoints(PageSetup.PageWidth) settings.
TestSample.zip (29.7 KB)