Search in sources :

Example 1 with HwmfImageRecord

use of org.apache.poi.hwmf.record.HwmfFill.HwmfImageRecord in project poi by apache.

the class TestHwmfParsing method parseWmfs.

@Test
@Ignore("This is work-in-progress and not a real unit test ...")
public void parseWmfs() throws IOException {
    // parse and render the extracted wmfs from the fetchWmfFromGovdocs step
    boolean outputFiles = false;
    boolean renderWmf = true;
    File indir = new File("E:\\project\\poi\\misc\\govdocs-ppt");
    File outdir = new File("build/wmf");
    outdir.mkdirs();
    final String startFile = "";
    File[] files = indir.listFiles(new FileFilter() {

        boolean foundStartFile = false;

        @Override
        public boolean accept(File pathname) {
            foundStartFile |= startFile.isEmpty() || pathname.getName().contains(startFile);
            return foundStartFile && pathname.getName().matches("(?i).*\\.wmf?$");
        }
    });
    for (File f : files) {
        try {
            String basename = f.getName().replaceAll(".*?([^/]+)\\.wmf", "$1");
            FileInputStream fis = new FileInputStream(f);
            HwmfPicture wmf = new HwmfPicture(fis);
            fis.close();
            int bmpIndex = 1;
            for (HwmfRecord r : wmf.getRecords()) {
                if (r instanceof HwmfImageRecord) {
                    BufferedImage bi = ((HwmfImageRecord) r).getImage();
                    if (bi != null && outputFiles) {
                        String filename = String.format(Locale.ROOT, "%s-%04d.png", basename, bmpIndex);
                        ImageIO.write(bi, "PNG", new File(outdir, filename));
                    }
                    bmpIndex++;
                }
            }
            if (renderWmf) {
                Dimension dim = wmf.getSize();
                int width = Units.pointsToPixel(dim.getWidth());
                // keep aspect ratio for height
                int height = Units.pointsToPixel(dim.getHeight());
                BufferedImage bufImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
                Graphics2D g = bufImg.createGraphics();
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
                g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
                wmf.draw(g);
                g.dispose();
                ImageIO.write(bufImg, "PNG", new File(outdir, basename + ".png"));
            }
        } catch (Exception e) {
            System.out.println(f.getName() + " ignored.");
        }
    }
}
Also used : HwmfImageRecord(org.apache.poi.hwmf.record.HwmfFill.HwmfImageRecord) HwmfRecord(org.apache.poi.hwmf.record.HwmfRecord) Dimension(java.awt.Dimension) FileInputStream(java.io.FileInputStream) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) HwmfPicture(org.apache.poi.hwmf.usermodel.HwmfPicture) Graphics2D(java.awt.Graphics2D) FileFilter(java.io.FileFilter) File(java.io.File) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Dimension (java.awt.Dimension)1 Graphics2D (java.awt.Graphics2D)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 FileFilter (java.io.FileFilter)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 HwmfImageRecord (org.apache.poi.hwmf.record.HwmfFill.HwmfImageRecord)1 HwmfRecord (org.apache.poi.hwmf.record.HwmfRecord)1 HwmfPicture (org.apache.poi.hwmf.usermodel.HwmfPicture)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1