Hi,
Happy new year.
We upgraded aspose imaging java from com.aspose:aspose-imaging:19.10:jdk16 to com.aspose:aspose-imaging:22.12:jdk16 recently, and found that some emf files can’t be displayed because the api EmfMetafileImage.playMetafile(Graphics2D) was removed since version 20.2
Then we tried to find the alternative of it, and the code below works in most cases.
BufferedImage image = ImageExtensions.toJava(emf);
graphics2D.drawImage(image , 0, 0, (int) emf.getWidth(), (int) emf.getHeight(), null);
However, some of emf can’t be displayed correctly, and it seems that the entire frame will be moved to right and bottom by roughly 100 pixels.
To investigate that issue, we tried to save the emf to other formats like wmf, png and svg. And they are all the same and the frame is always moved. The sample code and sample test data are below.
import java.io.File;
import com.aspose.imaging.Image;
import com.aspose.imaging.ImageOptionsBase;
import com.aspose.imaging.fileformats.emf.EmfImage;
import com.aspose.imaging.fileformats.emf.EmfRenderMode;
import com.aspose.imaging.imageoptions.EmfRasterizationOptions;
import com.aspose.imaging.imageoptions.PngOptions;
public class TestEmf {
public static void main(String[] args) throws Exception {
File emfFile = new File("src/main/resources/emf/test.emf");
File outputFile = new File("src/main/resources/emf/out-test.png");
//We try to display this emf file or save it to another image file.
// the api playMetafile in the old class EmfMetafileImage works, but it is in aspose imaging 19.x only and has been removed since aspose imaging 20.2
// EmfMetafileImage emfMetafileImage = new EmfMetafileImage(emfFile.getAbsolutePath());
// emfMetafileImage.playMetafile(Graphics2D);
//The code below is mostly same to https://reference.aspose.com/imaging/java/com.aspose.imaging.imageoptions/emfrasterizationoptions/
//It can create the Png file successfully.
//However, the frame is moved, and roughly 100 pixels in right and bottom are missing.
//It is the same if we save the emf to wmf, png or svg, the 100 pixels (roughly) in right and bottom are always missing.
EmfImage emfImage = (EmfImage) Image.load(emfFile.getAbsolutePath());
try {
ImageOptionsBase saveOptions = new PngOptions();
EmfRasterizationOptions rasterizationOptions = new EmfRasterizationOptions();
rasterizationOptions.setPageWidth(emfImage.getWidth());
rasterizationOptions.setPageHeight(emfImage.getHeight());
rasterizationOptions.setRenderMode(EmfRenderMode.Auto);
saveOptions.setVectorRasterizationOptions(rasterizationOptions);
emfImage.save(outputFile.getAbsolutePath(), saveOptions);
} finally {
emfImage.dispose();
}
}
}
Test data:
emf-test-data.zip (28.3 KB)
My questions are
- could class EmfMetafileImage and method emfMetafileImage.playMetafile(Graphics2D) be added back in the future, or could you tell us where they are in versions after 20.2?
- if the answer to 1 is No, could you tell us how to render the sample emf file successfully? Thanks.
Regards,
Tony