Java Code Examples for org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart#addObject()

The following examples show how to use org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart#addObject() . 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: 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 3
Source File: DocxBuilder.java    From TranskribusCore with GNU General Public License v3.0 6 votes vote down vote up
private void addParagraph(String mdName, String mdValue, MainDocumentPart mdp, String style) {
		
		if (mdValue != null && !mdValue.equals("")){
			org.docx4j.wml.P  p = mdp.createStyledParagraphOfText(style, mdName + mdValue);
			
			mdp.addObject(p);
			
//			org.docx4j.wml.Text  t = factory.createText();
//			t.setValue(mdName + mdValue);
//			t.setSpace("preserve");
//					
//			org.docx4j.wml.R  run = factory.createR();
//			p.getContent().add(run);
//			run.getContent().add(t);
//			
//		    org.docx4j.wml.PPr  pPr = factory.createPPr();
//	        p.setPPr(pPr);
//	        		
//		    org.docx4j.wml.PPrBase.PStyle pStyle = factory.createPPrBasePStyle();
//		    pPr.setPStyle(pStyle);
//		    pStyle.setVal(style);
		}
		
	}
 
Example 4
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 5
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 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: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public void createTableTest(WordprocessingMLPackage wordMLPackage,
		MainDocumentPart t, ObjectFactory factory) throws Exception {
	RPr titleRpr = getRPr(factory, "宋体", "000000", "22", STHint.EAST_ASIA,
			true, false, false, false);
	RPr contentRpr = getRPr(factory, "宋体", "000000", "22",
			STHint.EAST_ASIA, false, false, false, false);
	Tbl table = factory.createTbl();
	addBorders(table, "2");

	double[] colWidthPercent = new double[] { 15, 20, 20, 20, 25 };// 百分比
	setTableGridCol(wordMLPackage, factory, table, 100, colWidthPercent);
	List<String> columnList = new ArrayList<String>();
	columnList.add("序号");
	columnList.add("姓名信息|姓甚|名谁");
	columnList.add("名刺信息|籍贯|营生");
	addTableTitleCell(factory, wordMLPackage, table, columnList, titleRpr,
			JcEnumeration.CENTER, true, "C6D9F1");

	for (int i = 0; i < 10; i++) {
		Tr contentRow = factory.createTr();
		addTableCell(factory, wordMLPackage, contentRow, i + "",
				contentRpr, JcEnumeration.CENTER, false, null);
		addTableCell(factory, wordMLPackage, contentRow, "无名氏", contentRpr,
				JcEnumeration.CENTER, false, null);
		addTableCell(factory, wordMLPackage, contentRow, "佚名", contentRpr,
				JcEnumeration.CENTER, false, null);
		addTableCell(factory, wordMLPackage, contentRow, "武林", contentRpr,
				JcEnumeration.CENTER, false, null);
		addTableCell(factory, wordMLPackage, contentRow, "吟诗赋曲",
				contentRpr, JcEnumeration.CENTER, false, null);
		table.getContent().add(contentRow);
	}
	setTableAlign(factory, table, JcEnumeration.CENTER);
	t.addObject(table);
}
 
Example 9
Source File: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public void addPageBreak(WordprocessingMLPackage wordMLPackage,
		ObjectFactory factory, STBrType sTBrType) {
	MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
	Br breakObj = new Br();
	breakObj.setType(sTBrType);
	P paragraph = factory.createP();
	paragraph.getContent().add(breakObj);
	documentPart.addObject(paragraph);
}
 
