org.docx4j.openpackaging.parts.Part Java Examples

The following examples show how to use org.docx4j.openpackaging.parts.Part. 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 10 votes vote down vote up
public Ftr getFtr(WordprocessingMLPackage wordprocessingMLPackage,
		ObjectFactory factory, Part sourcePart, boolean isUnderLine,
		String underLineSz) throws Exception {
	Ftr ftr = factory.createFtr();
	File file = new File("f:/saveFile/tmp/xxt.jpg");
	java.io.InputStream is = new java.io.FileInputStream(file);
	ftr.getContent().add(
			newImage(wordprocessingMLPackage, factory, sourcePart,
					BufferUtil.getBytesFromInputStream(is), "filename",
					"这是页脚", 1, 2, isUnderLine, underLineSz,
					JcEnumeration.CENTER));
	if (isUnderLine) {
		ftr.getContent().add(
				createHeaderBlankP(wordprocessingMLPackage, factory,
						underLineSz, JcEnumeration.CENTER));
	}
	return ftr;
}
 
Example #2
Source File: Docx4jTest.java    From docx4j-template with Apache License 2.0 7 votes vote down vote up
/**
 * 创建包含图片的内容
 *
 * @param word
 * @param sourcePart
 * @param imageFilePath
 * @return
 * @throws Exception
 */
public static P newImage(WordprocessingMLPackage word,
                         Part sourcePart,
                         String imageFilePath) throws Exception {
    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage
            .createImagePart(word, sourcePart, new File(imageFilePath));
    //随机数ID
    int id = (int) (Math.random() * 10000);
    //这里的id不重复即可
    Inline inline = imagePart.createImageInline("image", "image", id, id * 2, false);

    Drawing drawing = factory.createDrawing();
    drawing.getAnchorOrInline().add(inline);

    R r = factory.createR();
    r.getContent().add(drawing);

    P p = factory.createP();
    p.getContent().add(r);

    return p;
}
 
Example #3
Source File: Docx4j_工具类_S3_Test.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public int getComments(WordprocessingMLPackage wordMLPackage) throws Exception{
	 Parts parts = wordMLPackage.getParts();  
 HashMap<PartName, Part> partMap = parts.getParts();  
 CommentsPart commentPart = (CommentsPart) partMap.get(new CommentsPart().getPartName());  
 Comments comments = commentPart.getContents();  
 List<Comment> commentList = comments.getComment();  
 for (Comment comment : commentList) {  
     StringBuffer sb = new StringBuffer();  
     sb.append(" ID: ").append(comment.getId());  
     sb.append(" 作者:").append(comment.getAuthor());  
     sb.append(" 时间: ").append(comment.getDate().toGregorianCalendar().getTime());  
     sb.append(" 内容:").append(comment.getContent());  
     //sb.append(" 文中内容:").append(docCmtMap.get(comment.getId().toString()));  
     System.out.println(sb.toString());  
 }  
 return 0;
}
 
Example #4
Source File: AbstractInliner.java    From yarg with Apache License 2.0 6 votes vote down vote up
protected Part resolveTextPartForDOCX(Text text, WordprocessingMLPackage wordPackage) {
    java.util.List<SectionWrapper> sectionWrappers = wordPackage.getDocumentModel().getSections();
    for (SectionWrapper sw : sectionWrappers) {
        HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();
        List<Part> parts = Arrays.asList(hfp.getFirstHeader(), hfp.getDefaultHeader(), hfp.getEvenHeader(),
                hfp.getFirstFooter(), hfp.getDefaultFooter(), hfp.getEvenFooter());
        for (Part part : parts) {
            TextMatchCallback callback = new TextMatchCallback(text);
            new TraversalUtil(part, callback);
            if (callback.matched) {
                return part;
            }
        }
    }
    return wordPackage.getMainDocumentPart();
}
 
