Hi Team,
I have a scenario where I need to generate an EMF Image out of the passed HTML file, so that it is available for the users to be inserted into any of the MS Office document viewer such as Word, Excel and PowerPoint.
I am using ASPOSE Words library v21.7.0 to load the HTML file into the ASPOSE Word document and then perform some additional property changes on the document object and finally save the HTML as EMF image.
Sample Code:
private void convertToImage() {
try {
byte[] htmlBytes = Files.readAllBytes("Test.html");
HtmlLoadOptions options = new HtmlLoadOptions();
Document doc = new Document(htmlBytes, options);
DocumentBuilder builder = new DocumentBuilder(doc);
PageSetup pageSetup = builder.getPageSetup();
//update page properties like size and margin
updatePageSettings(pageSetup);
Section section = doc.getFirstSection();
Body body = section.getBody();
// Update Table Layout properties such as Margin, Padding and Size
TableCollection tables = body.getTables();
if (tables.getCount() == 1) {
Table table = tables.get(0);
// Reset left indent. since table might be shifted left.
table.setLeftIndent(0);
// Need to ensure table has fixed column widths
table.autoFit(AutoFitBehavior.FIXED_COLUMN_WIDTHS);
//update table ROW and CELL Layout
updateTableRowSettings(table);
}
// Save as EMF format
ImageSaveOptions emfOptions = new ImageSaveOptions(SaveFormat.EMF);
emfOptions.setPageSet(new PageSet(0));
// Set document background color as transparent
emfOptions.setPaperColor(new Color(255, 255, 255, 0));
doc.save("Test.emf", emfOptions);
} catch(Exception ex) {
System.out.println(ex.getMessage());
}
}
private void updatePageSettings(PageSetup pageSetup) {
double margin = 0;
pageSetup.setLeftMargin(margin);
pageSetup.setRightMargin(margin);
pageSetup.setTopMargin(margin);
pageSetup.setBottomMargin(margin);
pageSetup.setHeaderDistance(0);
pageSetup.setFooterDistance(0);
pageSetup.setPaperSize(PaperSize.CUSTOM);
double widthpt = 448.5;
pageSetup.setPageWidth(widthpt);
double heightpt = 243;
pageSetup.setPageHeight(heightpt);
}
private void updateTableProperties(Table table) throws Exception{
double rowHeightpt = 234.75;
double cellWidthpt = 440.25;
// Set Table Row height and Cell width
for (Row row : table.getRows()) {
RowFormat rowFmt = row.getRowFormat();
rowFmt.setAllowBreakAcrossPages(false);
rowFmt.setHeightRule(HeightRule.EXACTLY);
rowFmt.setHeight(rowHeightpt);
for(Cell cell : row.getCells()) {
CellFormat cellFmt = cell.getCellFormat();
cellFmt.setWidth(cellWidthpt);
cellFmt.setTopPadding(0);
cellFmt.setBottomPadding(0);
}
}
}
When I insert the generated EMF Image from above code into Word or Excel or PPT, the EMF Image appears with black background even though the HTML passed carries the style “background-color: transparent”. If notice in the above the code, I am setting the document background color
to transparent since I am relying on Word Document to embed the HTML and then create EMF Image out of it, so that when Word/Excel/PPT has some background color the EMF Image must not appear as white background.
Attachments: Sample Test Case.zip (92.6 KB)
In the attachments above, you will find an example of incorrect EMF Image with white background for Excel sheet. I am expecting that EMF Image carry transparent background instead of white background.
What is that I am doing incorrectly? The issue with black background appears to coming from this line
emfOptions.setPaperColor(new Color(255, 255, 255, 0));.