Search in sources :

Example 1 with HwmfPicture

use of org.apache.poi.hwmf.usermodel.HwmfPicture in project poi by apache.

the class TestHwmfParsing method parse.

@Test
public void parse() throws IOException {
    File f = POIDataSamples.getSlideShowInstance().getFile("santa.wmf");
    FileInputStream fis = new FileInputStream(f);
    HwmfPicture wmf = new HwmfPicture(fis);
    fis.close();
    List<HwmfRecord> records = wmf.getRecords();
    assertEquals(581, records.size());
}
Also used : HwmfRecord(org.apache.poi.hwmf.record.HwmfRecord) File(java.io.File) FileInputStream(java.io.FileInputStream) HwmfPicture(org.apache.poi.hwmf.usermodel.HwmfPicture) Test(org.junit.Test)

Example 2 with HwmfPicture

use of org.apache.poi.hwmf.usermodel.HwmfPicture in project poi by apache.

the class TestHwmfParsing method testCyrillic.

@Test
@Ignore("If we decide we can use common crawl file specified, we can turn this back on")
public void testCyrillic() throws Exception {
    //TODO: move test file to framework and fix this
    File dir = new File("C:/somethingOrOther");
    File f = new File(dir, "ZMLH54SPLI76NQ7XMKVB7SMUJA2HTXTS-2.wmf");
    HwmfPicture wmf = new HwmfPicture(new FileInputStream(f));
    Charset charset = LocaleUtil.CHARSET_1252;
    StringBuilder sb = new StringBuilder();
    //do what Graphics does by maintaining the stack, etc.!
    for (HwmfRecord r : wmf.getRecords()) {
        if (r.getRecordType().equals(HwmfRecordType.createFontIndirect)) {
            HwmfFont font = ((HwmfText.WmfCreateFontIndirect) r).getFont();
            charset = (font.getCharSet().getCharset() == null) ? LocaleUtil.CHARSET_1252 : font.getCharSet().getCharset();
        }
        if (r.getRecordType().equals(HwmfRecordType.extTextOut)) {
            HwmfText.WmfExtTextOut textOut = (HwmfText.WmfExtTextOut) r;
            sb.append(textOut.getText(charset)).append("\n");
        }
    }
    String txt = sb.toString();
    assertContains(txt, "Общо");
    assertContains(txt, "Баланс");
}
Also used : HwmfRecord(org.apache.poi.hwmf.record.HwmfRecord) Charset(java.nio.charset.Charset) HwmfText(org.apache.poi.hwmf.record.HwmfText) File(java.io.File) FileInputStream(java.io.FileInputStream) HwmfFont(org.apache.poi.hwmf.record.HwmfFont) HwmfPicture(org.apache.poi.hwmf.usermodel.HwmfPicture) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with HwmfPicture

use of org.apache.poi.hwmf.usermodel.HwmfPicture in project poi by apache.

the class TestHwmfParsing method paint.

@Test
@Ignore("This is work-in-progress and not a real unit test ...")
public void paint() throws IOException {
    File f = POIDataSamples.getSlideShowInstance().getFile("santa.wmf");
    // File f = new File("bla.wmf");
    FileInputStream fis = new FileInputStream(f);
    HwmfPicture wmf = new HwmfPicture(fis);
    fis.close();
    Dimension dim = wmf.getSize();
    int width = Units.pointsToPixel(dim.getWidth());
    // keep aspect ratio for height
    int height = Units.pointsToPixel(dim.getHeight());
    double max = Math.max(width, height);
    if (max > 1500) {
        width *= 1500 / max;
        height *= 1500 / max;
    }
    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, new Rectangle2D.Double(0, 0, width, height));
    g.dispose();
    ImageIO.write(bufImg, "PNG", new File("bla.png"));
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) Dimension(java.awt.Dimension) File(java.io.File) FileInputStream(java.io.FileInputStream) BufferedImage(java.awt.image.BufferedImage) HwmfPicture(org.apache.poi.hwmf.usermodel.HwmfPicture) Graphics2D(java.awt.Graphics2D) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with HwmfPicture

use of org.apache.poi.hwmf.usermodel.HwmfPicture in project poi by apache.

the class TestHwmfParsing method testShift_JIS.

@Test
@Ignore("If we decide we can use the common crawl file attached to Bug 60677, " + "we can turn this back on")
public void testShift_JIS() throws Exception {
    //TODO: move test file to framework and fix this
    File f = new File("C:/data/file8.wmf");
    HwmfPicture wmf = new HwmfPicture(new FileInputStream(f));
    Charset charset = LocaleUtil.CHARSET_1252;
    StringBuilder sb = new StringBuilder();
    //do what Graphics does by maintaining the stack, etc.!
    for (HwmfRecord r : wmf.getRecords()) {
        if (r.getRecordType().equals(HwmfRecordType.createFontIndirect)) {
            HwmfFont font = ((HwmfText.WmfCreateFontIndirect) r).getFont();
            charset = (font.getCharSet().getCharset() == null) ? LocaleUtil.CHARSET_1252 : font.getCharSet().getCharset();
        }
        if (r.getRecordType().equals(HwmfRecordType.extTextOut)) {
            HwmfText.WmfExtTextOut textOut = (HwmfText.WmfExtTextOut) r;
            sb.append(textOut.getText(charset)).append("\n");
        }
    }
    String txt = sb.toString();
    assertContains(txt, "航空情報業務へのGIS");
}
Also used : HwmfRecord(org.apache.poi.hwmf.record.HwmfRecord) Charset(java.nio.charset.Charset) HwmfText(org.apache.poi.hwmf.record.HwmfText) File(java.io.File) FileInputStream(java.io.FileInputStream) HwmfFont(org.apache.poi.hwmf.record.HwmfFont) HwmfPicture(org.apache.poi.hwmf.usermodel.HwmfPicture) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with HwmfPicture

use of org.apache.poi.hwmf.usermodel.HwmfPicture 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

HwmfPicture (org.apache.poi.hwmf.usermodel.HwmfPicture)6 File (java.io.File)5 FileInputStream (java.io.FileInputStream)5 HwmfRecord (org.apache.poi.hwmf.record.HwmfRecord)5 Test (org.junit.Test)5 Ignore (org.junit.Ignore)4 Charset (java.nio.charset.Charset)3 HwmfFont (org.apache.poi.hwmf.record.HwmfFont)3 HwmfText (org.apache.poi.hwmf.record.HwmfText)3 Dimension (java.awt.Dimension)2 Graphics2D (java.awt.Graphics2D)2 BufferedImage (java.awt.image.BufferedImage)2 Rectangle2D (java.awt.geom.Rectangle2D)1 FileFilter (java.io.FileFilter)1 IOException (java.io.IOException)1 HwmfImageRecord (org.apache.poi.hwmf.record.HwmfFill.HwmfImageRecord)1 RecordFormatException (org.apache.poi.util.RecordFormatException)1 TikaException (org.apache.tika.exception.TikaException)1 XHTMLContentHandler (org.apache.tika.sax.XHTMLContentHandler)1