Example #5
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public Ftr getTextFtr(WordprocessingMLPackage wordprocessingMLPackage,
		ObjectFactory factory, Part sourcePart, String content,
		JcEnumeration jcEnumeration) throws Exception {
	Ftr ftr = factory.createFtr();
	P footerP = factory.createP();
	Text text = factory.createText();
	text.setValue(content);
	R run = factory.createR();
	run.getContent().add(text);
	footerP.getContent().add(run);

	PPr pPr = footerP.getPPr();
	if (pPr == null) {
		pPr = factory.createPPr();
	}
	Jc jc = pPr.getJc();
	if (jc == null) {
		jc = new Jc();
	}
	jc.setVal(jcEnumeration);
	pPr.setJc(jc);
	footerP.setPPr(pPr);
	ftr.getContent().add(footerP);
	return ftr;
}
 
Example #6
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public Hdr getTextHdr(WordprocessingMLPackage wordprocessingMLPackage,
		ObjectFactory factory, Part sourcePart, String content,
		JcEnumeration jcEnumeration) throws Exception {
	Hdr hdr = factory.createHdr();
	P headP = factory.createP();
	Text text = factory.createText();
	text.setValue(content);
	R run = factory.createR();
	run.getContent().add(text);
	headP.getContent().add(run);

	PPr pPr = headP.getPPr();
	if (pPr == null) {
		pPr = factory.createPPr();
	}
	Jc jc = pPr.getJc();
	if (jc == null) {
		jc = new Jc();
	}
	jc.setVal(jcEnumeration);
	pPr.setJc(jc);
	headP.setPPr(pPr);
	hdr.getContent().add(headP);
	return hdr;
}
 
Example #7
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public Ftr getTextFtr(WordprocessingMLPackage wordprocessingMLPackage,  
        ObjectFactory factory, Part sourcePart, String content,  
        JcEnumeration jcEnumeration) throws Exception {  
    Ftr ftr = factory.createFtr();  
    P footerP = factory.createP();  
    Text text = factory.createText();  
    text.setValue(content);  
    R run = factory.createR();  
    run.getContent().add(text);  
    footerP.getContent().add(run);  
  
    PPr pPr = footerP.getPPr();  
    if (pPr == null) {  
        pPr = factory.createPPr();  
    }  
    Jc jc = pPr.getJc();  
    if (jc == null) {  
        jc = new Jc();  
    }  
    jc.setVal(jcEnumeration);  
    pPr.setJc(jc);  
    footerP.setPPr(pPr);  
    ftr.getContent().add(footerP);  
    return ftr;  
}
 
Example #8
Source File: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public Hdr getHdr(WordprocessingMLPackage wordprocessingMLPackage,
		ObjectFactory factory, Part sourcePart, boolean isUnderLine,
		String underLineSize) throws Exception {
	Hdr hdr = factory.createHdr();
	File file = new File("f:/saveFile/tmp/xxt.jpg");
	java.io.InputStream is = new java.io.FileInputStream(file);
	hdr.getContent().add(
			newImage(wordprocessingMLPackage, factory, sourcePart,
					BufferUtil.getBytesFromInputStream(is), "filename",
					"这是页眉部分", 1, 2, isUnderLine, underLineSize,
					JcEnumeration.CENTER));
	if (isUnderLine) {
		hdr.getContent().add(
				createHeaderBlankP(wordprocessingMLPackage, factory,
						underLineSize, JcEnumeration.CENTER));
	}
	return hdr;
}
 
Example #9
Source File: Docx4j_SaveDocxImg_S3_Test.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/** 
 * @Description: 提取word图片 
 */  
public void saveDocxImg(String filePath, String savePath) throws Exception {  
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage  
            .load(new File(filePath));  
    for (Entry<PartName, Part> entry : wordMLPackage.getParts().getParts()  
            .entrySet()) {  
        if (entry.getValue() instanceof BinaryPartAbstractImage) {  
            BinaryPartAbstractImage binImg = (BinaryPartAbstractImage) entry  
                    .getValue();  
            // 图片minetype  
            String imgContentType = binImg.getContentType();  
            PartName pt = binImg.getPartName();  
            String fileName = null;  
            if (pt.getName().indexOf("word/media/") != -1) {  
                fileName = pt.getName().substring(  
                        pt.getName().indexOf("word/media/")  
                                + "word/media/".length());  
            }  
            System.out.println(String.format("mimetype=%s,filePath=%s",  
                    imgContentType, pt.getName()));  
            FileOutputStream fos = new FileOutputStream(savePath + fileName);  
            ((BinaryPart) entry.getValue()).writeDataToOutputStream(fos);  
            fos.close();  
        }  
    }  
}
 
