This is an automated email from the ASF dual-hosted git repository. centic pushed a commit to branch trunk in repository https://gitbox.apache.org/repos/asf/poi.git
commit d72e3a608ce4b46490542f85b80c3b5409488357 Author: Dominik Stadler <[email protected]> AuthorDate: Fri Jan 16 09:16:09 2026 +0100 Add a file-handler for .emf files These can be embedded in slideshows and POI supports some handling of them. Also Tika uses this code to extract some textual information from such images. --- .../org/apache/poi/stress/FileHandlerKnown.java | 1 + .../org/apache/poi/stress/HEMFFileHandler.java | 85 +++++++++++++++++++++ test-data/spreadsheet/stress.xls | Bin 76288 -> 76800 bytes 3 files changed, 86 insertions(+) diff --git a/poi-integration/src/test/java/org/apache/poi/stress/FileHandlerKnown.java b/poi-integration/src/test/java/org/apache/poi/stress/FileHandlerKnown.java index dc2af11ba9..1e113c714a 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/FileHandlerKnown.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/FileHandlerKnown.java @@ -29,6 +29,7 @@ public enum FileHandlerKnown { HSLF, HSMF, HSSF, + HEMF, HWMF, HWPF, OPC, diff --git a/poi-integration/src/test/java/org/apache/poi/stress/HEMFFileHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/HEMFFileHandler.java new file mode 100644 index 0000000000..dd0e5fbe27 --- /dev/null +++ b/poi-integration/src/test/java/org/apache/poi/stress/HEMFFileHandler.java @@ -0,0 +1,85 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.stress; + +import static org.junit.jupiter.api.Assertions.assertInstanceOf; + +import java.awt.geom.Rectangle2D; +import java.awt.image.BufferedImage; +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; + +import org.apache.poi.hemf.record.emf.HemfRecord; +import org.apache.poi.hemf.record.emf.HemfRecordType; +import org.apache.poi.hemf.record.emf.HemfText; +import org.apache.poi.hemf.usermodel.HemfPicture; +import org.junit.jupiter.api.Test; + +public class HEMFFileHandler implements FileHandler { + + @Override + public void handleExtracting(File file) throws Exception { + try (InputStream stream = new BufferedInputStream(new FileInputStream(file))) { + HemfPicture picture = new HemfPicture(stream); + + // mimic a bit what e.g. Tika does to extract some information from .emf files + for (HemfRecord record : picture.getRecords()) { + if (record.getEmfRecordType().equals(HemfRecordType.extTextOutW)) { + assertInstanceOf(HemfText.EmfExtTextOutW.class, record); + HemfText.EmfExtTextOutW textOut = (HemfText.EmfExtTextOutW) record; + textOut.getText(StandardCharsets.UTF_16LE); + } else if (record.getEmfRecordType().equals(HemfRecordType.extTextOutA)) { + assertInstanceOf(HemfText.EmfExtTextOutA.class, record); + HemfText.EmfExtTextOutA textOut = (HemfText.EmfExtTextOutA) record; + textOut.getText(StandardCharsets.UTF_8); + } + } + } + } + + @Override + public void handleAdditional(File file) throws Exception { + // no additional checks for now + } + + @Override + public void handleFile(InputStream stream, String path) throws Exception { + HemfPicture picture = new HemfPicture(stream); + + for (HemfRecord record : picture.getRecords()) { + record.getEmfRecordType(); + record.getGenericRecordType(); + } + + BufferedImage dest = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB); + picture.draw(dest.createGraphics(), new Rectangle2D.Double(0, 0, 256, 256)); + } + + @Test + void test() throws Exception { + String file = "test-data/slideshow/wrench.emf"; + + try (InputStream stream = new BufferedInputStream(new FileInputStream(file))) { + handleFile(stream, file); + } + + handleExtracting(new File(file)); + } +} diff --git a/test-data/spreadsheet/stress.xls b/test-data/spreadsheet/stress.xls index a9d24e8244..a03baec081 100644 Binary files a/test-data/spreadsheet/stress.xls and b/test-data/spreadsheet/stress.xls differ --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
