Manipulating Metafiles

Converting EMF To PDF

Using Aspose.Imaging for Python via .NET, developers can convert EMF metafile to PDF format. This topic explains the approach to load existing metafiles and convert it to PDF. Aspose.Imaging for Python via .NET provides the EmfImage class to load EMF files and same can be used to save the image to PDF format. Below provided sample code demonstrate how to convert EMF to PDF.

import aspose.pycore as aspycore
from aspose.imaging import Image, Color
from aspose.imaging.fileformats.emf import EmfImage
from aspose.imaging.imageoptions import EmfRasterizationOptions, PdfOptions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
file_paths = ["template.emf"]
for file_path in file_paths:
out_path = os.path.join(data_dir, "out_" + file_path + ".pdf")
with aspycore.as_of(Image.load(os.path.join(data_dir, file_path)), EmfImage) as image:
if not image.header.emf_header.valid:
raise Exception("The file {0} is not valid".format(out_path))
emf_rasterization = EmfRasterizationOptions()
emf_rasterization.page_width = float(image.width)
emf_rasterization.page_height = float(image.height)
emf_rasterization.background_color = Color.white_smoke
pdf_options = PdfOptions()
pdf_options.vector_rasterization_options = emf_rasterization
image.save(out_path, pdf_options)
if delete_output:
os.remove(out_path)

Cropping EMF Image

Image cropping usually refers to the removal of the outer parts of an image to help improve the framing. Cropping may also be used to cut out some portion of an image to increase the focus on a particular area. Aspose.Imaging for Python via .NET API supports two different approaches for cropping image: by shifts and by rectangle.

Using Shifts

The EmfImage class provides an overloaded version of the Crop method that accepts 4 integer values denoting Left, Right, Top & Bottom. Based on these four values, the crop method moves the image boundaries toward the center of the image while discarding the outer portion.

The code snippet below demonstrates how to crop an EMF image by shifts.

import aspose.pycore as aspycore
from aspose.imaging import Image, Color
from aspose.imaging.fileformats.emf import EmfImage
from aspose.imaging.imageoptions import EmfRasterizationOptions, PdfOptions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
# Create an instance of Rasterization options
emf_rasterization_options = EmfRasterizationOptions()
emf_rasterization_options.background_color = Color.white_smoke
# Create an instance of PNG options
pdf_options = PdfOptions()
pdf_options.vector_rasterization_options = emf_rasterization_options
# Load an existing image into an instance of EMF class
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.emf")), EmfImage) as image:
# Based on the shift values, apply the cropping on image and Crop method will shift the image bounds toward the center of image
image.crop(0, 0, 30, 30)
# Set height and width and Save the results to disk
pdf_options.vector_rasterization_options.page_width = float(image.width)
pdf_options.vector_rasterization_options.page_height = float(image.height)
image.save(os.path.join(data_dir, "result.pdf"), pdf_options)
if delete_output:
os.remove(os.path.join(data_dir, "result.pdf"))

Using Rectangle

The EmfImage class provides another overloaded version of the crop method that accepts an instance of the Rectangle class. You can cut out any portion of an image by providing the desired boundaries to the Rectangle object. The code snippet below demonstrates how.

import aspose.pycore as aspycore
from aspose.imaging import Image, Color
from aspose.imaging.fileformats.emf import EmfImage
from aspose.imaging.imageoptions import EmfRasterizationOptions, PdfOptions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
# Create an instance of Rasterization options
emf_rasterization_options = EmfRasterizationOptions()
emf_rasterization_options.background_color = Color.white_smoke
# Create an instance of PNG options
pdf_options = PdfOptions()
pdf_options.vector_rasterization_options = emf_rasterization_options
# Load an existing image into an instance of EMF class
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.emf")), EmfImage) as image:
# Based on the shift values, apply the cropping on image and Crop method will shift the image bounds toward the center of image
image.crop(0, 0, 30, 30)
# Set height and width and Save the results to disk
pdf_options.vector_rasterization_options.page_width = float(image.width)
pdf_options.vector_rasterization_options.page_height = float(image.height)
image.save(os.path.join(data_dir, "result.pdf"), pdf_options)
if delete_output:
os.remove(os.path.join(data_dir, "result.pdf"))

Export MetaFile To Raster Formats

Using Aspose.Imaging for Python via .NET, developers can convert metafile (Emf/Emf+) to raster formats. This article shows how to export/convert metafile file to raster image formats with Aspose.Imaging. Aspose.Imaging for Python via .NET provides the EmfImage class to load EMF files and same can be used to convert the Emf to raster formats. Below provided sample code demonstrate how to convert Emf/Emf+ to raster images.