Example #10
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public Hdr getTextHdr(WordprocessingMLPackage wordprocessingMLPackage,  
        ObjectFactory factory, Part sourcePart, String content,  
        JcEnumeration jcEnumeration) throws Exception {  
    Hdr hdr = factory.createHdr();  
    P headP = factory.createP();  
    Text text = factory.createText();  
    text.setValue(content);  
    R run = factory.createR();  
    run.getContent().add(text);  
    headP.getContent().add(run);  
  
    PPr pPr = headP.getPPr();  
    if (pPr == null) {  
        pPr = factory.createPPr();  
    }  
    Jc jc = pPr.getJc();  
    if (jc == null) {  
        jc = new Jc();  
    }  
    jc.setVal(jcEnumeration);  
    pPr.setJc(jc);  
      
    PBdr pBdr=pPr.getPBdr();  
    if(pBdr==null){  
        pBdr=factory.createPPrBasePBdr();  
    }  
      
    CTBorder value=new CTBorder();  
    value.setVal(STBorder.SINGLE);  
    value.setColor("000000");  
    value.setSpace(new BigInteger("0"));  
    value.setSz(new BigInteger("3"));  
    pBdr.setBetween(value);  
    pPr.setPBdr(pBdr);  
    headP.setPPr(pPr);  
    setParagraphSpacing(factory, headP, jcEnumeration, "0", "0");  
    hdr.getContent().add(headP);  
    hdr.getContent().add(createHeaderBlankP(wordprocessingMLPackage, factory, jcEnumeration));  
    return hdr;  
}
 
Example #11
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public Hdr getHdr(WordprocessingMLPackage wordprocessingMLPackage,  
        ObjectFactory factory, Part sourcePart) throws Exception {  
    Hdr hdr = factory.createHdr();  
    File file = new File("f:/saveFile/tmp/xxt.jpg");  
    java.io.InputStream is = new java.io.FileInputStream(file);  
    hdr.getContent().add(  
            newImage(wordprocessingMLPackage, factory, sourcePart,  
                    BufferUtil.getBytesFromInputStream(is), "filename",  
                    "这是页眉部分", 1, 2, JcEnumeration.CENTER));  
    hdr.getContent().add(createHeaderBlankP(wordprocessingMLPackage, factory,JcEnumeration.CENTER));  
    return hdr;  
}
 
Example #12
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public Ftr getFtr(WordprocessingMLPackage wordprocessingMLPackage,  
        ObjectFactory factory, Part sourcePart) throws Exception {  
    Ftr ftr = factory.createFtr();  
    File file = new File("f:/saveFile/tmp/xxt.jpg");  
    java.io.InputStream is = new java.io.FileInputStream(file);  
    ftr.getContent().add(  
            newImage(wordprocessingMLPackage, factory, sourcePart,  
                    BufferUtil.getBytesFromInputStream(is), "filename",  
                    "这是页脚", 1, 2, JcEnumeration.CENTER));  
    return ftr;  
}
 
