org.docx4j.relationships.Relationship Java Examples

The following examples show how to use org.docx4j.relationships.Relationship. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 7 votes vote down vote up
public void createHeaderReference(  
        WordprocessingMLPackage wordprocessingMLPackage,  
        MainDocumentPart t, ObjectFactory factory, 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();  
        t.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);  
}
 
Example #2
Source File: AddingAFooter.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 *  First we retrieve the document sections from the package. As we want to add
 *  a footer, we get the last section and take the section properties from it.
 *  The section is always present, but it might not have properties, so we check
 *  if they exist to see if we should create them. If they need to be created,
 *  we do and add them to the main document part and the section.
 *  Then we create a reference to the footer, give it the id of the relationship,
 *  set the type to header/footer reference and add it to the collection of
 *  references to headers and footers in the section properties.
 *
 * @param relationship
 */
private static void createFooterReference(Relationship relationship) {
    List<SectionWrapper> sections = wordMLPackage.getDocumentModel().getSections();
 
    SectPr sectionProperties = sections.get(sections.size() - 1).getSectPr();
    // There is always a section wrapper, but it might not contain a sectPr
    if (sectionProperties==null ) {
        sectionProperties = factory.createSectPr();
        wordMLPackage.getMainDocumentPart().addObject(sectionProperties);
        sections.get(sections.size() - 1).setSectPr(sectionProperties);
    }
 
    FooterReference footerReference = factory.createFooterReference();
    footerReference.setId(relationship.getId());
    footerReference.setType(HdrFtrRef.DEFAULT);
    sectionProperties.getEGHdrFtrReferences().add(footerReference);
}
 
Example #3
Source File: Docx4jTest.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * 创建页眉引用关系
 *
 * @param word
 * @param relationship
 * @throws InvalidFormatException
 */
public static void createHeaderReference(
        WordprocessingMLPackage word,
        Relationship relationship )
        throws InvalidFormatException {
    List<SectionWrapper> sections = word.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();
        word.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);
}
 
Example #4
Source File: AddingPageNrToFooter.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * This method fetches the document final section properties, and adds a newly
 * created footer reference to them.
 *
 * @param relationship
 */
public static void createFooterReference(Relationship relationship){
 
    List<SectionWrapper> sections =
        wordMLPackage.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();
        wordMLPackage.getMainDocumentPart().addObject(sectPr);
        sections.get(sections.size() - 1).setSectPr(sectPr);
    }
 
    FooterReference footerReference = factory.createFooterReference();
    footerReference.setId(relationship.getId());
    footerReference.setType(HdrFtrRef.DEFAULT);
    sectPr.getEGHdrFtrReferences().add(footerReference);
}
 
Example #5
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
	Docx4J_简单例子 t = new Docx4J_简单例子();
	WordprocessingMLPackage wordMLPackage = t
			.createWordprocessingMLPackage();
	MainDocumentPart mp = wordMLPackage.getMainDocumentPart();
	ObjectFactory factory = Context.getWmlObjectFactory();
	//页眉
	Relationship relationship =t.createHeaderPart(wordMLPackage, mp, factory);
	t.createHeaderReference(wordMLPackage, mp, factory, relationship);
	
	t.addParagraphTest(wordMLPackage, mp, factory);
	t.addPageBreak(wordMLPackage, factory);
	//页脚
	t.createNormalTableTest(wordMLPackage, mp, factory);
	relationship =t.createFooterPageNumPart(wordMLPackage, mp, factory);
	t.createFooterReference(wordMLPackage, mp, factory, relationship);
	
	t.saveWordPackage(wordMLPackage, new File(
			"f:/saveFile/temp/s_simple.docx"));
}
 
Example #6
Source File: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public void createFooterReference(
		WordprocessingMLPackage wordprocessingMLPackage,
		MainDocumentPart t, ObjectFactory factory, 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();
		t.addObject(sectPr);
		sections.get(sections.size() - 1).setSectPr(sectPr);
	}
	FooterReference footerReference = factory.createFooterReference();
	footerReference.setId(relationship.getId());
	footerReference.setType(HdrFtrRef.DEFAULT);
	sectPr.getEGHdrFtrReferences().add(footerReference);
}
 
