Page 1 of 1

docx4j

PostPosted: Wed Sep 06, 2017 9:52 pm
by aka241193
How to remove images from docx4j and their corresponding relationship.

Re: docx4j

PostPosted: Wed Sep 06, 2017 10:30 pm
by jason
1. Assuming you have the JAXB object which attaches the image to the MainDocument part (or header part, footer part), inspect it to get the relId.
2. Now access the relationship part, and delete the rel with that id.
3. Now remove the JAXB object (in 1 above) from whatever content list you found it in

Re: docx4j

PostPosted: Wed Sep 06, 2017 11:47 pm
by aka241193
if I have an existing docx file, how to access the JAXB obect corresponding to the image i need to remove

Re: docx4j

PostPosted: Thu Sep 07, 2017 8:17 am
by jason
Classically, find it using TraversalUtil or XPath (see Docx4j Getting Started or the cheat sheet).

Perhaps easier is to use https://github.com/plutext/docx4j/blob/ ... inder.java

You need to know what object you are looking for. How to figure that out? Perhaps easiest is to install the Docx4j Helper Word AddIn (see downloads), then open your docx in Word. Navigate to the object of interest, then select it, then use the helper to generate Java code. There you'll see what object is being used.

(There are 2 ways to represent an image, and then there are a whole lot of other graphical objects, such as SmartArt, WordArt, charts, formulae etc)

Re: docx4j

PostPosted: Thu Sep 07, 2017 4:20 pm
by aka241193
Code: Select all
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File("C:/Users/aagarwal/Desktop/02_Service_Plan.docx"));
       //Identifying JAXB object
       MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();

       String xpath = "//w:drawing";
       List<Object> list = documentPart.getJAXBNodesViaXPath(xpath, true);
       System.out.println(list.size());
       //Removing the relationship     
       MainDocumentPart Part = wordMLPackage.getMainDocumentPart();
       RelationshipsPart rp = Part.getRelationshipsPart();

       for ( Relationship r : rp.getRelationships().getRelationship() ) {
          Part part = rp.getPart(r);
          if (part instanceof ImagePngPart) {
             rp.removeRelationship(r);
             break;

          }
       }
       //removing the JAXB object
       list.remove(0);
       wordMLPackage.save(new java.io.File("C:/Users/aagarwal/Desktop/02_Service_Plan2.docx"));[img][/img]



THe place where image used to appear shows READ ERROR. AS per my thinking the relationship is still not removed

Re: docx4j

PostPosted: Thu Sep 07, 2017 9:28 pm
by aka241193
Pleases respond

Re: docx4j

PostPosted: Thu Sep 07, 2017 10:52 pm
by aka241193
Code: Select all
<w:sdt>
                    <w:sdtPr>
                        <w:id w:val="-172113594"/>
                        <w:picture/>
                    </w:sdtPr>
                    <w:sdtContent>
                        <w:p w14:paraId="542F0169" w14:textId="4DBB1CD1">
                            <w:r>
                                <w:rPr>
                                    <w:noProof/>
                                </w:rPr>
                                <w:drawing>
                                    <wp:inline distT="0" distB="0" distL="0" distR="0">
                                        <wp:extent cx="1905000" cy="927184"/>
                                        <wp:effectExtent l="0" t="0" r="0" b="6350"/>
                                        <wp:docPr id="2" name="Picture 1"/>
                                        <wp:cNvGraphicFramePr>
                                            <a:graphicFrameLocks noChangeAspect="true"/>
                                        </wp:cNvGraphicFramePr>
                                        <a:graphic>
                                            <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
                                                <pic:pic>
                                                    <pic:nvPicPr>
                                                        <pic:cNvPr id="0" name="Picture 1"/>
                                                        <pic:cNvPicPr>
                                                            <a:picLocks noChangeAspect="true" noChangeArrowheads="true"/>
                                                        </pic:cNvPicPr>
                                                    </pic:nvPicPr>
                                                    <pic:blipFill>
                                                        <a:blip r:embed="rId8"/>
                                                        <a:stretch>
                                                            <a:fillRect/>
                                                        </a:stretch>
                                                    </pic:blipFill>
                                                    <pic:spPr bwMode="auto">
                                                        <a:xfrm>
                                                            <a:off x="0" y="0"/>
                                                            <a:ext cx="1905000" cy="927184"/>
                                                        </a:xfrm>
                                                        <a:prstGeom prst="rect">
                                                            <a:avLst/>
                                                        </a:prstGeom>
                                                        <a:noFill/>
                                                        <a:ln>
                                                            <a:noFill/>
                                                        </a:ln>
                                                    </pic:spPr>
                                                </pic:pic>
                                            </a:graphicData>
                                        </a:graphic>
                                    </wp:inline>
                                </w:drawing>
                            </w:r>
                        </w:p>
                    </w:sdtContent>
                </w:sdt>