Example #13
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public P newImage(WordprocessingMLPackage wordMLPackage,  
        ObjectFactory factory, Part sourcePart, byte[] bytes,  
        String filenameHint, String altText, int id1, int id2,  
        JcEnumeration jcEnumeration) throws Exception {  
    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage  
            .createImagePart(wordMLPackage, sourcePart, bytes);  
    Inline inline = imagePart.createImageInline(filenameHint, altText, id1,  
            id2, false);  
    P p = factory.createP();  
    R run = factory.createR();  
    p.getContent().add(run);  
    Drawing drawing = factory.createDrawing();  
    run.getContent().add(drawing);  
    drawing.getAnchorOrInline().add(inline);  
    PPr pPr = p.getPPr();  
    if (pPr == null) {  
        pPr = factory.createPPr();  
    }  
    Jc jc = pPr.getJc();  
    if (jc == null) {  
        jc = new Jc();  
    }  
    jc.setVal(jcEnumeration);  
    pPr.setJc(jc);  
    p.setPPr(pPr);  
      
    PBdr pBdr=pPr.getPBdr();  
    if(pBdr==null){  
        pBdr=factory.createPPrBasePBdr();  
    }  
    CTBorder value=new CTBorder();  
    value.setVal(STBorder.SINGLE);  
    value.setColor("000000");  
    value.setSpace(new BigInteger("0"));  
    value.setSz(new BigInteger("3"));  
    pBdr.setBetween(value);  
    pPr.setPBdr(pBdr);  
    setParagraphSpacing(factory, p, jcEnumeration, "0", "0");  
    return p;  
}
 
Example #14
Source File: Docx4j_删除所有批注_S3_Test.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public void removeAllComment(String filePath, String savePath)  
        throws Exception {  
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage  
            .load(new java.io.File(filePath));  
    //清空comments.xml内容   
    Parts parts = wordMLPackage.getParts();  
    HashMap<PartName, Part> partMap = parts.getParts();  
    CommentsPart commentPart = (CommentsPart) partMap.get(new PartName(  
            "/word/comments.xml"));  
    Comments comments = commentPart.getContents();  
    List<Comment> commentList = comments.getComment();  
    for (int i = 0, len = commentList.size(); i < len; i++) {  
        commentList.remove(0);  
    }  
      
    //清空document.xml文件中批注  
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();  
    org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart  
            .getContents();  
    Body body = wmlDocumentEl.getBody();  
    CommentFinder cf = new CommentFinder();  
    new TraversalUtil(body, cf);  
    for (Child commentElement : cf.commentElements) {  
        System.out.println(commentElement.getClass().getName());  
        Object parent = commentElement.getParent();  
        List<Object> theList = ((ContentAccessor) parent).getContent();  
        boolean removeResult = remove(theList, commentElement);  
        System.out.println(removeResult);  
    }  
    wordMLPackage.save(new FileOutputStream(savePath));  
}
 
Example #15
Source File: AbstractInliner.java    From yarg with Apache License 2.0 5 votes vote down vote up
@Override
public void inlineToDocx(WordprocessingMLPackage wordPackage, Text text, Object paramValue, Matcher paramsMatcher) {
    try {
        Image image = new Image(paramValue, paramsMatcher);
        if (image.isValid()) {
            Part part = resolveTextPartForDOCX(text, wordPackage);
            BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordPackage, part, image.imageContent);
            int originalWidth = imagePart.getImageInfo().getSize().getWidthPx();
            int originalHeight = imagePart.getImageInfo().getSize().getHeightPx();

            double widthScale = (double) image.width / (double) originalWidth;
            double heightScale = (double) image.height  / (double) originalHeight;
            double actualScale = Math.min(widthScale, heightScale);

            long targetWidth = Math.round(originalWidth * actualScale);
            long targetHeight = Math.round(originalHeight * actualScale);

            Inline inline = imagePart.createImageInline("", "", docxUniqueId1++, docxUniqueId2++,
                    XlsxUtils.convertPxToEmu(targetWidth), XlsxUtils.convertPxToEmu(targetHeight), false);

            org.docx4j.wml.Drawing drawing = new org.docx4j.wml.ObjectFactory().createDrawing();
            R run = (R) text.getParent();
            run.getContent().add(drawing);
            drawing.getAnchorOrInline().add(inline);
            text.setValue("");
        }
    } catch (Exception e) {
        throw new ReportFormattingException("An error occurred while inserting bitmap to docx file", e);
    }
}
 
