Page 1 of 1

Different header for first page than default

PostPosted: Wed Feb 06, 2013 12:58 am
by Basil
First of all, I just want to say that I have read the getting started guide and most of the relevant threads in this forum, still I cannot find what's wrong with the following code! :(

So here it goes..

Code: Select all
    public static void createHeaderPart(WordprocessingMLPackage wordprocessingMLPackage)
            throws Exception {
       
        MainDocumentPart mainDocumentPart = wordprocessingMLPackage.getMainDocumentPart();
       
        HeaderPart headerPart1 = new HeaderPart();
        headerPart1.setPackage(wordprocessingMLPackage);
        Hdr hdr1 = getHdr(true);
        headerPart1.setJaxbElement(hdr1);
        Relationship r1 = mainDocumentPart.addTargetPart(headerPart1, "rIdmy1");
//      r1.setTarget("header1.xml");
//      This one causes runtime error
       
        HeaderPart headerPart2 = new HeaderPart();
        headerPart2.setPackage(wordprocessingMLPackage);
        Hdr hdr2 = getHdr(false);
        headerPart2.setJaxbElement(hdr2);
        Relationship r2 = mainDocumentPart.addTargetPart(headerPart2, "rIdmy2");
//      r2.setTarget("header2.xml");
//      as before
       
        List<SectionWrapper> sections = wordprocessingMLPackage.getDocumentModel().getSections();
           
        SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
        // There is always a section wrapper, but it might not contain a sectPr
        if (sectPr==null ) {
            sectPr = objectFactory.createSectPr();
            wordprocessingMLPackage.getMainDocumentPart().addObject(sectPr);
            sections.get(sections.size() - 1).setSectPr(sectPr);
        }

        HeaderReference headerReference1 = new HeaderReference();
        headerReference1.setId(r1.getId());
        headerReference1.setType(HdrFtrRef.FIRST);

        HeaderReference headerReference2 = new HeaderReference();       
        headerReference2.setId(r2.getId());
        headerReference2.setType(HdrFtrRef.DEFAULT);

        sectPr.getEGHdrFtrReferences().add(headerReference1);
        sectPr.getEGHdrFtrReferences().add(headerReference2);

    }

    public static Hdr getHdr(boolean first) throws Exception {       
        String hdrXml = new String();
        String hdrXml1 = "bla bla 1... valid xml for header";
        String hdrXml2 = "bla bla 2... other valid xml for header";
       
        if (first) {
            hdrXml = hdrXml1;
        }
        else {
            hdrXml = hdrXml2;
        }

        Hdr hdr = objectFactory.createHdr();
        hdr = (Hdr)XmlUtils.unmarshalString(hdrXml);
        return hdr;
    }


The document that gets created, only contains "bla bla 2" in its header, though the purpose is that "bla bla 1" should be in the header of the first page. It seems like the code for adding header info for first page gets omitted somehow. Am I missing something obvious here??? :oops:

Any help will be greatly appreciated! :D

Re: Different header for first page than default

PostPosted: Wed Feb 06, 2013 2:00 pm
by jason

Re: Different header for first page than default

PostPosted: Wed Feb 06, 2013 7:27 pm
by Basil
Thank you Jason, that thread really helped a lot! :D

Also, I've noticed that I was using the default constructor for creating HeaderPart, so there were not really 2 distinct header parts, but only one!!! :oops: :lol:

For anybody who is interested about the same topic, the following code works...

Code: Select all
    public static void createHeaderPart(WordprocessingMLPackage wordprocessingMLPackage)
            throws Exception {
        WordprocessingMLPackage w = wordprocessingMLPackage;
               
        HeaderPart cover_hdr_part = new HeaderPart(new PartName("/word/cover-header.xml"));
        HeaderPart content_hdr_part = new HeaderPart(new PartName("/word/content-header.xml"));
        w.getParts().put(cover_hdr_part);
        w.getParts().put(content_hdr_part);
       
        Hdr cover_hdr = getHdr(true);
        Hdr content_hdr = getHdr(false);
       
        // Bind the header JAXB elements as representing their header parts
        cover_hdr_part.setJaxbElement(cover_hdr);
        content_hdr_part.setJaxbElement(content_hdr);       
       
        // Add the reference to both header parts to the Main Document Part
        Relationship cover_hdr_rel = w.getMainDocumentPart().addTargetPart(cover_hdr_part);
        Relationship content_hdr_rel = w.getMainDocumentPart().addTargetPart(content_hdr_part);
       
        // Get last section SectPr and create a new one if it doesn't exist
        List<SectionWrapper> sections = wordprocessingMLPackage.getDocumentModel().getSections();
        SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
        if (sectPr == null ) {
            sectPr = objectFactory.createSectPr();
            w.getMainDocumentPart().addObject(sectPr);
            sections.get(sections.size() - 1).setSectPr(sectPr);
        }
       
        // link cover and content headers
        HeaderReference hdr_ref; // this variable is reused

        hdr_ref = objectFactory.createHeaderReference();
        hdr_ref.setId(cover_hdr_rel.getId());
        hdr_ref.setType(HdrFtrRef.FIRST);
        sectPr.getEGHdrFtrReferences().add(hdr_ref);

        hdr_ref = objectFactory.createHeaderReference();
        hdr_ref.setId(content_hdr_rel.getId());
        hdr_ref.setType(HdrFtrRef.DEFAULT);
        sectPr.getEGHdrFtrReferences().add(hdr_ref);
       
        BooleanDefaultTrue value = new BooleanDefaultTrue();
        value.setVal(Boolean.TRUE);
        sectPr.setTitlePg(value);

    }


Happy coding! 8-)