Search in sources :

Example 6 with HwmfRecord

use of org.apache.poi.hwmf.record.HwmfRecord 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)

Example 7 with HwmfRecord

use of org.apache.poi.hwmf.record.HwmfRecord in project tika by apache.

the class WMFParser method parse.

@Override
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
    XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
    xhtml.startDocument();
    try {
        HwmfPicture picture = new HwmfPicture(stream);
        //to determine when to keep two text parts on the same line
        for (HwmfRecord record : picture.getRecords()) {
            Charset charset = LocaleUtil.CHARSET_1252;
            //This fix should be done within POI
            if (record.getRecordType().equals(HwmfRecordType.createFontIndirect)) {
                HwmfFont font = ((HwmfText.WmfCreateFontIndirect) record).getFont();
                charset = (font.getCharSet() == null || font.getCharSet().getCharset() == null) ? LocaleUtil.CHARSET_1252 : font.getCharSet().getCharset();
            }
            if (record.getRecordType().equals(HwmfRecordType.extTextOut)) {
                HwmfText.WmfExtTextOut textOut = (HwmfText.WmfExtTextOut) record;
                xhtml.startElement("p");
                xhtml.characters(textOut.getText(charset));
                xhtml.endElement("p");
            } else if (record.getRecordType().equals(HwmfRecordType.textOut)) {
                HwmfText.WmfTextOut textOut = (HwmfText.WmfTextOut) record;
                xhtml.startElement("p");
                xhtml.characters(textOut.getText(charset));
                xhtml.endElement("p");
            }
        }
    } catch (RecordFormatException e) {
        //POI's hwmfparser can throw these for "parse exceptions"
        throw new TikaException(e.getMessage(), e);
    } catch (RuntimeException e) {
        //convert Runtime to RecordFormatExceptions
        throw new TikaException(e.getMessage(), e);
    } catch (AssertionError e) {
        //POI's hwmfparser can throw these for parse exceptions
        throw new TikaException(e.getMessage(), e);
    }
    xhtml.endDocument();
}
Also used : TikaException(org.apache.tika.exception.TikaException) HwmfRecord(org.apache.poi.hwmf.record.HwmfRecord) Charset(java.nio.charset.Charset) HwmfText(org.apache.poi.hwmf.record.HwmfText) XHTMLContentHandler(org.apache.tika.sax.XHTMLContentHandler) HwmfFont(org.apache.poi.hwmf.record.HwmfFont) HwmfPicture(org.apache.poi.hwmf.usermodel.HwmfPicture) RecordFormatException(org.apache.poi.util.RecordFormatException)

Aggregations

HwmfRecord (org.apache.poi.hwmf.record.HwmfRecord)7 HwmfPicture (org.apache.poi.hwmf.usermodel.HwmfPicture)5 File (java.io.File)4 FileInputStream (java.io.FileInputStream)4 Test (org.junit.Test)4 Charset (java.nio.charset.Charset)3 HwmfFont (org.apache.poi.hwmf.record.HwmfFont)3 HwmfText (org.apache.poi.hwmf.record.HwmfText)3 Ignore (org.junit.Ignore)3 Dimension (java.awt.Dimension)1 Graphics2D (java.awt.Graphics2D)1 AffineTransform (java.awt.geom.AffineTransform)1 Rectangle2D (java.awt.geom.Rectangle2D)1 BufferedImage (java.awt.image.BufferedImage)1 FileFilter (java.io.FileFilter)1 IOException (java.io.IOException)1 HwmfGraphics (org.apache.poi.hwmf.draw.HwmfGraphics)1 HwmfImageRecord (org.apache.poi.hwmf.record.HwmfFill.HwmfImageRecord)1 WmfSetWindowExt (org.apache.poi.hwmf.record.HwmfWindowing.WmfSetWindowExt)1 WmfSetWindowOrg (org.apache.poi.hwmf.record.HwmfWindowing.WmfSetWindowOrg)1