Example #16
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public Hdr getHdr(WordprocessingMLPackage wordprocessingMLPackage,
		ObjectFactory factory, Part sourcePart) throws Exception {
	Hdr hdr = factory.createHdr();
	File file = new File("f:/saveFile/tmp/xxt.jpg");
	java.io.InputStream is = new java.io.FileInputStream(file);
	hdr.getContent().add(
			newImage(wordprocessingMLPackage, factory, sourcePart,
					BufferUtil.getBytesFromInputStream(is), "filename",
					"这是页眉部分", 1, 2, JcEnumeration.CENTER));
	return hdr;
}
 
Example #17
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public Ftr getFtr(WordprocessingMLPackage wordprocessingMLPackage,
		ObjectFactory factory, Part sourcePart) throws Exception {
	Ftr ftr = factory.createFtr();
	File file = new File("f:/saveFile/tmp/xxt.jpg");
	java.io.InputStream is = new java.io.FileInputStream(file);
	ftr.getContent().add(
			newImage(wordprocessingMLPackage, factory, sourcePart,
					BufferUtil.getBytesFromInputStream(is), "filename",
					"这是页脚", 1, 2, JcEnumeration.CENTER));
	return ftr;
}
 
Example #18
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public P newImage(WordprocessingMLPackage wordMLPackage,
		ObjectFactory factory, Part sourcePart, byte[] bytes,
		String filenameHint, String altText, int id1, int id2,
		JcEnumeration jcEnumeration) throws Exception {
	BinaryPartAbstractImage imagePart = BinaryPartAbstractImage
			.createImagePart(wordMLPackage, sourcePart, bytes);
	Inline inline = imagePart.createImageInline(filenameHint, altText, id1,
			id2, false);
	P p = factory.createP();
	R run = factory.createR();
	p.getContent().add(run);
	Drawing drawing = factory.createDrawing();
	run.getContent().add(drawing);
	drawing.getAnchorOrInline().add(inline);
	PPr pPr = p.getPPr();
	if (pPr == null) {
		pPr = factory.createPPr();
	}
	Jc jc = pPr.getJc();
	if (jc == null) {
		jc = new Jc();
	}
	jc.setVal(jcEnumeration);
	pPr.setJc(jc);
	p.setPPr(pPr);
	return p;
}
 
Example #19
Source File: FOExporterVisitorDelegate.java    From docx4j-export-FO with Apache License 2.0 5 votes vote down vote up
protected void appendPartContent(FOConversionContext conversionContext, 
								  Document document, 
								  Element currentParent,
								  String name, Part part, List<Object> content) throws Docx4JException {
   Element	flow = document.createElementNS(XSL_FO, "static-content");    	
   	currentParent.appendChild(flow); 
   	flow.setAttribute("flow-name", name);
   	appendPartContent(conversionContext, document, part, content, flow);
}
 
Example #20
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 #21
Source File: OutputConversionHyperlinkHandler.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
@Override
public void handleHyperlink(Model hyperlinkModel, OpcPackage opcPackage, Part currentPart) throws Docx4JException {
	//do nothing
}
 
Example #22
Source File: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
public P newImage(WordprocessingMLPackage wordMLPackage,
		ObjectFactory factory, Part sourcePart, byte[] bytes,
		String filenameHint, String altText, int id1, int id2,
		boolean isUnderLine, String underLineSize,
		JcEnumeration jcEnumeration) throws Exception {
	BinaryPartAbstractImage imagePart = BinaryPartAbstractImage
			.createImagePart(wordMLPackage, sourcePart, bytes);
	Inline inline = imagePart.createImageInline(filenameHint, altText, id1,
			id2, false);
	P p = factory.createP();
	R run = factory.createR();
	p.getContent().add(run);
	Drawing drawing = factory.createDrawing();
	run.getContent().add(drawing);
	drawing.getAnchorOrInline().add(inline);
	PPr pPr = p.getPPr();
	if (pPr == null) {
		pPr = factory.createPPr();
	}
	Jc jc = pPr.getJc();
	if (jc == null) {
		jc = new Jc();
	}
	jc.setVal(jcEnumeration);
	pPr.setJc(jc);
	p.setPPr(pPr);

	if (isUnderLine) {
		PBdr pBdr = pPr.getPBdr();
		if (pBdr == null) {
			pBdr = factory.createPPrBasePBdr();
		}
		CTBorder value = new CTBorder();
		value.setVal(STBorder.SINGLE);
		value.setColor("000000");
		value.setSpace(new BigInteger("0"));
		value.setSz(new BigInteger(underLineSize));
		pBdr.setBetween(value);
		pPr.setPBdr(pBdr);
	}
	setParagraphSpacing(factory, p, jcEnumeration, true, "0", "0", null,
			null, true, "240", STLineSpacingRule.AUTO);
	return p;
}
 
