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.");
}
}
}
Aggregations