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