Example #7
Source File: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public void createHeaderReference(
		WordprocessingMLPackage wordprocessingMLPackage,
		MainDocumentPart t, ObjectFactory factory, 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();
		t.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);
}
 
Example #8
Source File: Docx4jTest.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * 创建页脚引用关系
 *
 * @param word
 * @param relationship
 * @throws InvalidFormatException
 */
public static void createFooterReference(
        WordprocessingMLPackage word,
        Relationship relationship )
        throws InvalidFormatException {
    List<SectionWrapper> sections = word.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();
        word.getMainDocumentPart().addObject(sectPr);
        sections.get(sections.size() - 1).setSectPr(sectPr);
    }
    FooterReference footerReference = factory.createFooterReference();
    footerReference.setId(relationship.getId());
    footerReference.setType(HdrFtrRef.DEFAULT);
    sectPr.getEGHdrFtrReferences().add(footerReference);
}
 
Example #9
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public void createFooterReference(  
        WordprocessingMLPackage wordprocessingMLPackage,  
        MainDocumentPart t, ObjectFactory factory, 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();  
        t.addObject(sectPr);  
        sections.get(sections.size() - 1).setSectPr(sectPr);  
    }  
    FooterReference footerReference = factory.createFooterReference();  
    footerReference.setId(relationship.getId());  
    footerReference.setType(HdrFtrRef.DEFAULT);  
    sectPr.getEGHdrFtrReferences().add(footerReference);  
}
 
Example #10
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {  
    Docx4J_简单例子 t = new Docx4J_简单例子();  
    WordprocessingMLPackage wordMLPackage = t  
            .createWordprocessingMLPackage();  
    MainDocumentPart mp = wordMLPackage.getMainDocumentPart();  
    ObjectFactory factory = Context.getWmlObjectFactory();  
    //图片页眉  
    //Relationship relationship =t.createHeaderPart(wordMLPackage, mp, factory);  
    //文字页眉  
    Relationship relationship =t.createTextHeaderPart(wordMLPackage, mp, factory, "我是页眉,多创造,少抄袭", JcEnumeration.CENTER);  
    t.createHeaderReference(wordMLPackage, mp, factory, relationship);  
      
    t.addParagraphTest(wordMLPackage, mp, factory);  
    t.addPageBreak(wordMLPackage, factory);  
      
    t.createNormalTableTest(wordMLPackage, mp, factory);  
    //页脚  
    relationship =t.createFooterPageNumPart(wordMLPackage, mp, factory);  
    t.createFooterReference(wordMLPackage, mp, factory, relationship);  
      
    t.saveWordPackage(wordMLPackage, new File(  
            "f:/saveFile/temp/s5_simple.docx"));  
}
 
Example #11
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public void createHeaderReference(
		WordprocessingMLPackage wordprocessingMLPackage,
		MainDocumentPart t, ObjectFactory factory, 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();
		t.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);
}
 
Example #12
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public void createFooterReference(
		WordprocessingMLPackage wordprocessingMLPackage,
		MainDocumentPart t, ObjectFactory factory, 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();
		t.addObject(sectPr);
		sections.get(sections.size() - 1).setSectPr(sectPr);
	}
	FooterReference footerReference = factory.createFooterReference();
	footerReference.setId(relationship.getId());
	footerReference.setType(HdrFtrRef.DEFAULT);
	sectPr.getEGHdrFtrReferences().add(footerReference);
}
 
Example #13
Source File: Docx4jUtils.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
private void insertDocx(MainDocumentPart main, byte[] bytes, int chunkId) {  
    try {  
        AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/part" + chunkId + ".docx"));  
        // afiPart.setContentType(new ContentType(CONTENT_TYPE));
        afiPart.setContentType(new ContentType(ContentTypes.APPLICATION_XML));
        afiPart.setBinaryData(bytes);  
        Relationship altChunkRel = main.addTargetPart(afiPart);  
  
        CTAltChunk chunk = Context.getWmlObjectFactory().createCTAltChunk();  
        chunk.setId(altChunkRel.getId());  
  
        main.addObject(chunk);  
    } catch (Exception e) {  
        e.printStackTrace();  
    }  
}
 
