Page 1 of 1

how can i remove some content of document

PostPosted: Thu Nov 17, 2011 9:08 pm
by tosswang
hi,all
i want to remove some content of word document ,below is my code

Code: Select all
public class TestDocx4jSnippet
{
   
   public static void main(String[] str)
   {
      try
      {
         LoadFromZipNG z = new LoadFromZipNG();
         WordprocessingMLPackage wordMLPackage = (WordprocessingMLPackage) z.get(new FileInputStream(new java.io.File("C:/Users/toss/Desktop/Doc1.docx")));
         
         MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
         
         String xpath = "//w:r[w:t[contains(text(),'" + "indicator" + "')]]";
         
         List<Object> list = documentPart.getJAXBNodesViaXPath(xpath, false);
         
         for (int i = 0; i < list.size(); i++)
         {
            org.docx4j.wml.R r = (org.docx4j.wml.R) list.get(i);
            
            org.docx4j.wml.P parent = (org.docx4j.wml.P) r.getParent();
            
            parent.getContent().remove(r);
         }
         
         ByteArrayOutputStream fos = new ByteArrayOutputStream();
         SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
         saver.save(fos);
         
      }
      catch(Exception e)
      {
         e.printStackTrace();
      }
   }
}


i find the content that isn't removed, what' wrong with my code ?

Any info would be greatly appreciated!

i use docx4j-2.7.0.jar

the attachment is my test document

Re: how can i remove some content of document

PostPosted: Fri Nov 18, 2011 10:09 am
by jason
I haven't looked at your docx, to see how many objects your xpath should match, but it matches 1, and removes it properly:

Code: Select all
Will remove: <w:r ..>
    <w:rPr>
        <w:rFonts w:hint="eastAsia"/>
    </w:rPr>
    <w:t>[[indicator]]</w:t>
</w:r>

from: <w:p ..>
    <w:r>
        <w:rPr>
            <w:rFonts w:hint="eastAsia"/>
        </w:rPr>
        <w:t>[[indicator]]</w:t>
    </w:r>
    <w:r w:rsidR="00490980">
        <w:rPr>
            <w:rFonts w:hint="eastAsia"/>
        </w:rPr>
        <w:t>test</w:t>
    </w:r>
</w:p>

Result: <w:p ..>
    <w:r w:rsidR="00490980">
        <w:rPr>
            <w:rFonts w:hint="eastAsia"/>
        </w:rPr>
        <w:t>test</w:t>
    </w:r>
</w:p>



You can see this using XmlUtils.marshaltoString(parent, true, true) etc

Re: how can i remove some content of document

PostPosted: Fri Nov 18, 2011 1:56 pm
by tosswang
hi,jason:
thank your reply,I saw the same results too,
Code: Select all
Result: <w:p ..>
    <w:r w:rsidR="00490980">
        <w:rPr>
            <w:rFonts w:hint="eastAsia"/>
        </w:rPr>
        <w:t>test</w:t>
    </w:r>
</w:p>


i try to save result by using below code

Code: Select all

ByteArrayOutputStream fos = new ByteArrayOutputStream();
         SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
         saver.save(fos);

but the word document doesn't change...

How should I do?

thanks again!

Re: how can i remove some content of document

PostPosted: Fri Nov 18, 2011 3:43 pm
by tosswang
oh,i see,my code that is saved content to word document is wrong,

Code: Select all
ByteArrayOutputStream fos = new ByteArrayOutputStream();
         SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
         saver.save(fos);


it saved content to ByteArrayOutputStream ,not saved content to the word file !