Converting WMF and EMF to Other Image Formats

Converting EMF or WMF to PDF

Using Aspose.Imaging for Java, developers can convert WMF metafile to PDF format. This topic explains the approach to load existing metafiles and convert it to .Aspose.Imaging for Java provides the Image class to load WMF files and same can be used to save the image to PDF format. The following code snippet shows you how to convert WMF to PDF.

// The path to the documents directory.
String dataDir = "DataDir/";
// Load an existing WMF image
try (Image image = Image.load(dataDir + "input.wmf"))
{
// Create an instance of EmfRasterizationOptions class and set different properties
EmfRasterizationOptions emfRasterizationOptions = new EmfRasterizationOptions();
emfRasterizationOptions.setBackgroundColor(Color.getWhiteSmoke());
emfRasterizationOptions.setPageWidth(image.getWidth());
emfRasterizationOptions.setPageHeight(image.getHeight());
// Create an instance of PdfOptions class and provide rasterization option
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setVectorRasterizationOptions(emfRasterizationOptions);
// Call the save method, provide output path and PdfOptions to convert the WMF file to PDF and save the output
image.save(dataDir + "ConvertWMFToPDF_out.pdf", pdfOptions);
}

Converting WMF To Webp

Using Aspose.Imaging for Java, developers can convert WMF metafile to Webp format. This topic explains the approach to load existing metafiles and convert it to Webp format. Aspose.Imaging for Java provides the Image class to load WMF files and same can be used to save the image to Webp format. The following code snippet shows you how to convert WMF to Webp.

// The path to the documents directory.
String dataDir = "DataDir/";
// Load an existing WMF image
try (Image image = Image.load(dataDir + "input.wmf"))
{
// Calculate new Webp image height
double k = (image.getWidth() * 1.00) / image.getHeight();
// Create an instance of EmfRasterizationOptions class and set different properties
EmfRasterizationOptions emfRasterization = new EmfRasterizationOptions();
emfRasterization.setBackgroundColor(Color.getWhiteSmoke());
emfRasterization.setPageWidth(400);
emfRasterization.setPageHeight((int)Math.round(400 / k));
emfRasterization.setBorderX(5);
emfRasterization.setBorderY(10);
// Create an instance of WebPOptions class and provide rasterization option
ImageOptionsBase imageOptions = new WebPOptions();
imageOptions.setVectorRasterizationOptions(emfRasterization);
// Call the save method, provide output path and WebPOptions to convert the WMF file to Webp and save the output
image.save(dataDir + "ConvertWMFToWebp_out.webp", imageOptions);
}

Converting WMF to PNG

Using Aspose.Imaging for Java, developers can convert WMF metafile to raster format. This topic explains the approach to load existing metafiles and convert it to raster format. Aspose.Imaging for Java provides the Image class to load WMF files and same can be used to save the image to PNG format. The following code snippet shows you how convert WMF To Raster Format.

// The path to the documents directory.
String dataDir = "DataDir/";
String inputFileName = dataDir + "thistlegirl_wmfsample.wmf";
String outputFileNamePng = dataDir + "thistlegirl_wmfsample.png";
try (Image image = Image.load(inputFileName))
{
WmfRasterizationOptions rasterizationOptions = new WmfRasterizationOptions();
rasterizationOptions.setBackgroundColor(Color.getWhiteSmoke());
rasterizationOptions.setPageWidth(image.getWidth());
rasterizationOptions.setPageHeight(image.getHeight());
PngOptions imageOptions = new PngOptions();
imageOptions.setVectorRasterizationOptions(rasterizationOptions);
image.save(outputFileNamePng, imageOptions);
}

Converting WMF MetaFile To SVG

Using Aspose.Imaging for Java, developers can convert WMF to SVG format. This topic explains in detail how to convert WMF to SVG format. Aspose.Imaging for Java provides the SvgOptions class to create SVG format image. The following code snippet shows you how to convert WMF To SVG format.

// The path to the documents directory.
String dataDir = "svg/";
String inputFileName = dataDir + "thistlegirl_wmfsample.wmf";
String outputFileNameSvg = dataDir + "thistlegirl_wmfsample.svg";
try (Image image = Image.load(inputFileName))
{
WmfRasterizationOptions rasterizationOptions = new WmfRasterizationOptions();
rasterizationOptions.setBackgroundColor(Color.getWhiteSmoke());
rasterizationOptions.setPageWidth(image.getWidth());
rasterizationOptions.setPageHeight(image.getHeight());
SvgOptions svgOptions = new SvgOptions();
svgOptions.setVectorRasterizationOptions(rasterizationOptions);
image.save(outputFileNameSvg, svgOptions);
}