Example #14
Source File: CoordinatesWalker.java    From docx-stamper with MIT License 6 votes vote down vote up
public void walk() {

        RelationshipsPart relationshipsPart = document.getMainDocumentPart().getRelationshipsPart();

        // walk through elements in headers
        List<Relationship> headerRelationships = getRelationshipsOfType(document, Namespaces.HEADER);
        for (Relationship header : headerRelationships) {
            HeaderPart headerPart = (HeaderPart) relationshipsPart.getPart(header.getId());
            walkContent(headerPart.getContent());
        }

        // walk through elements in main document part
        walkContent(document.getMainDocumentPart().getContent());

        // walk through elements in headers
        List<Relationship> footerRelationships = getRelationshipsOfType(document, Namespaces.FOOTER);
        for (Relationship footer : footerRelationships) {
            FooterPart footerPart = (FooterPart) relationshipsPart.getPart(footer.getId());
            walkContent(footerPart.getContent());
        }
    }
 
Example #15
Source File: UrlVisitor.java    From yarg with Apache License 2.0 6 votes vote down vote up
@Override
public List<Object> apply(Object o) {
    if (o instanceof P.Hyperlink) {
        P.Hyperlink hyperlink = (P.Hyperlink) o;
        try {
            Relationships contents = mainDocumentPart.getRelationshipsPart().getContents();
            List<Relationship> relationships = contents.getRelationship();
            for (Relationship relationship : relationships) {
                if (relationship.getId().equals(hyperlink.getId())) {
                    relationship.setTarget(docxFormatter.handleStringWithAliases(URLDecoder.decode(relationship.getTarget(), "UTF-8")));
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("An error occurred while processing URL with aliases",e);
        }
    }
    return null;
}
 
Example #16
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public Relationship createFooterPart(  
        WordprocessingMLPackage wordprocessingMLPackage,  
        MainDocumentPart t, ObjectFactory factory) throws Exception {  
    FooterPart footerPart = new FooterPart();  
    Relationship rel = t.addTargetPart(footerPart);  
    footerPart.setJaxbElement(getFtr(wordprocessingMLPackage, factory,  
            footerPart));  
    return rel;  
}
 
Example #17
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public Relationship createFooterPageNumPart(  
        WordprocessingMLPackage wordprocessingMLPackage,  
        MainDocumentPart t, ObjectFactory factory) throws Exception {  
    FooterPart footerPart = new FooterPart();  
    footerPart.setPackage(wordprocessingMLPackage);  
    footerPart.setJaxbElement(createFooterWithPageNr(factory));  
    return t.addTargetPart(footerPart);  
}
 
Example #18
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public Relationship createTextHeaderPart(  
        WordprocessingMLPackage wordprocessingMLPackage,  
        MainDocumentPart t, ObjectFactory factory, String content,  
        JcEnumeration jcEnumeration) throws Exception {  
    HeaderPart headerPart = new HeaderPart();  
    Relationship rel = t.addTargetPart(headerPart);  
    headerPart.setJaxbElement(getTextHdr(wordprocessingMLPackage, factory,  
            headerPart, content, jcEnumeration));  
    return rel;  
}
 
Example #19
Source File: AddingPageNrToFooter.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 *  As in the previous example, this method creates a footer part and adds it to
 *  the main document and then returns the corresponding relationship.
 *
 *  @return
 *  @throws InvalidFormatException
 */
private static Relationship createFooterPart() throws InvalidFormatException {
    FooterPart footerPart = new FooterPart();
    footerPart.setPackage(wordMLPackage);
 
    footerPart.setJaxbElement(createFooterWithPageNr());
 
    return wordMLPackage.getMainDocumentPart().addTargetPart(footerPart);
}
 
Example #20
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public Relationship createTextHeaderPart(
		WordprocessingMLPackage wordprocessingMLPackage,
		MainDocumentPart t, ObjectFactory factory, String content,
		JcEnumeration jcEnumeration) throws Exception {
	HeaderPart headerPart = new HeaderPart();
	Relationship rel = t.addTargetPart(headerPart);
	headerPart.setJaxbElement(getTextHdr(wordprocessingMLPackage, factory,
			headerPart, content, jcEnumeration));
	return rel;
}
 
Example #21
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public Relationship createTextFooterPart(
		WordprocessingMLPackage wordprocessingMLPackage,
		MainDocumentPart t, ObjectFactory factory, String content,
		JcEnumeration jcEnumeration) throws Exception {
	FooterPart footerPart = new FooterPart();
	Relationship rel = t.addTargetPart(footerPart);
	footerPart.setJaxbElement(getTextFtr(wordprocessingMLPackage, factory,
			footerPart, content, jcEnumeration));
	return rel;
}
 
Example #22
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public Relationship createHeaderPart(
		WordprocessingMLPackage wordprocessingMLPackage,
		MainDocumentPart t, ObjectFactory factory) throws Exception {
	HeaderPart headerPart = new HeaderPart();
	Relationship rel = t.addTargetPart(headerPart);
	// After addTargetPart, so image can be added properly
	headerPart.setJaxbElement(getHdr(wordprocessingMLPackage, factory,
			headerPart));
	return rel;
}
 
Example #23
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public Relationship createFooterPart(
		WordprocessingMLPackage wordprocessingMLPackage,
		MainDocumentPart t, ObjectFactory factory) throws Exception {
	FooterPart footerPart = new FooterPart();
	Relationship rel = t.addTargetPart(footerPart);
	footerPart.setJaxbElement(getFtr(wordprocessingMLPackage, factory,
			footerPart));
	return rel;
}
 
Example #24
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public Relationship createFooterPageNumPart(
		WordprocessingMLPackage wordprocessingMLPackage,
		MainDocumentPart t, ObjectFactory factory) throws Exception {
	FooterPart footerPart = new FooterPart();
	footerPart.setPackage(wordprocessingMLPackage);
	footerPart.setJaxbElement(createFooterWithPageNr(factory));
	return t.addTargetPart(footerPart);
}
 
Example #25
Source File: CoordinatesWalker.java    From docx-stamper with MIT License 5 votes vote down vote up
private List<Relationship> getRelationshipsOfType(WordprocessingMLPackage document, String type) {
    List<Relationship> allRelationhips = document
            .getMainDocumentPart()
            .getRelationshipsPart()
            .getRelationships()
            .getRelationship();
    List<Relationship> headerRelationships = new ArrayList<>();
    for (Relationship r : allRelationhips) {
        if (r.getType().equals(type)) {
            headerRelationships.add(r);
        }
    }
    return headerRelationships;
}
 
Example #26
Source File: AbstractInliner.java    From yarg with Apache License 2.0 5 votes vote down vote up
private void putImage(WorksheetPart worksheetPart, SpreadsheetMLPackage pkg, BinaryPartAbstractImage imagePart, CTOneCellAnchor anchor) throws Docx4JException {
    PartName drawingPart = new PartName(StringUtils.replaceIgnoreCase(worksheetPart.getPartName().getName(),
            "worksheets/sheet", "drawings/drawing"));
    String imagePartName = imagePart.getPartName().getName();
    Part part = pkg.getParts().get(drawingPart);
    if (part != null && !(part instanceof Drawing))
        throw new ReportFormattingException("Wrong Class: not a Drawing");
    Drawing drawing = (Drawing) part;
    int currentId = 0;
    if (drawing == null) {
        drawing = new Drawing(drawingPart);
        drawing.setContents(new org.docx4j.dml.spreadsheetdrawing.CTDrawing());
        Relationship relationship = worksheetPart.addTargetPart(drawing);
        org.xlsx4j.sml.CTDrawing smlDrawing = new org.xlsx4j.sml.CTDrawing();
        smlDrawing.setId(relationship.getId());
        smlDrawing.setParent(worksheetPart.getContents());
        worksheetPart.getContents().setDrawing(smlDrawing);
    } else {
        currentId = drawing.getContents().getEGAnchor().size();
    }

    CTPicture picture = new CTPicture();

    CTBlipFillProperties blipFillProperties = new CTBlipFillProperties();
    blipFillProperties.setStretch(new CTStretchInfoProperties());
    blipFillProperties.getStretch().setFillRect(new CTRelativeRect());
    blipFillProperties.setBlip(new CTBlip());
    blipFillProperties.getBlip().setEmbed("rId" + (currentId + 1));
    blipFillProperties.getBlip().setCstate(STBlipCompression.PRINT);

    picture.setBlipFill(blipFillProperties);

    CTNonVisualDrawingProps nonVisualDrawingProps = new CTNonVisualDrawingProps();
    nonVisualDrawingProps.setId(currentId + 2);
    nonVisualDrawingProps.setName(imagePartName.substring(imagePartName.lastIndexOf("/") + 1));
    nonVisualDrawingProps.setDescr(nonVisualDrawingProps.getName());

    CTNonVisualPictureProperties nonVisualPictureProperties = new CTNonVisualPictureProperties();
    nonVisualPictureProperties.setPicLocks(new CTPictureLocking());
    nonVisualPictureProperties.getPicLocks().setNoChangeAspect(true);
    CTPictureNonVisual nonVisualPicture = new CTPictureNonVisual();

    nonVisualPicture.setCNvPr(nonVisualDrawingProps);
    nonVisualPicture.setCNvPicPr(nonVisualPictureProperties);

    picture.setNvPicPr(nonVisualPicture);

    CTShapeProperties shapeProperties = new CTShapeProperties();
    CTTransform2D transform2D = new CTTransform2D();
    transform2D.setOff(new CTPoint2D());
    transform2D.setExt(new CTPositiveSize2D());
    shapeProperties.setXfrm(transform2D);
    shapeProperties.setPrstGeom(new CTPresetGeometry2D());
    shapeProperties.getPrstGeom().setPrst(STShapeType.RECT);
    shapeProperties.getPrstGeom().setAvLst(new CTGeomGuideList());

    picture.setSpPr(shapeProperties);

    anchor.setPic(picture);
    anchor.setClientData(new CTAnchorClientData());

    drawing.getContents().getEGAnchor().add(anchor);

    Relationship rel = new Relationship();
    rel.setId("rId" + (currentId + 1));
    rel.setType(Namespaces.IMAGE);
    rel.setTarget(imagePartName);

    drawing.getRelationshipsPart().addRelationship(rel);
    RelationshipsPart relPart = drawing.getRelationshipsPart();
    pkg.getParts().remove(relPart.getPartName());
    pkg.getParts().put(relPart);
    pkg.getParts().remove(drawing.getPartName());
    pkg.getParts().put(drawing);
}
 
Example #27
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public Relationship createTextFooterPart(  
        WordprocessingMLPackage wordprocessingMLPackage,  
        MainDocumentPart t, ObjectFactory factory, String content,  
        JcEnumeration jcEnumeration) throws Exception {  
    FooterPart footerPart = new FooterPart();  
    Relationship rel = t.addTargetPart(footerPart);  
    footerPart.setJaxbElement(getTextFtr(wordprocessingMLPackage, factory,  
            footerPart, content, jcEnumeration));  
    return rel;  
}
 
Example #28
Source File: WatermarkExcelPicture.java    From kbase-doc with Apache License 2.0 5 votes vote down vote up
/**
 * 使用水印图片作为excel背景,达到水印效果<但打印时不会生效>
 * @param pkg
 * @param worksheet
 * @throws Exception
 * @throws IOException
 */
private void createBgPic(SpreadsheetMLPackage pkg, WorksheetPart worksheet) throws Exception, IOException {
	CTSheetBackgroundPicture ctSheetBackgroundPicture = org.xlsx4j.jaxb.Context.getsmlObjectFactory().createCTSheetBackgroundPicture();
	worksheet.getContents().setPicture(ctSheetBackgroundPicture);
	
	BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(pkg, worksheet, FileUtils.readFileToByteArray(new File(outMarkPic)));
	Relationship sourceRelationship = imagePart.getSourceRelationships().get(0);
	String imageRelID = sourceRelationship.getId();
	ctSheetBackgroundPicture.setId(imageRelID);
}
 
Example #29
Source File: Docx4j_替换模板.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/** 
 * 替换图表数据 
 */  
private void replacePieChartData(WordprocessingMLPackage wordMLPackage, String[] chartArr) throws Docx4JException {  
    RelationshipsPart rp = wordMLPackage.getMainDocumentPart().getRelationshipsPart();  
    Relationship rel = rp.getRelationshipByType(Namespaces.SPREADSHEETML_CHART);  
    Chart chart = (Chart) rp.getPart(rel);  
    CTChartSpace chartSpace = chart.getContents();  
    List<Object> charObjList = chartSpace.getChart().getPlotArea().getAreaChartOrArea3DChartOrLineChart();  
    CTPieChart pieChart = (CTPieChart) charObjList.get(0);  
    List<CTPieSer> serList = pieChart.getSer();  
    CTNumDataSource serVal = serList.get(0).getVal();  
    List<CTNumVal> ptList = serVal.getNumRef().getNumCache().getPt();  
    ptList.get(0).setV(chartArr[0]);  
    ptList.get(1).setV(chartArr[1]);  
}
 
Example #30
Source File: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
	Docx4J_例子2 t = new Docx4J_例子2();
	WordprocessingMLPackage wordMLPackage = t
			.createWordprocessingMLPackage();
	MainDocumentPart mp = wordMLPackage.getMainDocumentPart();
	ObjectFactory factory = Context.getWmlObjectFactory();

	Relationship relationship = t.createHeaderPart(wordMLPackage, mp,
			factory, false, "3");
	relationship = t.createTextHeaderPart(wordMLPackage, mp, factory,
			"我是页眉,独乐乐不如众乐乐", true, "3", JcEnumeration.CENTER);
	t.addParagraphTest(wordMLPackage, mp, factory);
	t.addPageBreak(wordMLPackage, factory, STBrType.PAGE);
	t.createHeaderReference(wordMLPackage, mp, factory, relationship);
	t.createNormalTableTest(wordMLPackage, mp, factory);
	t.addPageBreak(wordMLPackage, factory, STBrType.TEXT_WRAPPING);
	t.createTableTest(wordMLPackage, mp, factory);
	t.addPageBreak(wordMLPackage, factory, STBrType.TEXT_WRAPPING);
	P paragraph=factory.createP();
	CTBorder topBorder=new CTBorder() ;
	topBorder.setSpace(new BigInteger("1"));
	topBorder.setSz(new BigInteger("2"));
	topBorder.setVal(STBorder.WAVE);
	t.createParagraghLine(wordMLPackage, mp, factory, paragraph, topBorder, topBorder, topBorder, topBorder);
	mp.addObject(paragraph);
	t.createHyperlink(wordMLPackage, mp, factory,paragraph,
			"mailto:[email protected]?subject=docx4j测试", "联系我","微软雅黑","24",JcEnumeration.CENTER);
	
	// 页脚
	// relationship = t.createFooterPart(wordMLPackage, mp, factory,
	// false,"3");
	// relationship = t.createTextFooterPart(wordMLPackage, mp,
	// factory,"我是页脚", true, "3", JcEnumeration.CENTER);
	relationship = t.createFooterPageNumPart(wordMLPackage, mp, factory,
			false, "3", JcEnumeration.CENTER);
	t.createFooterReference(wordMLPackage, mp, factory, relationship);
	t.saveWordPackage(wordMLPackage, new File(
			"f:/saveFile/temp/s7_simple.docx"));
}