Search in sources :

Example 6 with XWPFRun

use of org.apache.poi.xwpf.usermodel.XWPFRun in project poi by apache.

the class TestDocumentProtection method testIntegration.

@Test
public void testIntegration() throws IOException {
    XWPFDocument doc1 = new XWPFDocument();
    XWPFParagraph p1 = doc1.createParagraph();
    XWPFRun r1 = p1.createRun();
    r1.setText("Lorem ipsum dolor sit amet.");
    doc1.enforceCommentsProtection();
    File tempFile = TempFile.createTempFile("documentProtectionFile", ".docx");
    FileOutputStream out = new FileOutputStream(tempFile);
    doc1.write(out);
    out.close();
    FileInputStream inputStream = new FileInputStream(tempFile);
    XWPFDocument doc2 = new XWPFDocument(inputStream);
    inputStream.close();
    assertTrue(doc2.isEnforcedCommentsProtection());
    doc2.close();
    doc1.close();
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) FileOutputStream(java.io.FileOutputStream) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) TempFile(org.apache.poi.util.TempFile) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 7 with XWPFRun

use of org.apache.poi.xwpf.usermodel.XWPFRun in project pmph by BCSquad.

the class WordHelper method insertXWPFParagraph.

public static void insertXWPFParagraph(String key, XWPFDocument doc2, XWPFTable xwpfTable) {
    List<XWPFParagraph> paragraphList = doc2.getParagraphs();
    if (paragraphList != null && paragraphList.size() > 0) {
        for (int i = 0; i < paragraphList.size(); i++) {
            List<XWPFRun> runs = paragraphList.get(i).getRuns();
            for (XWPFRun run : runs) {
                String text = run.getText(0);
                if (text != null) {
                    if (text.equals(key)) {
                        XmlCursor cursor = paragraphList.get(i + 1).getCTP().newCursor();
                        XWPFTable table = doc2.insertNewTbl(cursor);
                        XWPFTableRow tableRow = table.getRow(0);
                        for (int cellNum = 0; cellNum < 27; cellNum++) {
                            tableRow.addNewTableCell();
                        }
                        for (int rowNum = 0; rowNum < 3; rowNum++) {
                            table.createRow();
                        }
                        // 合并列(没有作用)
                        for (int rowNum = 0; rowNum < table.getRows().size(); rowNum++) {
                            for (int cellIndex = 0; cellIndex < tableRow.getTableCells().size(); cellIndex++) {
                                CTHMerge hmerge = CTHMerge.Factory.newInstance();
                                if (cellIndex == 0) {
                                    // The first merged cell is set with RESTART merge value
                                    hmerge.setVal(STMerge.RESTART);
                                } else {
                                    // Cells which join (merge) the first one, are set with CONTINUE
                                    hmerge.setVal(STMerge.CONTINUE);
                                }
                                XWPFTableCell cell = table.getRow(rowNum).getCell(cellIndex);
                                // Try getting the TcPr. Not simply setting an new one every time.
                                CTTcPr tcPr = cell.getCTTc().getTcPr();
                                if (tcPr != null) {
                                    tcPr.setHMerge(hmerge);
                                } else {
                                    // only set an new TcPr if there is not one already
                                    tcPr = CTTcPr.Factory.newInstance();
                                    tcPr.setHMerge(hmerge);
                                    cell.getCTTc().setTcPr(tcPr);
                                }
                            }
                        }
                        // 合并行
                        for (int col = 0; col < tableRow.getTableCells().size(); col++) {
                            for (int rowIndex = 0; rowIndex < table.getRows().size(); rowIndex++) {
                                XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
                                if (rowIndex == 0) {
                                    // The first merged cell is set with RESTART merge value
                                    cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
                                } else {
                                    // Cells which join (merge) the first one, are set with CONTINUE
                                    cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) CTHMerge(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHMerge) XWPFTableCell(org.apache.poi.xwpf.usermodel.XWPFTableCell) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) XWPFTable(org.apache.poi.xwpf.usermodel.XWPFTable) CTTcPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr) XmlCursor(org.apache.xmlbeans.XmlCursor) XWPFTableRow(org.apache.poi.xwpf.usermodel.XWPFTableRow)

Example 8 with XWPFRun

use of org.apache.poi.xwpf.usermodel.XWPFRun in project pmph by BCSquad.

the class WordHelper method addTable.

public XWPFDocument addTable(XWPFDocument document) {
    List<XWPFTable> tables = document.getTables();
    XWPFTable old = tables.get(19);
    XWPFTable t = document.createTable();
    XWPFTableRow row = t.getRow(0);
    XWPFTableCell cell = row.getCell(0);
    CTTc cttc = cell.getCTTc();
    CTTcPr tcpr = cttc.addNewTcPr();
    CTTblWidth tcw = tcpr.addNewTcW();
    tcw.setType(old.getRow(0).getCell(0).getCTTc().getTcPr().getTcW().getType());
    tcw.setW(old.getRow(0).getCell(0).getCTTc().getTcPr().getTcW().getW());
    if (old.getRow(0).getCell(0).getCTTc().getTcPr().getGridSpan() != null) {
        tcpr.setGridSpan(old.getRow(0).getCell(0).getCTTc().getTcPr().getGridSpan());
    }
    XWPFRun run = cell.getParagraphs().get(0).createRun();
    run.setText("Cells which join (merge) the first one, are set with CONTINUE");
    return document;
}
Also used : CTTblWidth(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth) XWPFTableCell(org.apache.poi.xwpf.usermodel.XWPFTableCell) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) XWPFTable(org.apache.poi.xwpf.usermodel.XWPFTable) CTTcPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr) XWPFTableRow(org.apache.poi.xwpf.usermodel.XWPFTableRow) CTTc(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc)

Example 9 with XWPFRun

use of org.apache.poi.xwpf.usermodel.XWPFRun in project pmph by BCSquad.

the class WordHelper method fromDeclarationEtcBOList.

/**
 * 从一个或多个专家信息对象中读取数据,转化成若干Word文档
 *
 * @param materialName
 *            教材名称
 * @param list
 *            DeclarationEtcBO实例集合
 * @param filter
 *            使用可填项的二进制选中
 * @param extensions
 *            教材扩展项
 * @return 包含文档名称和Word格式文档的键值对
 * @throws CheckedServiceException
 *             已检查的异常
 */
public HashMap<String, XWPFDocument> fromDeclarationEtcBOList(String materialName, List<DeclarationEtcBO> list, String filter, List<MaterialExtension> extensions) throws CheckedServiceException {
    InputStream is;
    XWPFDocument document;
    String path = this.getClass().getClassLoader().getResource("ResumeTemplate.docx").getPath();
    HashMap<String, XWPFDocument> map = new HashMap<>(list.size());
    for (DeclarationEtcBO bo : list) {
        try {
            is = new FileInputStream(path);
            document = new XWPFDocument(is);
        } catch (IOException ex) {
            logger.error("读取Word模板文件'ResumeTemplate.docx'时出现错误:{}", ex.getMessage());
            throw new CheckedServiceException(CheckedExceptionBusiness.MATERIAL, CheckedExceptionResult.FILE_CREATION_FAILED, "未找到模板文件");
        }
        if (StringUtil.notEmpty(materialName)) {
            List<XWPFRun> runs = document.getParagraphs().get(0).getRuns();
            runs.get(0).setText(materialName.concat("申报表"), 0);
        }
        List<XWPFParagraph> xwpfParagraphs = document.getParagraphs();
        List<XWPFTable> tables = document.getTables();
        int i = 21;
        XWPFTable old = tables.get(19);
        for (MaterialExtension extension : extensions) {
            XmlCursor cursor = xwpfParagraphs.get(i).getCTP().newCursor();
            // ---这个是关键
            XWPFParagraph xwpfParagraph = document.insertNewParagraph(cursor);
            CTPPr pPPr = xwpfParagraph.getCTP().getPPr() != null ? xwpfParagraph.getCTP().getPPr() : xwpfParagraph.getCTP().addNewPPr();
            CTSpacing pSpacing = pPPr.getSpacing() != null ? pPPr.getSpacing() : pPPr.addNewSpacing();
            pSpacing.setLine(BigInteger.valueOf(360L));
            pSpacing.setBeforeLines(BigInteger.valueOf(100L));
            XWPFRun xwpfRun = xwpfParagraph.createRun();
            xwpfRun.setText(extension.getExtensionName());
            xwpfRun.setFontSize(12);
            xwpfRun.setFontFamily("宋体");
            xwpfRun.setBold(true);
            /* 以下填充扩展项内容 */
            cursor = xwpfParagraphs.get(i + 1).getCTP().newCursor();
            XWPFTable t = document.insertNewTbl(cursor);
            XWPFTableRow row = t.getRow(0);
            XWPFTableCell cell = row.getCell(0);
            CTTc cttc = cell.getCTTc();
            CTTcPr tcpr = cttc.addNewTcPr();
            CTTblWidth tcw = tcpr.addNewTcW();
            tcw.setType(old.getRow(0).getCell(0).getCTTc().getTcPr().getTcW().getType());
            tcw.setW(old.getRow(0).getCell(0).getCTTc().getTcPr().getTcW().getW());
            if (old.getRow(0).getCell(0).getCTTc().getTcPr().getGridSpan() != null) {
                tcpr.setGridSpan(old.getRow(0).getCell(0).getCTTc().getTcPr().getGridSpan());
            }
            if (!bo.getDecExtensionVOs().isEmpty()) {
                for (DecExtensionVO decExtensionVO : bo.getDecExtensionVOs()) {
                    if (extension.getId().equals(decExtensionVO.getExtensionId())) {
                        String content = decExtensionVO.getContent();
                        if (StringUtil.notEmpty(content)) {
                            XWPFRun run = cell.getParagraphs().get(0).createRun();
                            run.setText(content);
                        }
                        break;
                    }
                }
            }
            i++;
        }
        /* 申报单位 */
        String chosenOrgName = bo.getChosenOrgName();
        if (StringUtil.notEmpty(chosenOrgName)) {
            xwpfParagraphs.get(i).createRun().setText(chosenOrgName);
        }
        String filename = generateFileName(bo);
        fillDeclarationPosition(tables.get(0), bo);
        fillDeclarationData(tables.get(1), bo);
        fillDecEduExpData(tables.get(2), bo.getDecEduExps());
        fillDecWorkExpData(tables.get(3), bo.getDecWorkExps());
        fillDecTeachExpData(tables.get(4), bo.getDecTeachExps());
        fillDecAchievementData(tables.get(5), bo.getDecAchievement());
        fillDecAcadeData(tables.get(6), bo.getDecAcades());
        fillDecLastPositionData(tables.get(7), bo.getDecLastPositions());
        fillDecNationalPlanData(tables.get(8), bo.getDecNationalPlans());
        fillDecTextbookPmphData(tables.get(9), bo.getDecTextbookPmphs());
        fillDecTextbookData(tables.get(10), bo.getDecTextbooks());
        fillDecMoocDigitalData(tables.get(11), bo.getDecMoocDigital());
        fillDecCourseConstructionData(tables.get(12), bo.getDecCourseConstructions());
        fillDecResearchData(tables.get(13), bo.getDecResearchs());
        fillDecMonographData(tables.get(14), bo.getDecMonographs());
        fillDecPublishRewardData(tables.get(15), bo.getPublishRewards());
        fillDecSciData(tables.get(16), bo.getDecScis());
        fillDecClinicalRewardData(tables.get(17), bo.getDecClinicalRewards());
        fillDecAcadeRewardData(tables.get(18), bo.getDecAcadeRewards());
        fillDecIntentionData(tables.get(19), bo.getDecIntention());
        map.put(filename, removeEmptyTables(document, filter));
        map.put(filename, document);
    }
    return map;
}
Also used : CTTblWidth(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth) XWPFTableCell(org.apache.poi.xwpf.usermodel.XWPFTableCell) HashMap(java.util.HashMap) CTSpacing(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSpacing) DeclarationEtcBO(com.bc.pmpheep.back.bo.DeclarationEtcBO) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) CTTc(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc) XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) XWPFTable(org.apache.poi.xwpf.usermodel.XWPFTable) DecExtensionVO(com.bc.pmpheep.back.vo.DecExtensionVO) MaterialExtension(com.bc.pmpheep.back.po.MaterialExtension) CheckedServiceException(com.bc.pmpheep.service.exception.CheckedServiceException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) XmlCursor(org.apache.xmlbeans.XmlCursor) XWPFTableRow(org.apache.poi.xwpf.usermodel.XWPFTableRow) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) CTPPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr) CTTcPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr)

