Page 1 of 1

Insert an altChunk within a table cell

PostPosted: Mon May 19, 2014 3:29 pm
by david.zhaowl
Hi everybody,

To continue my post here : http://www.docx4java.org/forums/docx-java-f6/convert-a-html-file-into-one-column-of-docx-table-t1888.html?sid=9b7b2605f896ed74f148fbb7ee01e9f3 , I'm wondering if it's possible to add an AltChunk with in a table cell object? I have to the following example code now:

Code: Select all
public class AltChunkAddOfTypeHtml {

   private static ObjectFactory factory;

   public static void main(String[] args) throws Exception {

      WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
            .createPackage();
      factory = Context.getWmlObjectFactory();
      Tbl table = factory.createTbl();
      Tr tableRow = factory.createTr();
      Tc tableCell = factory.createTc();
      tableCell.getContent().add(
            wordMLPackage.getMainDocumentPart().createParagraphOfText(
                  "Field 1"));
      tableRow.getContent().add(tableCell);

      table.getContent().add(tableRow);

      wordMLPackage.getMainDocumentPart().addObject(table);

      String html = "<html><head><title>Import me</title></head><body><p>Hello World!</p></body></html>";

      wordMLPackage.getMainDocumentPart().addAltChunk(AltChunkType.Html,
            html.getBytes());

      wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/test.docx"));
   }

}


How to add AltChunk into tableCell? Or is there other approach to do that? I want to insert a part of html code into a table cell in a docx file. Thanks.

Re: Insert an altChunk within a table cell

PostPosted: Mon May 19, 2014 4:31 pm
by jason
Hi David

If you have a quick look at the source code in https://github.com/plutext/docx4j/blob/ ... kHost.java
you'll see that the addAltChunk methods do two things:

1. they create an AlternativeFormatInputPart and populate it with your content

2. they add an altChunk referencing that part to some content list.

Since you don't want to add the altChunk at the document body level, you need the method signature which lets you specify the attachment point

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
        public AlternativeFormatInputPart addAltChunk(AltChunkType type, byte[] bytes,
                        ContentAccessor attachmentPoint)   throws Docx4JException {
               
                AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(type);
                Relationship altChunkRel = this.addTargetPart(afiPart, AddPartBehaviour.RENAME_IF_NAME_EXISTS);
                // now that its attached to the package ..
                afiPart.registerInContentTypeManager();
               
                afiPart.setBinaryData(bytes);          
               
                // .. the bit in document body
                CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk();
                ac.setId(altChunkRel.getId() );
                attachmentPoint.getContent().add(ac);
                                       
                return afiPart;
        }
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


So that third arg should be your tableCell object:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
@XmlRootElement(name = "tc")
public class Tc
    implements Child, ContentAccessor
 
Parsed in 0.013 seconds, using GeSHi 1.0.8.4

Re: Insert an altChunk within a table cell

PostPosted: Tue May 20, 2014 12:27 am
by david.zhaowl
Thanks for your help Jason. It works nicely.

Re: Insert an altChunk within a table cell

PostPosted: Mon Feb 01, 2021 10:25 pm
by micrf
This seems to work till Word 2019.
After upgrading Word 2016 -> Word 2019 I could not open document anymore: HRESULT 0x80004005

Testcode:

Code: Select all
@Test
void tabelCellChunk_CouldNotOpenWord2019() throws Exception {
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
        MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();

        ObjectFactory factory = Context.getWmlObjectFactory();
        Tbl table = factory.createTbl();
        Tr row = factory.createTr();
        Tc cell = factory.createTc();

        String html = "<html><head></head><body><table><tr><td>TC11</td><td>TC12</td></tr><tr><td>TC21</td><td>TC22</td></tr></table></body></html>";
        mdp.addAltChunk(AltChunkType.Html, html.getBytes(), cell);
        row.getContent().add(cell);
        table.getContent().add(row);
        mdp.addObject(table);


        // Display result
        System.out.println(
            XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true));

        Path path = Paths.get("target", "CouldNotOpenWord2019.docx");
        OutputStream out = Files.newOutputStream(path);
        wordMLPackage.save(out);
        out.close();
    }


What do I have to change?
Thanks a lot!

It works adding an additional p after added chunk, but I don't need this additional spacing!
Used versions:
    docx4j-core 11.2.8
    docx4j-JAXB-ReferenceImpl 11.2.8
    docx4j-ImportXHTML 8.2.0

Re: Insert an altChunk within a table cell

PostPosted: Wed Feb 03, 2021 6:32 am
by jason
Its a little surprising that Microsoft has changed that behaviour in Word 2019.

That said, Word doesn't like "empty" table cells, so the fact that you might need an empty P isn't surprising.

I'm afraid it is what it is. You could make your additional P tiny (0 line spacing, small font).

Or alternatively, you could switch to using -ImportXHTML.