EMF Image when inserted into Word Document has black background

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));.

@oraspose
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-25739

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Is there any temporary workaround for this? This issue is a blocker for our feature release.

@oraspose Unfortunately, I cannot suggest you a workaround for transparent background. You can only use white background.
The issue is currently in the queue for analysis, once analysis is done we will be able to provide you more information.

Thank you the information. Have a good day

1 Like

Just an FYI - there is another issue opened against Aspose.Cells for Java product with EMF image having black background:
Issue ID(s): CELLSJAVA-45536
https://forum.aspose.com/t/change-in-shapes-background-when-inserting-excel-saved-emf-images-into-powerpoint-and-printing-to-pdf/268950/

It could be the root cause of this issue we are experiencing.
Thank you.

@oraspose Thank you for additional information. We will keep you updated and let you know once the issue is resolved or we have more information for you.

The issues you have found earlier (filed as WORDSNET-25739) have been fixed in this Aspose.Words for Java 23.9 update.

We have verified this issue with this version Java 23.9 and it got fixed.

1 Like