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();
}
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);
}
}
}
}
}
}
}
}
}
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;
}
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;
}
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);
}
Aggregations