import aspose.pycore as aspycore
from aspose.imaging import Image, Color
from aspose.imaging.fileformats.emf import EmfImage
from aspose.imaging.imageoptions import EmfRasterizationOptions, BmpOptions, GifOptions, JpegOptions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
# Create EmfRasterizationOption class instance and set properties
emf_rasterization_options = EmfRasterizationOptions()
emf_rasterization_options.background_color = Color.papaya_whip
emf_rasterization_options.page_width = 300.0
emf_rasterization_options.page_height = 300.0
# Load an existing EMF file as image and convert it to EmfImage class object
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.emf")), EmfImage) as image:
if not image.header.emf_header.valid:
raise ImageLoadException("The file {0} is not valid".format(os.path.join(data_dir, "Picture1.emf")))
# Convert EMF to BMP, GIF, JPEG, J2K, PNG, PSD, TIFF and WebP
bmp_opt = BmpOptions()
bmp_opt.vector_rasterization_options = emf_rasterization_options
image.save(os.path.join(data_dir, "result.bmp"), bmp_opt)
gif_opt = GifOptions()
gif_opt.vector_rasterization_options = emf_rasterization_options
image.save(os.path.join(data_dir, "result.gif"), gif_opt)
jpg_opt = JpegOptions()
jpg_opt.vector_rasterization_options = emf_rasterization_options
image.save(os.path.join(data_dir, "result.jpg"), jpg_opt)
if delete_output:
os.remove(os.path.join(data_dir, "result.bmp"))
os.remove(os.path.join(data_dir, "result.gif"))
os.remove(os.path.join(data_dir, "result.jpg"))

Specifying Font Folder While Converting

Using Aspose.Imaging for Python via .NET, developers can specify the font folder path used while converting WMF file. This topic explains the approach to specify the font folder that should be used. The following code snippet demonstrates the usage of Aspose.Imaging for Python via .NET API to change the font while converting EMF to raster image.

import aspose.pycore as aspycore
from aspose.imaging import Image, Color, FontSettings
from aspose.imaging.fileformats.emf import EmfImage
from aspose.imaging.imageoptions import EmfRasterizationOptions, PngOptions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
emf_rasterization_options = EmfRasterizationOptions()
emf_rasterization_options.background_color = Color.white_smoke
# Create an instance of PNG options
png_options = PngOptions()
png_options.vector_rasterization_options = emf_rasterization_options
# Load an existing EMF image
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.emf")), EmfImage) as image:
image.cache_data()
# Set height and width, Reset font settings
png_options.vector_rasterization_options.page_width = 300.0
png_options.vector_rasterization_options.page_height = 350.0
FontSettings.reset()
image.save(os.path.join(data_dir, "result.png"), png_options)
# Initialize font list
fonts = list(FontSettings.get_default_fonts_folders())
# Add new font path to font list and Assign list of font folders to font settings and Save the EMF file to PNG image with new font
fonts.append(os.path.join(data_dir, "fonts"))
FontSettings.set_fonts_folders(fonts, True)
image.save(os.path.join(data_dir, "result2.png"), png_options)
if delete_output:
os.remove(os.path.join(data_dir, "result.png"))
os.remove(os.path.join(data_dir, "result2.png"))

Support For Replacing Missing Fonts

Using Aspose.Imaging for Python via .NET, developers can replace missing fonts when saving ODG, SVG and MetaFile images. This topic explains the approach to replace the fonts that should be used. The following code snippet demonstrates the usage of Aspose.Imaging for Python via .NET API to replace the font while saving ODG images.

import aspose.pycore as aspycore
from aspose.imaging import Image, Color, FontSettings
from aspose.imaging.fileformats.emf import EmfImage
from aspose.imaging.imageoptions import EmfRasterizationOptions, PngOptions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
emf_rasterization_options = EmfRasterizationOptions()
emf_rasterization_options.background_color = Color.white_smoke
# Create an instance of PNG options
png_options = PngOptions()
png_options.vector_rasterization_options = emf_rasterization_options
# Load an existing EMF image
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.emf")), EmfImage) as image:
image.cache_data()
# Set height and width, Reset font settings
png_options.vector_rasterization_options.page_width = 300.0
png_options.vector_rasterization_options.page_height = 350.0
FontSettings.reset()
image.save(os.path.join(data_dir, "result.png"), png_options)
# Initialize font list
fonts = list(FontSettings.get_default_fonts_folders())
# Add new font path to font list and Assign list of font folders to font settings and Save the EMF file to PNG image with new font
fonts.append(os.path.join(data_dir, "fonts"))
FontSettings.set_fonts_folders(fonts, True)
image.save(os.path.join(data_dir, "result2.png"), png_options)
if delete_output:
os.remove(os.path.join(data_dir, "result.png"))
os.remove(os.path.join(data_dir, "result2.png"))