Re: docx4j

PostPosted: Thu Sep 07, 2017 11:00 pm
by aka241193
Above is the xml partof the code that represent the image in the docx file

Re: docx4j

PostPosted: Fri Sep 08, 2017 10:25 am
by jason
Notice your XML contains <a:blip r:embed="rId8"/>

That's the rel id.

In your code, you remove the first rel of type png that you encounter.

Looks like that might be the wrong one. Remove the one with the corresponding rel id (ie rId8)

Re: docx4j

PostPosted: Fri Sep 08, 2017 3:53 pm
by aka241193
Code: Select all
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File("C:/Users/aagarwal/Desktop/02_Service_Plan.docx"));
       //Identifying JAXB object
       MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();

       String xpath = "//w:picture";
       List<Object> list = documentPart.getJAXBNodesViaXPath(xpath, false);
       System.out.println(list.size());
       //Removing the relationship     
       MainDocumentPart Part = wordMLPackage.getMainDocumentPart();
       RelationshipsPart rp = Part.getRelationshipsPart();

       for ( Relationship r : rp.getRelationships().getRelationship() ) {
          Part part = rp.getPart(r);
          if (part instanceof ImagePngPart) {
             rp.removeRelationship(r);
             part.remove();
             System.out.println(part.getSourceRelationship().getId());//and it gives rel id= rId8
             break;

          }
       }
       //removing the JAXB object
       list.remove(0);
       wordMLPackage.save(new java.io.File("C:/Users/aagarwal/Desktop/02_Service_Plan2.docx"));


it means i am removing the correct part.but still the same problem exists

Re: docx4j

PostPosted: Fri Sep 08, 2017 4:01 pm
by jason
Ok, save and unzip your resulting docx. Is the w:drawing element you tried to delete still present?

Re: docx4j

PostPosted: Fri Sep 08, 2017 4:10 pm
by aka241193
One thing I notice is that even though i try to remove the relationship and the Image part. the corresponding rel id still exists in the unzipped output file. it means something is not right.
Code: Select all
<w:sdt>
                    <w:sdtPr>
                        <w:id w:val="-172113594"/>
                        <w:picture/>
                    </w:sdtPr>
                    <w:sdtContent>
                        <w:p w14:paraId="542F0169" w14:textId="4DBB1CD1">
                            <w:r>
                                <w:rPr>
                                    <w:noProof/>
                                </w:rPr>
                                <w:drawing>
                                    <wp:inline distT="0" distB="0" distL="0" distR="0">
                                        <wp:extent cx="1905000" cy="927184"/>
                                        <wp:effectExtent l="0" t="0" r="0" b="6350"/>
                                        <wp:docPr id="2" name="Picture 1"/>
                                        <wp:cNvGraphicFramePr>
                                            <a:graphicFrameLocks noChangeAspect="true"/>
                                        </wp:cNvGraphicFramePr>
                                        <a:graphic>
                                            <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
                                                <pic:pic>
                                                    <pic:nvPicPr>
                                                        <pic:cNvPr id="0" name="Picture 1"/>
                                                        <pic:cNvPicPr>
                                                            <a:picLocks noChangeAspect="true" noChangeArrowheads="true"/>
                                                        </pic:cNvPicPr>
                                                    </pic:nvPicPr>
                                                    <pic:blipFill>
                                                        <a:blip r:embed="rId8"/>
                                                        <a:stretch>
                                                            <a:fillRect/>
                                                        </a:stretch>
                                                    </pic:blipFill>
                                                    <pic:spPr bwMode="auto">
                                                        <a:xfrm>
                                                            <a:off x="0" y="0"/>
                                                            <a:ext cx="1905000" cy="927184"/>
                                                        </a:xfrm>
                                                        <a:prstGeom prst="rect">
                                                            <a:avLst/>
                                                        </a:prstGeom>
                                                        <a:noFill/>
                                                        <a:ln>
                                                            <a:noFill/>
                                                        </a:ln>
                                                    </pic:spPr>
                                                </pic:pic>
                                            </a:graphicData>
                                        </a:graphic>
                                    </wp:inline>
                                </w:drawing>
                            </w:r>
                        </w:p>
                    </w:sdtContent>
                </w:sdt>


