Question

How to insert image into a word document where a word document consist of a table,that image should be inserted in one of the table cell how to do that?please help me out

Was it helpful?

Solution

Check How to: Insert a picture into a word processing document (Open XML SDK) article at MSDN . It will help you on inserting images to word docuemnt.

Also Working with WordprocessingML tables (Open XML SDK) and How to: Insert a Table into a Word Processing Document will help you on working with Tables in OpenXml.

Update :

I assume you get your desired cell in the document by th code mentioned in http://msdn.microsoft.com/en-us/library/cc850841(v=office.14).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-6 (so you have your TableCell in tc2 variable) , And also I assume you have added your desired image to document by the code mentioned in http://msdn.microsoft.com/en-us/library/office/bb497430(v=office.15).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-5 (so you have image refrence in element varialbe)

so simply run this code to add the image to your tc2 table cell :

tc2.AppendChild(new Paragraph(new Run(element)));

OTHER TIPS

The question is old and I think whoever asked has found a way to add an image into a table's cell, but I will share my code with any want might find it useful in the future.

# We need to iterate through:
            # rows       'tr_lst' then ->
            # cells      'tc_lst' then ->
            # paragraphs 'p_lst'  then ->
            # runs       'r_lst'  then checks if the run has text if not then check its XML if contains required rId
            for _tbl_row in _table.tr_lst:
                for _row_cell in _tbl_row.tc_lst:
                    for _cell_paragraph in _row_cell.p_lst:
                        for _cell_run in _cell_paragraph.r_lst:
                            if _cell_run.text == "":
                                if _rid in _cell_run.xml:
                                    image_path = _path
                                    docx__paragraph = Paragraph(_cell_paragraph, our_table._parent)
                                    #our_table is the same as the table itself before accessing its protected attribute "_tbl"
                                    # This run is considered corrupted which is why need to delete it and then add a new one
                                    _run = docx__paragraph.runs[0]._element
                                    _run.getparent().remove(_run)
                                    _run._r = _run._element = None
                                    # New run contains only an image therefore we don't need to add additional info.
                                    docx__paragraph.add_run().add_picture(image_path)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top