https://bz.apache.org/bugzilla/show_bug.cgi?id=49765

--- Comment #47 from Stefan Edelbusch <[email protected]> ---
I am using POI-4.1.2 for my tests.

I now created a small test, that inserts in a docx-template an image file.
When the resulting document is opende it results an error message that says
that ther is content in the file, that can not be read.  

To execute this test please insert the two attachements that I just uploaded to
folder "C:\TEMP\xwpfrun_addpicture_testfiles" and than run the following
Unit-Test:




public class Ticket_xwpfrun_addpicture extends BaseTest {
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }


    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }


    private List<XWPFParagraph> getParagraphsOfTablesRecursive(List<XWPFTable>
tables) {

        List<XWPFParagraph> paraList = new ArrayList<XWPFParagraph>();
        for (XWPFTable table : tables)
        {
            for (XWPFTableRow row : table.getRows())
            {
                for (XWPFTableCell cell : row.getTableCells())
                {
                    List<XWPFParagraph> cellParaList = cell.getParagraphs();
                    paraList.addAll(cellParaList);

                    List<XWPFTable> cellTables = cell.getTables();
                    List<XWPFParagraph> tablesParaList =
getParagraphsOfTablesRecursive(cellTables);
                    paraList.addAll(tablesParaList);
                }
            }
        }
        return(paraList);
    }

    public void insertImageAtBookmark(List<XWPFParagraph> paraList,String
bookmarkName,String fileName, int pictureType) {
        Iterator<XWPFParagraph> paraIter = paraList.iterator();

        while(paraIter.hasNext()) {
            XWPFParagraph para = paraIter.next();

            List<CTBookmark> bookmarkList =
para.getCTP().getBookmarkStartList();
            Iterator<CTBookmark> bookmarkIter = bookmarkList.iterator();

            while (bookmarkIter.hasNext()) {
                CTBookmark bookmark = bookmarkIter.next();
                String nameActual = bookmark.getName();
                if (nameActual.equalsIgnoreCase(bookmarkName)) {

                    final XWPFRun run = para.createRun();
                    InputStream pic1 = null;
                    try {
                        pic1 = new FileInputStream(fileName);
                        run.addPicture(pic1, pictureType, "my pic 1",
Units.toEMU(200), Units.toEMU(200));
                        pic1.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (InvalidFormatException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

        @Test
    public void testFromScratch() throws Exception {
        InputStream docTemplate = new
FileInputStream("C:/TEMP/xwpfrun_addpicture_testfiles/xwpfrun_addpicture_template.docx");
        XWPFDocument docx = new XWPFDocument(docTemplate);

        List<XWPFParagraph> paraList = new ArrayList<XWPFParagraph>();
        List<XWPFParagraph> docxParaList = docx.getParagraphs();
        paraList.addAll(docxParaList);
        List<XWPFTable> tables = docx.getTables();
        List<XWPFParagraph> tablesParaList =
getParagraphsOfTablesRecursive(tables);
        paraList.addAll(tablesParaList);

        String bookmarkName = "REKKUND_DOCS_PATH";
        String fileName =
"C:/TEMP/xwpfrun_addpicture_testfiles/xwpfrun_addpicture_testimage.jpg";
        insertImageAtBookmark(paraList, bookmarkName,
fileName,Document.PICTURE_TYPE_JPEG);

        FileOutputStream fos = new
FileOutputStream("C:/TEMP/xwpfrun_addpicture_testfiles/xwpfrun_addpicture_with_image.docx");
        docx.write(fos);
    }
}

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to