Search in sources :

Example 11 with Picture

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());
}
Also used : Picture(org.apache.poi.hwpf.usermodel.Picture) PicturesTable(org.apache.poi.hwpf.model.PicturesTable)

Example 12 with Picture

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());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Picture(org.apache.poi.hwpf.usermodel.Picture) PicturesTable(org.apache.poi.hwpf.model.PicturesTable) BufferedImage(java.awt.image.BufferedImage)

Example 13 with Picture

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);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Document(org.w3c.dom.Document) PicturesManager(org.apache.poi.hwpf.converter.PicturesManager) WordToHtmlConverter(org.apache.poi.hwpf.converter.WordToHtmlConverter) HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Picture(org.apache.poi.hwpf.usermodel.Picture) PictureType(org.apache.poi.hwpf.usermodel.PictureType) List(java.util.List)

Aggregations

Picture (org.apache.poi.hwpf.usermodel.Picture)13 PicturesTable (org.apache.poi.hwpf.model.PicturesTable)6 HWPFDocument (org.apache.poi.hwpf.HWPFDocument)4 CharacterRun (org.apache.poi.hwpf.usermodel.CharacterRun)4 ArrayList (java.util.ArrayList)3 Range (org.apache.poi.hwpf.usermodel.Range)3 FileNotFoundException (java.io.FileNotFoundException)2 List (java.util.List)2 Transformer (javax.xml.transform.Transformer)2 TransformerFactory (javax.xml.transform.TransformerFactory)2 DOMSource (javax.xml.transform.dom.DOMSource)2 StreamResult (javax.xml.transform.stream.StreamResult)2 PicturesManager (org.apache.poi.hwpf.converter.PicturesManager)2 WordToHtmlConverter (org.apache.poi.hwpf.converter.WordToHtmlConverter)2 Field (org.apache.poi.hwpf.usermodel.Field)2 Paragraph (org.apache.poi.hwpf.usermodel.Paragraph)2 PictureType (org.apache.poi.hwpf.usermodel.PictureType)2 Document (org.w3c.dom.Document)2 BufferedImage (java.awt.image.BufferedImage)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1