Example 10
Source File: Docx4j_创建批注_S3_Test.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public void testCreateComment(WordprocessingMLPackage wordMLPackage,  
        MainDocumentPart t, ObjectFactory factory) throws Exception {  
    P p = factory.createP();  
    setParagraphSpacing(factory, p, true, "0",  
            "0",true, null, "100", true, "240", STLineSpacingRule.AUTO);  
    t.addObject(p);  
    RPr fontRPr = getRPrStyle(factory, "微软雅黑", "000000", "20",  
            STHint.EAST_ASIA, false, false, false, true, UnderlineEnumeration.SINGLE,  "B61CD2",  
            true, "darkYellow", false, null, null, null);  
    RPr commentRPr = getRPrStyle(factory, "微软雅黑", "41A62D", "18",  
            STHint.EAST_ASIA, true, true, false, false, null, null, false,  
            null, false, null, null, null);  
    Comments comments = addDocumentCommentsPart(wordMLPackage, factory);  
    BigInteger commentId = BigInteger.valueOf(1);  
      
    createCommentEnd(factory, p, "测试", "这是官网Demo", fontRPr, commentRPr, commentId, comments);  
    commentId = commentId.add(BigInteger.ONE);  
  
    createCommentRound(factory, p, "批注", "这是批注comment", fontRPr, commentRPr, commentId, comments);  
    commentId = commentId.add(BigInteger.ONE);  
      
    p = factory.createP();  
    setParagraphSpacing(factory, p, true, "0",  
            "0",true, null, "100", true, "240", STLineSpacingRule.AUTO);  
    t.addObject(p);  
    createCommentRound(factory, p, "批注2", "这是批注comment2", fontRPr, commentRPr, commentId, comments);  
    commentId = commentId.add(BigInteger.ONE);  
      
    createCommentEnd(factory, p, "测试2", "这是官网Demo", fontRPr, commentRPr, commentId, comments);  
    commentId = commentId.add(BigInteger.ONE);  
}
 
Example 11
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public void createNormalTableTest(WordprocessingMLPackage wordMLPackage,
		MainDocumentPart t, ObjectFactory factory) throws Exception {
	RPr titleRpr = getRPr(factory, "宋体", "000000", "22", STHint.EAST_ASIA,
			true, false, false, false);
	RPr contentRpr = getRPr(factory, "宋体", "000000", "22",
			STHint.EAST_ASIA, false, false, false, false);
	Tbl table = factory.createTbl();
	addBorders(table, "2");
	Tr titleRow = factory.createTr();
	addTableCell(factory, wordMLPackage, titleRow, "序号", titleRpr,
			JcEnumeration.CENTER, true, "C6D9F1");
	addTableCell(factory, wordMLPackage, titleRow, "姓甚", titleRpr,
			JcEnumeration.CENTER, true, "C6D9F1");
	addTableCell(factory, wordMLPackage, titleRow, "名谁", titleRpr,
			JcEnumeration.CENTER, true, "C6D9F1");
	addTableCell(factory, wordMLPackage, titleRow, "籍贯", titleRpr,
			JcEnumeration.CENTER, true, "C6D9F1");
	addTableCell(factory, wordMLPackage, titleRow, "营生", titleRpr,
			JcEnumeration.CENTER, true, "C6D9F1");
	table.getContent().add(titleRow);
	for (int i = 0; i < 10; i++) {
		Tr contentRow = factory.createTr();
		addTableCell(factory, wordMLPackage, contentRow, i + "",
				contentRpr, JcEnumeration.CENTER, false, null);
		addTableCell(factory, wordMLPackage, contentRow, "无名氏", contentRpr,
				JcEnumeration.CENTER, false, null);
		addTableCell(factory, wordMLPackage, contentRow, "佚名", contentRpr,
				JcEnumeration.CENTER, false, null);
		addTableCell(factory, wordMLPackage, contentRow, "武林", contentRpr,
				JcEnumeration.CENTER, false, null);
		addTableCell(factory, wordMLPackage, contentRow, "吟诗赋曲",
				contentRpr, JcEnumeration.CENTER, false, null);
		table.getContent().add(contentRow);
	}
	setTableAlign(factory, table, JcEnumeration.CENTER);
	t.addObject(table);
}
 
Example 12
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public void addPageBreak(WordprocessingMLPackage wordMLPackage,
		ObjectFactory factory) {
	MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
	Br breakObj = new Br();
	breakObj.setType(STBrType.PAGE);
	P paragraph = factory.createP();
	paragraph.getContent().add(breakObj);
	documentPart.addObject(paragraph);
}
 
Example 13
Source File: SampleDocumentGenerator.java    From docx4j-export-FO with Apache License 2.0 5 votes vote down vote up
static void addObject(MainDocumentPart wordDocumentPart, String template, String fontName ) throws JAXBException {
	
    HashMap substitution = new HashMap();
    substitution.put("fontname", fontName);
    Object o = XmlUtils.unmarshallFromTemplate(template, substitution);
    wordDocumentPart.addObject(o);    		    
	
}
 
