try {
FileInputStream in = new FileInputStream("in.docx");
XWPFDocument oldDoc = new XWPFDocument(in);
XWPFDocument newDoc = new XWPFDocument();
// Copy styles from template to new doc
XWPFStyles newXStyles = newDoc.createStyles();
newXStyles.setStyles(oldDoc.getStyle());
List<XWPFParagraph> oldDocParagraphs = oldDoc.getParagraphs();
for (XWPFParagraph oldPar : oldDocParagraphs) {
// Create new paragraph and set it style of old paragraph
XWPFParagraph newPar = newDoc.createParagraph();
newPar.setStyle(oldPar.getStyle());
// Loop in runs of old paragraphs.
for (XWPFRun oldRun : oldPar.getRuns()) { // Paragraf?n sitillere g?re par?alanm?? stringleri
// Create a run for the new paragraph
XWPFRun newParRun = newPar.createRun();
// Set old run's text of old paragraph to the run of new paragraph
String runText = oldRun.text();
newParRun.setText(runText);
// Set old run's style of old paragraph to the run of new paragraph
CTRPr oldCTRPr = oldRun.getCTR().getRPr();
if (oldCTRPr != null) {
if (oldCTRPr.sizeOfRStyleArray() != 0){
String carStyle = oldRun.getStyle();
newParRun.setStyle(carStyle);
}
}
// Add the new run to the new paragraph
newPar.addRun(newParRun);
}
// Write to file and close.
FileOutputStream out = new FileOutputStream("out.docx");
newDoc.write(out);
out.close();
}
} catch (IOException | XmlException e) {
e.printStackTrace();
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…