Converting EMF to WMF Format

Using Aspose.Imaging for Java, developers can convert EMF to WMF format. This topic explains in detail how to convert WMF to WMF format. The following code snippet shows you how to convert WMF To SVG format.

// List of existing EMF images.
String path = "DataDir/";
String[] files = new String[] { "TestEmfRotatedText.emf", "TestEmfPlusFigures.emf", "TestEmfBezier.emf" };
// Loop for each file name.
for (String file : files)
{
// Input file name & path.
String filePath = path + file;
// Load the EMF image as image and convert it to MetaImage object.
try (com.aspose.imaging.fileformats.emf.MetaImage image = (com.aspose.imaging.fileformats.emf.MetaImage)Image.load(filePath))
{
// Convert the EMF image to WMF image by creating and passing WMF image options class object.
image.save(filePath + "_out.wmf", new com.aspose.imaging.imageoptions.WmfOptions());
}
}

Cropping WMF file while converting to PNG

Aspose.Imaging for Java provides the Image class to load WMF files and same can be used to crop and save the image to PNG format.

Below provided sample code demonstrate how to crop the WMF file while converting it to PNG.

String dataDir = "dataDir/";
// Load an existing WMF image
Image image = Image.load(dataDir + "File.wmf");
try
{
image.crop(new Rectangle(300, 200, 200, 200));
// Create an instance of EmfRasterizationOptions class and set
// different properties
EmfRasterizationOptions emf = new EmfRasterizationOptions();
emf.setPageWidth(1000);
emf.setPageHeight(1000);
emf.setBackgroundColor(Color.getWhiteSmoke());
// Create an instance of PngOptions class and provide rasterization
// option
ImageOptionsBase options = new PngOptions();
options.setVectorRasterizationOptions(emf);
// Call the save method, provide output path and PngOptions to
// convert the cropped WMF file to PNG and save the output
image.save(dataDir + "ConvertWMFToPNG_out.png", options);
}
finally
{
image.dispose();
}

Support For saving EMF and EMF+ format to File

Using Aspose.Imaging for Java, developers can save EMF and EMF plus format to file. This topic explains in detail how to save emf graphics files. The code snippet has been provided below.

String dataDir = "dataDir/";
String path = dataDir + "TestEmfPlusFigures.emf";
EmfImage image = (EmfImage)Image.load(path);
try
{
image.save(path + ".emf", new EmfOptions());
}
finally
{
image.close();
}
String dataDir = "dataDir/";
String path = dataDir + "TestEmfBezier.emf";
EmfImage image = (EmfImage)Image.load(path);
try
{
image.save(path + ".emf", new EmfOptions());
}
finally
{
image.close();
}
String dataDir = "dataDir/";
com.aspose.imaging.fileformats.emf.graphics.EmfRecorderGraphics2D graphics = new com.aspose.imaging.fileformats.emf.graphics.EmfRecorderGraphics2D(
new com.aspose.imaging.Rectangle(0, 0, 5000, 5000),
new com.aspose.imaging.Size(5000, 5000),
new com.aspose.imaging.Size(1000, 1000));
Font font = new Font("Arial", 10, FontStyle.Bold | FontStyle.Underline);
graphics.drawString(font.getName()+ " " + font.getSize() + " " + FontStyle.toString(FontStyle.class, font.getStyle()), font, Color.getBrown(), 10, 10);
graphics.drawString("some text", font, Color.getBrown(), 10, 30);
font = new Font("Arial", 24, FontStyle.Italic | FontStyle.Strikeout);
graphics.drawString(font.getName() + " " + font.getSize() + " " + FontStyle.toString(FontStyle.class, font.getStyle()), font, Color.getBrown(), 20, 50);
graphics.drawString("some text", font, Color.getBrown(), 20, 80);
EmfImage image = graphics.endRecording();
try
{
String path = dataDir + "Fonts.emf";
image.save(path, new EmfOptions());
}
finally
{
image.close();
}