Page 1 of 1

Problem with Section Properties and Header/Footer

PostPosted: Sun Sep 09, 2012 2:01 am
by sandra
Hello,

I'm creating a docx document with a lot of text and a table at the end of the document. The pages with the text should be in "portait" format and the table in "landscape" format. The orientation thing is not my problem- it works fine. But I also have header and footer in my document and since I add the change in the oriantation they disappeared.

I add the "landscaped" table like this:

Code: Select all
public boolean add(WordTable wTable, boolean vertical){
   
    if(wTable == null) return false;
   
    if(!vertical) return add(wTable);
       
    SectPr sectionLandscape = objectFactory.createSectPr();
   
    //dieser Wert werden als referenz auf das Format benoetigt
    String rsidR = sectionLandscape.getRsidR();
   
    SectPr sectionPortrait = objectFactory.createSectPr();
    sectionPortrait.setRsidR(rsidR);
    sectionPortrait.setRsidSect(rsidR);   
   
   PgSz landscape = new PgSz();
    landscape.setOrient(STPageOrientation.LANDSCAPE);
    landscape.setH(BigInteger.valueOf(11906));
    landscape.setW(BigInteger.valueOf(16383));
   

    PgSz portrait = new PgSz();
    portrait.setOrient(STPageOrientation.PORTRAIT);
    portrait.setH(BigInteger.valueOf(16383));
    portrait.setW(BigInteger.valueOf(11906));   
   
    sectionLandscape.setPgSz(landscape);
    sectionPortrait.setPgSz(portrait);       
   
    P p1 = objectFactory.createP();
    PPr pPr = objectFactory.createPPr();
    pPr.setSectPr(sectionPortrait);   
    p1.setRsidR(rsidR);
    p1.setRsidRDefault(rsidR);
    p1.setPPr(pPr);
   
    mainDocumentPart.addObject(p1); 
    mainDocumentPart.addObject(wTable.getDocx4jTable());
    mainDocumentPart.addObject(sectionLandscape);
   
    return true;
  }


I generated this code according to the xml I saw in my test docx (which i created in the way the document should look like). Please see the attachmend for this file.

I create a P like that:
-<w:p w:rsidRDefault="00E6608B" w:rsidR="00E6608B">-<w:pPr>-<w:sectPr w:rsidR="00E6608B" w:rsidSect="00E6608B"><w:pgSz w:w="11906" w:h="16838"/><w:pgMar w:gutter="0" w:footer="708" w:header="708" w:left="1417" w:bottom="1134" w:right="1417" w:top="1417"/><w:cols w:space="708"/><w:docGrid w:linePitch="360"/></w:sectPr></w:pPr></w:p>

add the table to the document

and add the sectPr similar like this:

-<w:sectPr w:rsidR="00267409" w:rsidSect="00E6608B"><w:pgSz w:w="16838" w:h="11906" w:orient="landscape"/><w:pgMar w:gutter="0" w:footer="709" w:header="709" w:left="1134" w:bottom="1418" w:right="1418" w:top="1418"/><w:cols w:space="708"/><w:docGrid w:linePitch="360"/></w:sectPr>

But why are my header and footer gone? The footer and header worked fine before...

If there are any further documents or code you need, just let me know.

Re: Problem with Section Properties and Header/Footer

PostPosted: Sun Sep 09, 2012 2:22 am
by sandra
I made a new test document with an header and the two different orientations. Now I see the problem. The sectPr also should have references to my header and footer. How can I get my header and footer and create the references for my sectPr ?

in short: how can i create this entries in the document.xml and the entries in the document.rel.xml belonging to them:

Code: Select all
<w:headerReference w:type="even" r:id="rID8">
<w:headerReference w:type="default" r:id="rID9">
<w:headerReference w:type="first" r:id="rID10">

(same for footer)


I aleady add the header and footer to my whole document from my template file with this method:

Code: Select all
private void loadPartsFromTemplate(){
   try{
     if (log.isInfoEnabled())
         log.info("Lade Template Dokument:"+templateFile);
    
    WordprocessingMLPackage tempPkg = WordprocessingMLPackage.load(new File(templateFile));
    Styles tmpStyles = (Styles)tempPkg.getMainDocumentPart().getStyleDefinitionsPart().getJaxbElement();

    //fuege die aus dem tmp document geladenen styles dem neuen hinzu
    StyleDefinitionsPart styleDefinitionsPart = new StyleDefinitionsPart();
    styleDefinitionsPart.setPackage(wordMLPackage);
    styleDefinitionsPart.setJaxbElement(tmpStyles);
    mainDocumentPart.addTargetPart(styleDefinitionsPart);
   
    //fuege die aus dem tmp document geladenen numbering styles dem neuen hinzu 
    NumberingDefinitionsPart ndp = tempPkg.getMainDocumentPart().getNumberingDefinitionsPart();
    mainDocumentPart.addTargetPart(ndp); 
   
    //Footer aus der Vorlage kopieren
    RelationshipsPart rp = tempPkg.getMainDocumentPart().getRelationshipsPart();
    Relationship rel = rp.getRelationshipByType(Namespaces.FOOTER);
    Part footerPart = rp.getPart(rel);
    footerPart.setPartName(new PartName("/word/footer1.xml"));
    //Hinzufuegen
    Relationship rFooter = mainDocumentPart.addTargetPart(footerPart);     
    createFooterReference(wordMLPackage, rFooter);
   
    //erzeugt einen leeren Header
    //Relationship rHeader = createHeaderPart();     
    //createHeaderReference(wordMLPackage, rHeader);   
   
    //fuegt alle Header aus dem Vorlagendokument mit den enthaltenen
    //SubParts hinzu (z.b. Bilder)
    List<SectionWrapper> sectionWrappers = tempPkg.getDocumentModel().getSections();
   
   
   
    System.out.println("Sections\n");
    for (SectionWrapper sw : sectionWrappers) {
            HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();
            System.out.println("Header");
           
            if (hfp.getFirstHeader()!=null) {
              System.out.println("-first");   
              Part headerPart = hfp.getFirstHeader();               
              Relationship rh1 = mainDocumentPart.addTargetPart(headerPart);     
              createHeaderReference(wordMLPackage, rh1,1);
              addSubParts(headerPart);
            }
            if (hfp.getDefaultHeader()!=null){
              System.out.println("-default");
              Part headerPart = hfp.getDefaultHeader();               
              Relationship rh2 = mainDocumentPart.addTargetPart(headerPart);     
              createHeaderReference(wordMLPackage, rh2,3);
              addSubParts(headerPart);
            }           
    }


What should I change in my method add(WordTable wTable, boolean vertical) to add them to the sectPr also??