xml part of the resultant file

Re: docx4j

PostPosted: Fri Sep 08, 2017 4:18 pm
by aka241193
The READ ERROR is showing in the corresponding place where image used to appear is because i could remove the Imagepart from the content_type and the part doesnot show in the part name list. but i guess the main document part still has it and still shows it as w:drawing but could not find the corresponding image part.
what else i need to do to remove the w:drawing from the main document part and the relationship

Re: docx4j

PostPosted: Fri Sep 08, 2017 4:22 pm
by jason
So list.remove(0) is removing something different to what you intend.

If you want more help, you'll need to post source docx, resulting docx, and executable code.

You can anonymise the docx first if you wish: https://github.com/plutext/docx4j/blob/ ... ingle.java

Re: docx4j

PostPosted: Fri Sep 08, 2017 4:32 pm
by aka241193
I have already mentioned everything and all of the code

Re: docx4j

PostPosted: Fri Sep 08, 2017 5:01 pm
by aka241193
Actually i checked it now. list.remove(0) is not removing anything
is there any other way to remove jaxb object

Re: docx4j

PostPosted: Fri Sep 08, 2017 5:06 pm
by jason
E remove(int index) Removes the element at the
specified position in this list (optional operation). Shifts
any subsequent elements to the left (subtracts one
from their indices). Returns the element that was
removed from the list.


What is it returning?

Re: docx4j

PostPosted: Fri Sep 08, 2017 5:19 pm
by aka241193
javax.xml.bind.JAXBElement@601d6622

Re: docx4j

PostPosted: Fri Sep 08, 2017 5:35 pm
by aka241193
Can you please provide me the working code to remove image from the docx file using docx4j

Re: docx4j

PostPosted: Fri Sep 08, 2017 7:14 pm
by jason
The only real challenge here is knowing which image you want to remove. What are your criteria for selecting that? eg the nth picture sdt in the main document part?

Re: docx4j

PostPosted: Fri Sep 08, 2017 10:14 pm
by aka241193
There is only one image in the entire document and one sdt element containing that image. so it simplifies the fact of locating the image

Re: docx4j

PostPosted: Mon Sep 11, 2017 10:53 pm
by jason
Here's a basic example (without any error handling)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
import java.io.File;
import java.util.List;

import org.docx4j.Docx4J;
import org.docx4j.XmlUtils;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.relationships.Relationship;
import org.docx4j.wml.Drawing;
import org.docx4j.wml.R;
import org.docx4j.dml.wordprocessingDrawing.Inline;
import org.docx4j.dml.Graphic;
import org.docx4j.dml.GraphicData;
import org.docx4j.dml.picture.Pic;


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

            String inputfilepath = System.getProperty("user.dir") + "/picture_cc.docx";
               
                // Load the docx
                WordprocessingMLPackage wordMLPackage = Docx4J.load(new java.io.File(inputfilepath));
               
                // 1. find the picture
               String xpath = "//w:drawing";
               List<Object> list = wordMLPackage.getMainDocumentPart().getJAXBNodesViaXPath(xpath, true);
               
               Object toBeRemoved = list.get(0);
               Drawing drawing = (Drawing)XmlUtils.unwrap(toBeRemoved);
               
                // 2. get the relId
                Inline inline = (Inline)drawing.getAnchorOrInline().get(0);
                Graphic g = inline.getGraphic();
                GraphicData graphicdata = g.getGraphicData();
                Object o = graphicdata.getAny().get(0);
                Pic pic = (Pic)XmlUtils.unwrap(o);
                String relId = pic.getBlipFill().getBlip().getEmbed();
               
                // 3. delete the rel
                Relationship rel = wordMLPackage.getMainDocumentPart().getRelationshipsPart(false).getRelationshipByID(relId);
                wordMLPackage.getMainDocumentPart().getRelationshipsPart(false).removeRelationship(rel);
               
                // 4. remove the drawing
                System.out.println(drawing.getParent().getClass().getName());
                R r = (R)drawing.getParent();
                r.getContent().remove(toBeRemoved);
               
                // Save it
                String outputfilepath = System.getProperty("user.dir") + "/OUT_ImageDelete.docx";
                Docx4J.save(wordMLPackage, new File(outputfilepath), Docx4J.FLAG_NONE); //(FLAG_NONE == default == zipped docx)
               
                System.out.println("Saved: " + outputfilepath);
        }
               

}
 
Parsed in 0.020 seconds, using GeSHi 1.0.8.4