Last Updated: June 17, 2016
·
387
· zondray

EMF Meta-file to PDF Conversion inside .NET Apps

Using Aspose.Imaging for Java, developers can convert EMF metafile to PDF format. Aspose.Imaging 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.

String[] filePaths = new String[] {
"input\FilledRectangleRotateMode_c.emf",
"input\image5.emf",
"input\LinearGradientBrushCircuitMode.emf",
"input\Pict.emf",
"input\Picture1.emf",
"input\test.emf",
"input\wrong-font-size.emf"
};

for (String filePath : filePaths)
{
      String outPath = filePath + ".pdf";

      com.aspose.imaging.fileformats.emf.EmfImage image = com.aspose.imaging.fileformats.emf.EmfImage.load(filePath);
      try
      {
           com.aspose.imaging.system.io.FileStream outputStream = 
                   new com.aspose.imaging.system.io.FileStream(outPath, com.aspose.imaging.system.io.FileMode.Create);
           try
           {
                 if(!image.getHeader().getEmfHeader().getValid())
                 {
                      throw new com.aspose.imaging.exceptions.ImageLoadException("The file" + outPath +" is not valid");
                 }

                 com.aspose.imaging.imageoptions.EmfRasterizationOptions emfRasterization = 
                         new com.aspose.imaging.imageoptions.EmfRasterizationOptions();

                 emfRasterization.setPageWidth(image.getWidth());
                 emfRasterization.setPageHeight(image.getHeight());
                 emfRasterization.setBackgroundColor(com.aspose.imaging.Color.getWhiteSmoke());

                 PdfOptions pdfOptions = new PdfOptions();
                 pdfOptions.setVectorRasterizationOptions(emfRasterization);

                 image.save(outputStream.toOutputStream(), pdfOptions);
            }
            finally
            {
                 outputStream.close();
                 outputStream.dispose();
            }
      }
      finally
      {
          image.dispose();
      }
}