Example 14
Source File: SampleDocument.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
static void addObject(MainDocumentPart wordDocumentPart, String template, String fontName ) throws JAXBException {
	
    HashMap substitution = new HashMap();
    substitution.put("fontname", fontName);
    Object o = XmlUtils.unmarshallFromTemplate(template, substitution);
    wordDocumentPart.addObject(o);    		    
	
}
 
Example 15
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
public void createNormalTableTest(WordprocessingMLPackage wordMLPackage,  
        MainDocumentPart t, ObjectFactory factory) throws Exception {  
    RPr titleRpr = getRPr(factory, "宋体", "000000", "22", STHint.EAST_ASIA,  
            true, false, false, false);  
    RPr contentRpr = getRPr(factory, "宋体", "000000", "22",  
            STHint.EAST_ASIA, false, false, false, false);  
    Tbl table = factory.createTbl();  
    addBorders(table, "2");  
      
    double[]widthPercent=new double[]{15,20,20,20,25};//百分比  
    setTableGridCol(wordMLPackage, factory, table, widthPercent);  
      
    Tr titleRow = factory.createTr();  
    setTableTrHeight(factory, titleRow, "500");  
    addTableCell(factory, wordMLPackage, titleRow, "序号", titleRpr,  
            JcEnumeration.CENTER, true, "C6D9F1");  
    addTableCell(factory, wordMLPackage, titleRow, "姓甚", titleRpr,  
            JcEnumeration.CENTER, true, "C6D9F1");  
    addTableCell(factory, wordMLPackage, titleRow, "名谁", titleRpr,  
            JcEnumeration.CENTER, true, "C6D9F1");  
    addTableCell(factory, wordMLPackage, titleRow, "籍贯", titleRpr,  
            JcEnumeration.CENTER, true, "C6D9F1");  
    addTableCell(factory, wordMLPackage, titleRow, "营生", titleRpr,  
            JcEnumeration.CENTER, true, "C6D9F1");  
    table.getContent().add(titleRow);  
      
    for (int i = 0; i < 10; i++) {  
        Tr contentRow = factory.createTr();  
        addTableCell(factory, wordMLPackage, contentRow, i + "",  
                contentRpr, JcEnumeration.CENTER, false, null);  
        addTableCell(factory, wordMLPackage, contentRow, "无名氏", contentRpr,  
                JcEnumeration.CENTER, false, null);  
        addTableCell(factory, wordMLPackage, contentRow, "佚名", contentRpr,  
                JcEnumeration.CENTER, false, null);  
        addTableCell(factory, wordMLPackage, contentRow, "武林", contentRpr,  
                JcEnumeration.CENTER, false, null);  
        addTableCell(factory, wordMLPackage, contentRow, "吟诗赋曲",  
                contentRpr, JcEnumeration.CENTER, false, null);  
        table.getContent().add(contentRow);  
    }  
    setTableAlign(factory, table, JcEnumeration.CENTER);  
    t.addObject(table);  
}
 
