/** * Creates document header including process name * * @param doc WordprocessingMLPackage for the document. * @throws InvalidFormatException */ private void createHeader(WordprocessingMLPackage doc) throws InvalidFormatException { MainDocumentPart content = doc.getMainDocumentPart(); // Create header HeaderPart header = new HeaderPart(); Relationship rel = content.addTargetPart(header); Hdr hdr = objectFactory.createHdr(); P headerParagraph = content.createParagraphOfText(p.getTitle()); hdr.getContent().add(headerParagraph); header.setJaxbElement(hdr); setStyle(headerParagraph, "Header"); alignParagraph(headerParagraph, JcEnumeration.CENTER); // Relate to document List<SectionWrapper> sections = doc.getDocumentModel().getSections(); SectPr sectPr = sections.get(sections.size() - 1).getSectPr(); if (null == sectPr) { sectPr = objectFactory.createSectPr(); content.addObject(sectPr); sections.get(sections.size() - 1).setSectPr(sectPr); } HeaderReference headerReference = objectFactory.createHeaderReference(); headerReference.setId(rel.getId()); headerReference.setType(HdrFtrRef.DEFAULT); sectPr.getEGHdrFtrReferences().add(headerReference); }
/** * Nastaví záhlaví dokumentu. Opsáno z příkladu: * http://dev.plutext.org/trac/docx4j/browser/trunk/docx4j/src/main/java/org/docx4j/samples/HeaderFooterList.java * * @param irPackage * @throws InvalidFormatException */ private Hdr SetupHeader(WordprocessingMLPackage irPackage) throws InvalidFormatException { ObjectFactory lrFactory = Context.getWmlObjectFactory(); /* create header part */ HeaderPart lrHeaderPart = new HeaderPart(); lrHeaderPart.setPackage(irPackage); Hdr lrHeader = lrFactory.createHdr(); lrHeaderPart.setJaxbElement(lrHeader); Relationship lrRelationship = irPackage.getMainDocumentPart().addTargetPart(lrHeaderPart); /* end create header part */ /* create header reference */ List<SectionWrapper> lrSections = irPackage.getDocumentModel().getSections(); SectPr lrSectionPr = lrSections.get(lrSections.size() - 1).getSectPr(); // There is always a section wrapper, but it might not contain a sectPr if (lrSectionPr == null) { lrSectionPr = lrFactory.createSectPr(); irPackage.getMainDocumentPart().addObject(lrSectionPr); lrSections.get(lrSections.size() - 1).setSectPr(lrSectionPr); } HeaderReference lrHeaderReference = lrFactory.createHeaderReference(); lrHeaderReference.setId(lrRelationship.getId()); lrHeaderReference.setType(HdrFtrRef.DEFAULT); lrSectionPr.getEGHdrFtrReferences().add(lrHeaderReference); // add header or footer references /* end create header reference */ return lrHeader; }
private void createHeaderReference( WordprocessingMLPackage wordprocessingMLPackage, Relationship relationship) throws InvalidFormatException { 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 = factory.createSectPr(); wordprocessingMLPackage.getMainDocumentPart().addObject(sectPr); sections.get(sections.size() - 1).setSectPr(sectPr); } HeaderReference headerReference = factory.createHeaderReference(); headerReference.setId(relationship.getId()); headerReference.setType(HdrFtrRef.DEFAULT); sectPr.getEGHdrFtrReferences().add(headerReference); }