use of org.apache.poi.hwpf.usermodel.Picture in project poi by apache.
the class TestHWPFPictures method testImageCount.
/**
* Test that we have the right numbers of images in each file
*/
public void testImageCount() {
HWPFDocument docA = HWPFTestDataSamples.openSampleFile(docAFile);
HWPFDocument docB = HWPFTestDataSamples.openSampleFile(docBFile);
assertNotNull(docA.getPicturesTable());
assertNotNull(docB.getPicturesTable());
PicturesTable picA = docA.getPicturesTable();
PicturesTable picB = docB.getPicturesTable();
List<Picture> picturesA = picA.getAllPictures();
List<Picture> picturesB = picB.getAllPictures();
assertEquals(7, picturesA.size());
assertEquals(2, picturesB.size());
}
use of org.apache.poi.hwpf.usermodel.Picture in project poi by apache.
the class TestHWPFPictures method testMacImages.
public void testMacImages() throws Exception {
HWPFDocument docC = HWPFTestDataSamples.openSampleFile("53446.doc");
PicturesTable picturesTable = docC.getPicturesTable();
List<Picture> pictures = picturesTable.getAllPictures();
assertEquals(4, pictures.size());
int[][] expectedSizes = { // PNG
{ 185, 42 }, // PNG
{ 260, 114 }, // PNG
{ 185, 42 }, // PNG
{ 260, 114 } };
for (int i = 0; i < pictures.size(); i++) {
BufferedImage image = ImageIO.read(new ByteArrayInputStream(pictures.get(i).getContent()));
assertNotNull(image);
int[] dimensions = expectedSizes[i];
assertEquals(dimensions[0], image.getWidth());
assertEquals(dimensions[1], image.getHeight());
}
}
use of org.apache.poi.hwpf.usermodel.Picture in project portal by ixinportal.
the class WordToHtmlTest method convert2Html.
// word 转 html
public static void convert2Html(String fileName, String outPutFile) throws TransformerException, IOException, ParserConfigurationException {
HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(// WordToHtmlUtils.loadDoc(new
fileName));
// FileInputStream(inputFile));
// 兼容2007 以上版本
// XSSFWorkbook xssfwork=new XSSFWorkbook(new
// FileInputStream(fileName));
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
wordToHtmlConverter.setPicturesManager(new PicturesManager() {
public String savePicture(byte[] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches) {
return "test/" + suggestedName;
}
});
wordToHtmlConverter.processDocument(wordDocument);
// save pictures
List pics = wordDocument.getPicturesTable().getAllPictures();
if (pics != null) {
for (int i = 0; i < pics.size(); i++) {
Picture pic = (Picture) pics.get(i);
System.out.println();
try {
pic.writeImageContent(new FileOutputStream("D:/test/" + pic.suggestFullFileName()));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Document htmlDocument = wordToHtmlConverter.getDocument();
ByteArrayOutputStream out = new ByteArrayOutputStream();
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(out);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "HTML");
serializer.transform(domSource, streamResult);
out.close();
writeFile(new String(out.toByteArray()), outPutFile);
}
Aggregations