Create EMF metafile image using Aspose.Imaging for Java 3.9.0

Share on FacebookTweet about this on TwitterShare on LinkedIn

Aspose.Imaging for Java logo We are pleased to announce the release of Aspose.Imaging for Java 3.9.0. The major development in this release is support for creating EMF metafile image. This release supports PSD file created/updated using SAI and GIMP2 tools. Support to convert EMF metafile to SVG format has also been incorporated in this release.

Create EMF Metafile Image

Aspose.Imaging for Java now supports creating EMF metafile image. Aspose.Imaging for Java provides the EmfRecorderGraphics2D class to create EMF metafile. Below provided sample code demonstrates how to create EMF metafile.



public void penAndMixedArcTests()
{
// EmfRecorderGraphics2D class provides you the frame or canvas to draw shapes on it.
// Create an instance of EmfRecorderGraphics2D class. The constructor takes in 3 parameters:
// 1. Instance of Imaging Rectangle class
// 2. Instance of Imaging Size class
// 3. Instance of Imaging Size class
EmfRecorderGraphics2D graphics = new EmfRecorderGraphics2D(new Rectangle(0, 0, 1000, 1000), new Size(1000, 1000)
            , new Size(100, 100));
        {
            
            // Create an instance of Imaging Pen class and mention its color.
            Pen pen = new Pen(Color.getBisque().Clone());

            // Draw a line by calling DrawLine method and passing x,y coordinates of 1st point and same for 2nd point along with color info as Pen.
            graphics.drawLine(pen, 1, 1, 50, 50);

            // Reset the Pen color.
            pen = new Pen(Color.getBlueViolet().Clone(), 3);

            // specify the end style of the line.
            pen.setEndCap(LineCap.Round);

            // Draw a line by calling DrawLine method and passing x,y coordinates of 1st point and same for 2nd point along with color infor as Pen and end style of line.
            graphics.drawLine(pen, 15, 5, 50, 60);

            // specify the end style of the line.
            pen.setEndCap(LineCap.Square);

            // Draw a line by calling DrawLine method.
            graphics.drawLine(pen, 5, 10, 50, 10);

            // specify the end style of the line.
            pen.setEndCap(LineCap.Flat);

            // Draw a line by calling DrawLine method.
            graphics.drawLine(pen, new Point(5, 20), new Point(50, 20));
            
            // Create an instance of HatchBrush class to define rectanglurar brush with with different settings.
            HatchBrush hatchBrush = new com.aspose.imaging.brushes.HatchBrush();
            hatchBrush.setBackgroundColor(Color.getAliceBlue().Clone());
            hatchBrush.setForegroundColor(Color.getRed().Clone());
            hatchBrush.setHatchStyle(HatchStyle.Cross);
            pen = new Pen(hatchBrush, 7);
            graphics.drawRectangle(pen, 50, 50, 20, 30);
            graphics.setBackgroundMode(com.aspose.imaging.fileformats.emf.emf.consts.EmfBackgroundMode.OPAQUE);
            graphics.drawLine(pen, 80, 50, 80, 80);
            
            // Draw a line by calling DrawLine method and passing parameters.
            pen = new Pen(new SolidBrush(Color.getAqua().Clone()), 3);
            pen.setLineJoin(LineJoin.MiterClipped);

            // Draw a polygon by calling DrawPolygon method and passing parameters with line join setting/style.
            graphics.drawPolygon(pen, new Point[] { new Point(10, 20), new Point(12, 45), new Point(22, 48), new Point(48
                , 36), new Point(30, 55) });
            pen.setLineJoin(LineJoin.Bevel);
            graphics.drawRectangle(pen, 50, 10, 10, 5);
            pen.setLineJoin(LineJoin.Round);
            graphics.drawRectangle(pen, 65, 10, 10, 5);
            pen.setLineJoin(LineJoin.Miter);
            graphics.drawRectangle(pen, 80, 10, 10, 5);

            
            // Call EndRecording method to produce the final shape. EndRecording method will return the final shape as EmfImage.
            // So create an instance of EmfImage class and initialize it with EmfImage returned by EndRecording method.           
            final EmfImage image = graphics.endRecording();

            try /*JAVA: was using*/
            {
                
                test(image, "Pens.emf");
            }
            finally
            {
                if (image != null)
                    (image).dispose();
            }
        }
    }
}

private void test(EmfImage image, String fileName)
    {
        // Create an instance of PdfOptions class.
        PdfOptions options = new PdfOptions();

        // Create an instance of EmfRasterizationOptions class and define different settings.
        EmfRasterizationOptions rasterizationOptions = new EmfRasterizationOptions();
        options.setVectorRasterizationOptions(rasterizationOptions);
        rasterizationOptions.setPageSize(com.aspose.imaging.Size.to_SizeF(image.getSize()).Clone());

        String outPath = fileName + ".pdf";

	// Call the save method to convert the EMF metafile image to PDF.
        image.save(outPath, options);
    }


Converting EMF To SVG

Using Aspose.Imaging for Java, developers can convert EMF to SVG format. Below is the code demonstration.



String sourcePath = "input\\tiger.emf";
String destPath = "output\\tiger.svg";

    // Load an existing EMF file
    Image image = Image.load(sourcePath, new MetafileLoadOptions(true));
    try
    {
        // Create an instance of EmfRasterizationOptions class and set different options
	EmfRasterizationOptions emfRasterizationOptions = new EmfRasterizationOptions();
	emfRasterizationOptions.setBackgroundColor(Color.getWhite());
	emfRasterizationOptions.setPageWidth(image.getWidth());
	emfRasterizationOptions.setPageHeight(image.getHeight());

        // call the save method and pass instance of SvgOptions class to convert it to SVG format.
	image.save(destPath, new SvgOptions() {{ setVectorRasterizationOptions(emfRasterizationOptions); }});
    }
    finally
    {
        image.dispose();
    }

Enhancements

Following enhancements have been introduced in this release.

  • Process of updating text in PSD text layers has been improved.

  • Support to load PSD file created/updated using SAI software/editing tool has been incorporated.

  • Support to load PSD file created/updated using GIMP2 software/editing tool has been incorporated.

  • Processing of Extracting TIFF Frame has been improved.

  • Process of converting TIFF to JPEG has been improved.

  • Process of converting Djvu to PDF has been improved.

  • Process of converting Djvu image pages to PNG has been improved.

  • ICC profiles for Jpeg2000 images have been implemented.

  • Process of rotate operation on raster image has been improved.

Please refer to the release notes of Aspose.Imaging for Java 3.9.0 for a full view of improvements along with sample code snippets for newly added features. If you are planning to upgrade the API to the latest revision, we strongly suggest you to check the Public API Changes section to know what has been changed in the public API since your current version.

Aspose.Imaging for Java Resources

The resources, you may need to accomplish your tasks:

As always we appreciate your feedback so if you ever have anything to tell us about this release or anything else, please head to the Aspose.Imaging forum for a chat.