[英]How to write mixed text (normal and subscript) in docx using apache poi?
I will try to explain what I need. 我会尝试解释我需要什么。 I have a text that contains both normal-sized and subscript text (eg oxygene O2) which I would like to write to docx file using apache poi 3.9 library.
我有一个包含正常大小和下标文本(例如oxygene O2)的文本,我想使用apache poi 3.9库写入docx文件。 I get either all text normal sized or subscript.
我得到所有文本正常大小或下标。 Is there a way to do that?
有没有办法做到这一点? Here is my code:
这是我的代码:
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.VerticalAlign;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class ApachePOI {
public static void main(String[] args) {
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("Oxygene - O");
run.setSubscript(VerticalAlign.SUBSCRIPT);
run.setText("2");
try {
FileOutputStream fos = new FileOutputStream("test.docx");
document.write(fos);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Yes, try this code - 是的,试试这个代码 -
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.VerticalAlign;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class ApachePOI {
public static void main(String[] args) {
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraphOne = document.createParagraph();
paragraphOne.setAlignment(ParagraphAlignment.CENTER);
XWPFRun paragraphOneRunOne = paragraphOne.createRun();
paragraphOneRunOne.setFontSize(25);
paragraphOneRunOne.setBold(true);
paragraphOneRunOne.setText("Oxygene - O");
XWPFRun paragraphOneRunTwo = paragraphOne.createRun();
paragraphOneRunTwo.setFontSize(17);
paragraphOneRunTwo.setBold(true);
paragraphOneRunTwo.setSubscript(VerticalAlign.SUBSCRIPT);
paragraphOneRunTwo.setText("2");
try {
FileOutputStream fos = new FileOutputStream("C://test.docx");
document.write(fos);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.