Page 1 of 1

Merging the documents may not getting the headers

PostPosted: Fri Sep 30, 2016 10:37 pm
by Sindhu
Merging the documents using
WordprocessingMLPackage template=null;

does not getting the headers to the template.
Every thing is working fine without merging the document i.e works only for single document.

Please response as soon as possible.
thanx in advance

Re: Merging the documents may not getting the headers

PostPosted: Wed Oct 12, 2016 3:27 pm
by jason
I don't know what your dUtil.mergeDocFiles method does, but generally speaking, to merge docx files you'll need to takecare of relationships. See further http://www.docx4java.org/blog/2010/11/m ... documents/

The easiest/most reliable way to do this is to use Plutext's commercial Docx4j Enterprise ;-)

Re: Merging the documents may not getting the headers

PostPosted: Fri Dec 16, 2016 9:29 pm
by Sindhu
WARN : org.docx4j.openpackaging.io.LoadFromZipNG - No JAXB model for this CustomXmlDataStorage part; unexpected element (uri:"http://schemas.microsoft.com/office/2006/coverPageProps", local:"CoverPageProperties"). Expected elements are <{urn:schemas-microsoft-com:office:excel}ClientData>,<{urn:schemas-microsoft-com:office:office}OLEObject>,<{http://opendope.org/SmartArt/DataHierarchy}SmartArtDataHierarchy>,<{http://schemas.openxmlformats.org/officeDocument/2006/bibliography}Sources>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}abstractNum>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}alias>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}altChunk>,<{http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing}anchor>,<{urn:schemas-microsoft-com:office:word}anchorlock>,<{http://opendope.org/conditions}and>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}annotationRef>,<{urn:schemas-microsoft-com:vml}arc>,<{urn:schemas-microsoft-com:vml}background>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}bibliography>,<{http://schemas.openxmlformats.org/drawingml/2006/main}blip>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}body>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}bookmarkEnd>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}bookmarkStart>,<{urn:schemas-microsoft-com:office:word}borderbottom>,<{urn:schemas-microsoft-com:office:word}borderleft>,<{urn:schemas-microsoft-com:office:word}borderright>,<{urn:schemas-microsoft-com:office:word}bordertop>,<{urn:schemas-microsoft-com:office:office}bottom>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}br>,<{urn:schemas-microsoft-com:office:office}callout>...............

Here is the attached document. headers are notgetting while loading the document above warning is displying, for empty document headers is getting no warning is getting, can u plz help me with this.

Re: Merging the documents may not getting the headers

PostPosted: Sat Dec 17, 2016 7:20 pm
by jason
In docx4j 3.3.x that part is handled as:

Code: Select all
        /word/document.xml's rId1 is /customXml/item1.xml [org.docx4j.openpackaging.parts.DocPropsCoverPagePart] http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml containing:org.docx4j.docProps.coverPageProps.CoverPageProperties
            /customXml/item1.xml's rId1 is /customXml/itemProps1.xml [org.docx4j.openpackaging.parts.CustomXmlDataStoragePropertiesPart] http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXmlProps containing:org.docx4j.customXmlProperties.DatastoreItem


I guess you are using some old version of docx4j. LoadFromZipNG is deprecated. Use Load3 via OpcPackage.load or Docx4J facade.

Re: Merging the documents may not getting the headers

PostPosted: Mon Dec 19, 2016 6:52 pm
by Sindhu
As you said i changed the jar to new version still not getting headers.

Re: Merging the documents may not getting the headers

PostPosted: Mon Dec 19, 2016 9:31 pm
by jason
It is not at all clear what you are asking.

Maybe https://github.com/plutext/docx4j/blob/ ... reate.java helps?

Re: Merging the documents may not getting the headers

PostPosted: Mon Dec 19, 2016 9:40 pm
by Sindhu
I am using same code for headers and footers what u have provided the link, but headers and footers not getting for attached document.
can u please help me why not getting for that document.

Re: Merging the documents may not getting the headers

PostPosted: Tue Dec 20, 2016 7:48 am
by jason
Your first sectPr contains:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
        <w:sectPr w:rsidR="006143AF" w:rsidSect="00142D03">
          <w:headerReference w:type="even" r:id="rId9"/>
          <w:headerReference w:type="default" r:id="rId10"/>
          <w:footerReference w:type="even" r:id="rId11"/>
          <w:footerReference w:type="default" r:id="rId12"/>
          <w:headerReference w:type="first" r:id="rId13"/>
          <w:footerReference w:type="first" r:id="rId14"/>
          <w:pgSz w:w="11906" w:h="16838"/>
          <w:pgMar w:top="1098" w:right="707" w:bottom="851" w:left="567" w:header="142" w:footer="708" w:gutter="0"/>
          <w:cols w:space="708"/>
          <w:docGrid w:linePitch="360"/>
        </w:sectPr>
 
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


If you want the first page header/footer to be displayed, you need to:

1. add some content to the relevant part eg the part identified by rId14
2. add <w:titlePg/>

Your second sectPr:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
        <w:sectPr w:rsidR="00D36F31" w:rsidSect="003C1B5E">
          <w:headerReference w:type="default" r:id="rId15"/>
          <w:footerReference w:type="default" r:id="rId16"/>
          <w:headerReference w:type="first" r:id="rId17"/>
          <w:pgSz w:w="11906" w:h="16838"/>
          <w:pgMar w:top="1098" w:right="707" w:bottom="851" w:left="567" w:header="142" w:footer="708" w:gutter="0"/>
          <w:cols w:space="708"/>
          <w:titlePg/>
          <w:docGrid w:linePitch="360"/>
        </w:sectPr>
 
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


You have w:titlePg here, but the first page header in rId17 has no content!

I guess the issue is similar with your subsequent sectPr elements, but I haven't looked.

Feel free to post again when you've fixed the above.

Re: Merging the documents may not getting the headers

PostPosted: Fri Dec 23, 2016 1:45 am
by Sindhu
what u said is ok, but adding new header to different sections is not working , just it overwriting the header,

Is it possible to create new header for every different sections using docx4j?

if possible please let me suggest code.

Re: Merging the documents may not getting the headers

PostPosted: Fri Dec 23, 2016 7:31 am
by jason
Sindhu wrote:but adding new header to different sections is not working , just it overwriting the header,


You are doing it wrong then. Probably altering the wrong entry in sectPr?

Sindhu wrote:s it possible to create new header for every different sections using docx4j?


Of course. You can do pretty much anything with docx4j which the file format allows.

Sindhu wrote:if possible please let me suggest code.


In know you are trying to merge documents, and keep the headers. I have suggested you use MergeDocx to do that. If you want to do it yourself, that's fine, but it does get a bit tricky where "same as previous" is used etc. Helping with this is getting beyond the scope of what is practicable in this forum, sorry.

Re: Merging the documents may not getting the headers

PostPosted: Wed Dec 28, 2016 10:51 pm
by Sindhu
As u said Previous for each section there is sectpr. for different headers for different section.

Here i am doing is iterating the sections from the template and getting realtionship from createHeaderPart method.
title=text on header
Relationship relationship = createHeaderPart(template,headerPart,title);
Code: Select all
public static Relationship createHeaderPart(
                  WordprocessingMLPackage wordprocessingMLPackage, HeaderPart sourcePart,String title)
                  throws Exception {
               
                sourcePart.setJaxbElement(getHdr(wordprocessingMLPackage, sourcePart,title));
                sourcePart.setPackage(wordprocessingMLPackage);
                sourcePart.setRelationshipType(Namespaces.HEADER);
               return wordprocessingMLPackage.getMainDocumentPart()
                     .addTargetPart(sourcePart);

            }


Code: Select all
public static Hdr getHdr(WordprocessingMLPackage wordprocessingMLPackage,
                  Part sourcePart,String title) throws Exception {
               
               Hdr hdr = objectFactory.createHdr();
               List<Object> obj = new ArrayList<Object>();
               
               obj.add(makeParagraph(sourcePart, "Risk Assessment Summary",title));

               hdr.getEGBlockLevelElts().addAll(obj);
         
               return hdr;

            }


Code: Select all
public static P makeParagraph(Part part, String text,String text2) {
                
                 P p = objectFactory.createP();
                 R r = objectFactory.createR();
                 org.docx4j.wml.RPr rpr = objectFactory.createRPr();
                 org.docx4j.wml.Color color =objectFactory.createColor();
                 color.setVal("#608bd1");
                 rpr.setColor(color);
                 org.docx4j.wml.BooleanDefaultTrue b = new org.docx4j.wml.BooleanDefaultTrue();
                 b.setVal(true);   
                 rpr.setB(b);
                 HpsMeasure size = new HpsMeasure();
                 size.setVal(BigInteger.valueOf(21));
                 rpr.setSz(size);
                 rpr.setSzCs(size);
                 r.setRPr(rpr);
                 p.getContent().add(r);
                 org.docx4j.wml.Text t = objectFactory.createText();
                 r.getContent().add(t);
                 t.setValue(text);
                t.setSpace("preserve");
                 Br lineBreak1 = objectFactory.createBr();
                 lineBreak1.setType(STBrType.TEXT_WRAPPING);
                 ((R) r).getContent().add(lineBreak1);
                 org.docx4j.wml.Text t1 = objectFactory.createText();
                 r.getContent().add(t1);
                 t1.setValue(text2);
                 t1.setSpace("preserve");
                 PPr paragraphProperties = objectFactory.createPPr();
                Jc justification = objectFactory.createJc();
                justification.setVal(JcEnumeration.CENTER);
                paragraphProperties.setJc(justification);
                p.setPPr(paragraphProperties);
                 return p;
         }

from the above code.
Here for every section same relationship id is returning rId10.
so that header is overwriting for every time.
how to resolve this.
please find the attached document, thank you

Re: Merging the documents may not getting the headers

PostPosted: Thu Dec 29, 2016 7:18 pm
by jason
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
        public Relationship addTargetPart(Part targetpart) throws InvalidFormatException {
                return this.addTargetPart(targetpart, AddPartBehaviour.OVERWRITE_IF_NAME_EXISTS, null);
        }
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


so the implication is that either you keep passing the same HeaderPart, or you are passing different objects with the same name (which won't happen if those objects are read out of a docx you've simply loaded). I guess you could change the name of a part so it was the same as some other, but you shouldn't be doing that.... You'll need to debug your code.