Example 16
Source File: DocxBuilder.java    From TranskribusCore with GNU General Public License v3.0 4 votes vote down vote up
private void exportTextRegion(TrpTextRegionType tr, boolean wordBased, P p, Tc cell, MainDocumentPart mdp) {
	
	if (p == null){
		p = factory.createP();
		mdp.addObject(p);
	}

	List<TextLineType> lines = tr.getTextLine();
	for (int i=0; i<lines.size(); ++i) {
		TrpTextLineType trpL = (TrpTextLineType) lines.get(i);
											
		try {
			if (wordBased && trpL.getWord().size()>0){
				getFormattedTextForLineElement(trpL.getWord(), p, mdp);
			}
			else {
				getFormattedTextForShapeElement(trpL, p, mdp);
				
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		/* with ¶ the user can mark a new paragraph inside a text region
		 * unicode is \u00B6
		 */
		if (trpL.getCustomTagList().containsParagraphTag()){
			//then new paragraph should be used;
			//logger.debug("paragraph sign detected ");
			p = factory.createP();
			if (cell != null){
				cell.getContent().add(p);
			}
			else{
				mdp.addObject(p);
			}
			
		}
		/*add line break after each text line
		 * or omit this if explicitely wished to have dense lines
		 * No line break at end of paragraph
		 */
		else if (preserveLineBreaks && !(i+1==lines.size()) ){
			Br br = factory.createBr(); // this Br element is used break the current and go for next line
			p.getContent().add(br);
		}
	}
	
}
 
Example 17
Source File: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
public void createNormalTableTest(WordprocessingMLPackage wordMLPackage,
		MainDocumentPart t, ObjectFactory factory) throws Exception {
	RPr titleRpr = getRPr(factory, "宋体", "000000", "22", STHint.EAST_ASIA,
			true, false, false, false);
	RPr contentRpr = getRPr(factory, "宋体", "000000", "22",
			STHint.EAST_ASIA, false, false, false, false);
	Tbl table = factory.createTbl();
	CTBorder topBorder = new CTBorder();
	topBorder.setColor("80C687");
	topBorder.setVal(STBorder.DOUBLE);
	topBorder.setSz(new BigInteger("2"));
	CTBorder leftBorder = new CTBorder();
	leftBorder.setVal(STBorder.NONE);
	leftBorder.setSz(new BigInteger("0"));

	CTBorder hBorder = new CTBorder();
	hBorder.setVal(STBorder.SINGLE);
	hBorder.setSz(new BigInteger("1"));

	addBorders(table, topBorder, topBorder, leftBorder, leftBorder,
			hBorder, null);

	double[] colWidthPercent = new double[] { 15, 20, 20, 20, 25 };// 百分比
	setTableGridCol(wordMLPackage, factory, table, 80, colWidthPercent);

	Tr titleRow = factory.createTr();
	setTableTrHeight(factory, titleRow, "500");
	addTableCell(factory, wordMLPackage, titleRow, "序号", titleRpr,
			JcEnumeration.CENTER, true, "C6D9F1");
	addTableCell(factory, wordMLPackage, titleRow, "姓甚", titleRpr,
			JcEnumeration.CENTER, true, "C6D9F1");
	addTableCell(factory, wordMLPackage, titleRow, "名谁", titleRpr,
			JcEnumeration.CENTER, true, "C6D9F1");
	addTableCell(factory, wordMLPackage, titleRow, "籍贯", titleRpr,
			JcEnumeration.CENTER, true, "C6D9F1");
	addTableCell(factory, wordMLPackage, titleRow, "营生", titleRpr,
			JcEnumeration.CENTER, true, "C6D9F1");
	table.getContent().add(titleRow);

	for (int i = 0; i < 10; i++) {
		Tr contentRow = factory.createTr();
		addTableCell(factory, wordMLPackage, contentRow, i + "",
				contentRpr, JcEnumeration.CENTER, false, null);
		addTableCell(factory, wordMLPackage, contentRow, "无名氏", contentRpr,
				JcEnumeration.CENTER, false, null);
		addTableCell(factory, wordMLPackage, contentRow, "佚名", contentRpr,
				JcEnumeration.CENTER, false, null);
		addTableCell(factory, wordMLPackage, contentRow, "武林", contentRpr,
				JcEnumeration.CENTER, false, null);
		addTableCell(factory, wordMLPackage, contentRow, "吟诗赋曲",
				contentRpr, JcEnumeration.CENTER, false, null);
		table.getContent().add(contentRow);
	}
	setTableAlign(factory, table, JcEnumeration.CENTER);
	t.addObject(table);
}
 
Example 18
Source File: DocxBuilder.java    From TranskribusCore with GNU General Public License v3.0 4 votes vote down vote up
private void writeDocxForTranscriptWithTables(MainDocumentPart mdp, TrpPageType trpPage,
			boolean wordBased, boolean preserveLineBreaks) {
		boolean rtl = false;
		
		//TrpTableRegionType is contained in the regions too
		List<TrpRegionType> regions = trpPage.getRegions();
//		Collections.sort(regions, new TrpElementReadingOrderComparator<RegionType>(true));
		TrpShapeTypeUtils.sortShapesByReadingOrderOrCoordinates(regions);
		
		for (int j=0; j<regions.size(); ++j) {
			TrpRegionType r = regions.get(j);
			
			if (r instanceof TrpTableRegionType){
				logger.debug("is table");
				TrpTableRegionType table = (TrpTableRegionType) r;
				
				int cols = table.getNCols();
				int rows = table.getNRows();
				
	        	double maxX = table.getBoundingBox().getMaxX();//PageXmlUtils.buildPolygon(table.getCoords().getPoints()).getBounds().getMaxX();
	        	double minX = table.getBoundingBox().getMinX();//PageXmlUtils.buildPolygon(table.getCoords().getPoints()).getBounds().getMinX();
	        	int tablesize = (int) (maxX - minX);
				
				List<List<TrpTableCellType>> allRowCells = new ArrayList<List<TrpTableCellType>>();
				for (int k = 0; k<rows; k++){
					allRowCells.add(table.getRowCells(k));
				}
				
	            List<HashMap<Integer, TrpTableCellType>> allRows = new ArrayList<HashMap<Integer, TrpTableCellType>>();
	            
	            HashMap<Integer, TrpTableCellType> nextRowMap = new HashMap<Integer, TrpTableCellType>();
	           
	            for (List<TrpTableCellType> rowCells : allRowCells){
	          
	            	HashMap<Integer, TrpTableCellType> currRowMap = new HashMap<Integer, TrpTableCellType>();
	            	
	            	/*
	            	 * fill up all cells which are not set in TRP (needed for vertical cell merge)
	            	 * the nextRowMap contains already all cells which span vertically with the cells above - means they got merged 
	            	 * in the table but have to be considered here 
	            	 */
    				currRowMap.putAll(nextRowMap);
    				nextRowMap.clear();
	            	
	            	for (TrpTableCellType cell : rowCells){
		            	//logger.debug("table cell text " + cell.getUnicodeTextFromLines());
		            	currRowMap.put(cell.getCol(), cell);
		            	if (cell.getRowSpan() > 1){
		            		nextRowMap.put(cell.getCol(), null);
		            	}
	            	}
	            	allRows.add(currRowMap);
	            }

	      		Tbl thisTable;
				try {
					thisTable = getDocxTable(wordMLPackage, wordBased, rows, cols, allRows, tablesize, mdp);
					mdp.addObject(thisTable);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				//Br br = factory.createBr(); // this Br element is used break the current and go for next line
				org.docx4j.wml.P  p = factory.createP();
				mdp.addObject(p);
				//p.getContent().add(br);

			}
			else if (r instanceof TrpTextRegionType){
				
				TrpTextRegionType tr = (TrpTextRegionType) r;
			
				/*
				 * create one paragraph for each text region
				 * but only if there is some text in it
				 */
				String helper = tr.getUnicodeText().replaceAll("\n", "");
				
				//logger.debug("region unicode text " + helper);
				
				if (!helper.equals("")){
					
					exportTextRegion(tr, wordBased, null, null, mdp);
		
				}
			}
		}				
	}
 
Example 19
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
public void addParagraphTest(WordprocessingMLPackage wordMLPackage,
		MainDocumentPart t, ObjectFactory factory) throws Exception {
	RPr titleRPr = getRPr(factory, "黑体", "000000", "28", STHint.EAST_ASIA,
			true, false, false, false);
	RPr boldRPr = getRPr(factory, "宋体", "000000", "22", STHint.EAST_ASIA,
			true, false, false, false);
	RPr fontRPr = getRPr(factory, "宋体", "000000", "20", STHint.EAST_ASIA,
			false, false, false, false);
	P paragraph = factory.createP();
	setParagraphAlign(factory, paragraph, JcEnumeration.CENTER);
	Text txt = factory.createText();
	txt.setValue("七年级上册Unit2 This is just a test. sectionA测试卷答题卡");
	R run = factory.createR();
	run.getContent().add(txt);
	run.setRPr(titleRPr);
	paragraph.getContent().add(run);
	t.addObject(paragraph);

	paragraph = factory.createP();
	setParagraphAlign(factory, paragraph, JcEnumeration.CENTER);
	txt = factory.createText();
	txt.setValue("班级:________    姓名:________");
	run = factory.createR();
	run.getContent().add(txt);
	run.setRPr(fontRPr);
	paragraph.getContent().add(run);
	t.addObject(paragraph);

	paragraph = factory.createP();
	txt = factory.createText();
	txt.setValue("一、单选题");
	run = factory.createR();
	run.getContent().add(txt);
	run.setRPr(boldRPr);
	paragraph.getContent().add(run);
	t.addObject(paragraph);

	paragraph = factory.createP();
	txt = factory.createText();
	txt.setValue("1.下列有关仪器用途的说法错误的是(    )");
	run = factory.createR();
	run.getContent().add(txt);
	run.setRPr(fontRPr);
	paragraph.getContent().add(run);
	t.addObject(paragraph);

	paragraph = factory.createP();
	txt = factory.createText();
	txt.setValue("A.烧杯用于较多量试剂的反应容器");
	run = factory.createR();
	run.getContent().add(txt);
	run.setRPr(fontRPr);
	paragraph.getContent().add(run);
	t.addObject(paragraph);

	paragraph = factory.createP();
	txt = factory.createText();
	txt.setValue("B.烧杯用于较多量试剂的反应容器");
	run = factory.createR();
	run.getContent().add(txt);
	run.setRPr(fontRPr);
	paragraph.getContent().add(run);
	t.addObject(paragraph);

	paragraph = factory.createP();
	txt = factory.createText();
	txt.setValue("C.烧杯用于较多量试剂的反应容器");
	run = factory.createR();
	run.getContent().add(txt);
	run.setRPr(fontRPr);
	paragraph.getContent().add(run);
	t.addObject(paragraph);

	paragraph = factory.createP();
	txt = factory.createText();
	txt.setValue("D.烧杯用于较多量试剂的反应容器");
	run = factory.createR();
	run.getContent().add(txt);
	run.setRPr(fontRPr);
	paragraph.getContent().add(run);
	t.addObject(paragraph);
}
 
Example 20
Source File: Docx4j_Helper.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
public void testDocx4jSetPageSize() throws Exception {  
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();  
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();  
  
    String titleStr="静夜思    李白";  
    String str="床前明月光,疑似地上霜。";  
    String str2="举头望明月,低头思故乡。";  
    P p = Docx4j_Helper.factory.createP();  
    String rprStr = "<w:rPr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:rFonts w:hint=\"eastAsia\" w:ascii=\"Times New Roman\" w:hAnsi=\"Times New Roman\" w:eastAsia=\"宋体\"/><w:b/><w:color w:val=\"333333\"/><w:sz w:val=\"32\"/><w:szCs w:val=\"32\"/></w:rPr>";  
    RPr rpr = (RPr) XmlUtils.unmarshalString(rprStr);  
    setParagraphContent(p, rpr,titleStr);  
    mdp.addObject(p);  
      
    p = Docx4j_Helper.factory.createP();  
    setParagraphContent(p, rpr,str);  
    mdp.addObject(p);  
      
    p = Docx4j_Helper.factory.createP();  
    PPr pPr=Docx4j_Helper.factory.createPPr();  
    //设置文字方向  
    SectPr sectPr = Docx4j_Helper.factory.createSectPr();  
    TextDirection textDirect = Docx4j_Helper.factory.createTextDirection();  
    //文字方向:垂直方向从右往左  
    textDirect.setVal("tbRl");  
    sectPr.setTextDirection(textDirect);  
    Type sectType = Docx4j_Helper.factory.createSectPrType();  
    //下一页  
    sectType.setVal("nextPage");  
    sectPr.setType(sectType);  
    //设置页面大小  
    PgSz pgSz =  Docx4j_Helper.factory.createSectPrPgSz();  
    pgSz.setW(new BigInteger("8335"));  
    pgSz.setH(new BigInteger("11850"));  
    sectPr.setPgSz(pgSz);  
    pPr.setSectPr(sectPr);  
    p.setPPr(pPr);  
    setParagraphContent(p, rpr,str2);  
    mdp.addObject(p);  
      
    p = createParagraphWithHAlign();  
    setParagraphContent(p, rpr,titleStr);  
    mdp.addObject(p);  
      
    p = createParagraphWithHAlign();  
    setParagraphContent(p, rpr,str);  
    mdp.addObject(p);  
      
    p = createParagraphWithHAlign();  
    setParagraphContent(p, rpr,str2);  
    mdp.addObject(p);  
   // Docx4j_Helper.saveWordPackage(wordMLPackage, outputfilepath);  
}