wmf and emf can not be converted into png correctly

Last post 05-23-2011, 2:52 PM by codewarior. 1 replies.
Sort Posts: Previous Next
  •  03-24-2011, 10:15 PM 292971

    wmf and emf can not be converted into png correctly Java

    Attachment: Present (inaccessible)
    I am testing the Aspose.Metafiles for java and need to use it to convert many wmf and emf files into png files. this is the testing code I am using:

    import java.awt.Color;
    import com.aspose.words.*;
    import java.io.*;
    import java.text.MessageFormat;
    import com.aspose.metafiles.*;
    import javax.imageio.ImageIO;
    import javax.imageio.stream.FileImageInputStream;
    import javax.imageio.stream.ImageInputStream;
    import javax.imageio.stream.ImageOutputStream;

    import java.awt.geom.AffineTransform;
    import java.awt.image.BufferedImage;
    import java.awt.image.renderable.RenderContext;
    import java.awt.RenderingHints;
    import java.net.URL;
    public class AsposeCreateDOC {

        public static void main(String[] args) throws Exception {
    Document doc = new Document(".. /Syllabus6.doc");
            // Get all shapes.
            NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true);
            System.out.println("there are "+shapes.getCount()+" shapes in this doc file");
            System.out.println("*****************************************************************************");
            // Loop through all shapes.
            int imageIndex = 0;
            for (int i = 0; i < shapes.getCount(); i++){
               
                Shape shape = (Shape)shapes.get(i);
             
                if(shape.hasImage()){
                   
                   String extension = imageTypeToExtension(shape.getImageData().getImageType());
                   String imageFileName = MessageFormat.format("Image.ExportImages.{0} Out.{1}", imageIndex, extension);
                   shape.getImageData().save(".. /extractedShapes/" + imageFileName);
                   imageIndex++;
                    if(extension =="wmf"){
                           ImageData image=shape.getImageData();
                           ByteArrayInputStream BAIStream = new ByteArrayInputStream(image.getImageBytes());
                  
                           WmfMetafile wmf= new WmfMetafile(BAIStream);
                           wmf.setRenderableBackgroundColor(Color.white);
                          
                           AffineTransform AT= new AffineTransform();
                           java.awt.image.renderable.RenderContext rc = new RenderContext(AT);
                          
                           int width=100;
                           int height=80;
                          
                           java.awt.RenderingHints hints = new RenderingHints(null);
                          
                           java.awt.image.RenderedImage renderedImage =  wmf.createDefaultRendering();
                           ImageIO.write(renderedImage, "png", new File("../extractedShapes/Sample"+i+".png"));
                    }
                   
                    if(extension =="emf"){
                           ImageData image=shape.getImageData();
                           ByteArrayInputStream BAIStream = new ByteArrayInputStream(image.getImageBytes());
                  
                           EmfMetafile emf= new EmfMetafile(BAIStream);
                           emf.setRenderableBackgroundColor(Color.white);
                          
                           AffineTransform AT= new AffineTransform();
                           java.awt.image.renderable.RenderContext rc = new RenderContext(AT);
                          
                           int width=100;
                           int height=80;
                          
                           java.awt.RenderingHints hints = new RenderingHints(null);
                          
                           java.awt.image.RenderedImage renderedImage =  emf.createDefaultRendering();
                           ImageIO.write(renderedImage, "png", new File("../extractedShapes/Sample"+i+".png"));
                   }
                  
                   
                 
                  
                }
               
            }
       
        }
       
        private static String imageTypeToExtension(int imageType) throws Exception{
            switch (imageType){
                case ImageType.BMP:
                    return "bmp";
                case ImageType.EMF:
                    return "emf";
                case ImageType.JPEG:
                    return "jpeg";
                case ImageType.PICT:
                    return "pict";
                case ImageType.PNG:
                    return "png";
                case ImageType.WMF:
                    return "wmf";
                default:
                    throw new Exception("Unknown image type.");
            }
        }
    }

    I can get the generated png files. but it seems like all the math operators all converted into wrong. I have put my testing word file in the attachment, could you do a test and provide a correct code sample or solution to get the correct png files ?
    thanks a lot.

     
  •  05-23-2011, 2:52 PM 305399 in reply to 292971

    Re: wmf and emf can not be converted into png correctly

    Attachment: Present (inaccessible)

    Hello Matthew,

    Thanks for using our products and sorry for replying you so late.

    I have tested the scenario using attached Aspose.Words for Java and Aspose.Metafiles for Java 1.7.1 and I am unable to notice any problem. All the images are properly being converted into PNG format. Please take a look over the attached PNG files and extracted .WMF and EMF files from word document.

    Please note that in order to convert .EMF file into PNG format, I have used the attached code snippet. In case it does not resolve your problem or you have any further query, please feel free to contact. We are really sorry for this inconvenience.

    PS, In order to convert .emf file into PNG format, you also need to use jai_codec-1.1.3.jar along with Aspose.Metafiles for Java


    Nayyer Shahbaz
    Support Developer, Aspose Sialkot Team
    About Us
    Contact Us

    Keep in touch! We're on Twitter and Facebook
     
View as RSS news feed in XML