Example #23
Source File: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
public Hdr getTextHdr(WordprocessingMLPackage wordprocessingMLPackage,
		ObjectFactory factory, Part sourcePart, String content,
		boolean isUnderLine, String underLineSz, JcEnumeration jcEnumeration)
		throws Exception {
	Hdr hdr = factory.createHdr();
	P headP = factory.createP();
	Text text = factory.createText();
	text.setValue(content);
	R run = factory.createR();
	run.getContent().add(text);
	headP.getContent().add(run);

	PPr pPr = headP.getPPr();
	if (pPr == null) {
		pPr = factory.createPPr();
	}
	Jc jc = pPr.getJc();
	if (jc == null) {
		jc = new Jc();
	}
	jc.setVal(jcEnumeration);
	pPr.setJc(jc);

	if (isUnderLine) {
		PBdr pBdr = pPr.getPBdr();
		if (pBdr == null) {
			pBdr = factory.createPPrBasePBdr();
		}
		CTBorder value = new CTBorder();
		value.setVal(STBorder.SINGLE);
		value.setColor("000000");
		value.setSpace(new BigInteger("0"));
		value.setSz(new BigInteger(underLineSz));
		pBdr.setBetween(value);
		pPr.setPBdr(pBdr);
		headP.setPPr(pPr);
	}
	setParagraphSpacing(factory, headP, jcEnumeration, true, "0", "0",
			null, null, true, "240", STLineSpacingRule.AUTO);
	hdr.getContent().add(headP);
	if (isUnderLine) {
		hdr.getContent().add(
				createHeaderBlankP(wordprocessingMLPackage, factory,
						underLineSz, jcEnumeration));
	}
	return hdr;
}
 
Example #24
Source File: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
public Ftr getTextFtr(WordprocessingMLPackage wordprocessingMLPackage,
		ObjectFactory factory, Part sourcePart, String content,
		boolean isUnderLine, String underLineSz, JcEnumeration jcEnumeration)
		throws Exception {
	Ftr ftr = factory.createFtr();
	P footerP = factory.createP();
	Text text = factory.createText();
	text.setValue(content);
	R run = factory.createR();
	run.getContent().add(text);
	footerP.getContent().add(run);

	PPr pPr = footerP.getPPr();
	if (pPr == null) {
		pPr = factory.createPPr();
	}
	Jc jc = pPr.getJc();
	if (jc == null) {
		jc = new Jc();
	}
	jc.setVal(jcEnumeration);
	pPr.setJc(jc);
	footerP.setPPr(pPr);
	setParagraphSpacing(factory, footerP, jcEnumeration, true, "0", "0",
			null, null, true, "240", STLineSpacingRule.AUTO);
	if (isUnderLine) {
		PBdr pBdr = pPr.getPBdr();
		if (pBdr == null) {
			pBdr = factory.createPPrBasePBdr();
		}
		CTBorder value = new CTBorder();
		value.setVal(STBorder.SINGLE);
		value.setColor("000000");
		value.setSpace(new BigInteger("0"));
		value.setSz(new BigInteger(underLineSz));
		pBdr.setBetween(value);
		pPr.setPBdr(pBdr);
		footerP.setPPr(pPr);
	}
	ftr.getContent().add(footerP);
	if (isUnderLine) {
		ftr.getContent().add(
				createHeaderBlankP(wordprocessingMLPackage, factory,
						underLineSz, jcEnumeration));
	}
	return ftr;
}