Example 10 with XWPFRun

use of org.apache.poi.xwpf.usermodel.XWPFRun in project Gargoyle by callakrsos.

the class MSWord method addPicture.

/**
	 * 사진 추가.
	 *
	 * @param imgFile
	 * @param id
	 * @param width
	 * @param height
	 */
public void addPicture(final String imgFile, final int id, int width, int height) {
    XWPFParagraph p = doc.createParagraph();
    XWPFRun r1 = new KrXWPFRun(p.createRun());
    addPicture(imgFile, id, width, height, r1);
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun)

Aggregations

XWPFRun (org.apache.poi.xwpf.usermodel.XWPFRun)24 XWPFParagraph (org.apache.poi.xwpf.usermodel.XWPFParagraph)22 XWPFDocument (org.apache.poi.xwpf.usermodel.XWPFDocument)13 FileOutputStream (java.io.FileOutputStream)10 XWPFTable (org.apache.poi.xwpf.usermodel.XWPFTable)6 File (java.io.File)5 OutputStream (java.io.OutputStream)5 XWPFTableCell (org.apache.poi.xwpf.usermodel.XWPFTableCell)5 XWPFTableRow (org.apache.poi.xwpf.usermodel.XWPFTableRow)5 CommonUtils.nullToEmptyString (org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptyString)5 CTTcPr (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr)4 FileInputStream (java.io.FileInputStream)3 XWPFHeaderFooterPolicy (org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy)3 XmlCursor (org.apache.xmlbeans.XmlCursor)3 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 XWPFFooter (org.apache.poi.xwpf.usermodel.XWPFFooter)2 XWPFHeader (org.apache.poi.xwpf.usermodel.XWPFHeader)2 Test (org.junit.Test)2 CTTblWidth (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth)2