Page 1 of 1

Conflict between image in Header and Body

PostPosted: Wed Jul 21, 2010 1:31 am
by rabriol
Hi Jason i have a problem...

so look!!!

i'm reading a document

Code: Select all
WordprocessingMLPackage.load(new File("C://model.docx"));



after this i'm creating an ArrayList of each elements on the body, because i need to identify where the tables are, to put a image. Up here is everything ok.

but before save the new document i'm using the header's model.docx to create my own like this:

Code: Select all
            HashMap<Integer, String> map = new HashMap<Integer, String>();
           
            //here the map record each elements of body
            map = inspectDocument(wordMLPackage);

            ObjectFactory factory = new ObjectFactory();
            MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
            Document wmlDocumentEl = documentPart.getJaxbElement();
            //after to discoverer and map each elements of body i create a new body object to poputale
            Body body = factory.createBody();
           
            int i = 0;
            while (i < map.size()) {
                if (map.get(i).contains("inserir foto")) {
                        body.getEGBlockLevelElts().add(createTableWithPicture(ctg.getFotos(), wordMLPackage));
                }
                else if (map.get(i).equals("P")) {
                    body.getEGBlockLevelElts().add(wmlDocumentEl.getBody().getEGBlockLevelElts().get(i));
                }
                else if (map.get(i).equals("Tbl")) {
                    body.getEGBlockLevelElts().add(wmlDocumentEl.getBody().getEGBlockLevelElts().get(i));
                }               
                i++;
            }
           
            SaveToZipFile saver = new SaveToZipFile(wordMLPackage);

            //and before to save the document i fill body's properties with model.docx
            body.setSectPr(wmlDocumentEl.getBody().getSectPr());
            body.setParent(wmlDocumentEl.getBody().getParent());
            saver.save("C:\\relatório.docx");

but when i save the document, and try open it, the image on header was substitute with the others image there i had add on document.

i know that the error is :
Code: Select all
body.setSectPr(wmlDocumentEl.getBody().getSectPr());
            body.setParent(wmlDocumentEl.getBody().getParent());


but i don't know how to copy the header with the image and others attributes to add in my new document or change the name of the header's image in /word/media to solve the conflict.

Re: Conflict between image in Header and Body

PostPosted: Mon Jul 26, 2010 8:15 pm
by jason
Images involve three things:

• the image part itself
• a relationship, in the relationships part of the main document part (or header part etc). This relationship includes:
o the name of the image part (for example, /word/media/image1.jpeg)
o the relationship ID
• some XML in the main document part (or header part etc), referencing the relationship ID (see w:drawing and w:pict examples above)

This means that if you are moving images around, you need to take care to ensure that the relationships remain valid.

You can manually manipulate the relationship, and you can manually manipulate the XML referencing the relationship IDs.

Given an image part, you can get the relationship pointing to it

Relationship rel = copiedImagePart.getSourceRelationship();
String id = rel.getId();

You can then ensure the reference matches.