如何在Apache POI中部分加粗一个段落?(Word文档)

huangapple go评论93阅读模式
英文:

How to partially bold a Paragraph in Apache POI? (Word Documents)

问题

我最近一直在使用Apache POI,并且我不能弄清楚如何设置一个像这样的字符串:“Hello World”。这是我一直在尝试的代码部分:

XWPFDocument document = new XWPFDocument();
String path = System.getProperty("user.home")+ "/Desktop/"+ array.get(0); //"array" is an ArrayList<String>
path = path.replace("\\","/");

XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
String str1 = "Price: ";
String str2 = array.get(1); // This needs to be Bold
run.setFontSize(9);
run.setFontFamily("Arial");
run.setText(str1);
run.setBold(true);
run.setText(str2);
paragraph.setSpacingBetween(1);
paragraph.setAlignment(ParagraphAlignment.RIGHT);
try {
    FileOutputStream output = new FileOutputStream(ruta);
    document.write(output);
    output.close();
    document.close();
}catch(Exception e) {
    e.printStackTrace();
}

我知道"run.setBold(true)"应该应用于整个段落,但这是我在Word文档中找到的唯一方法,所以我需要一些帮助来解决这个问题。感谢您的帮助。

英文:

I been working with Apache POI recently and i can't figure out how to set a string like this: "Hello World". This is what i been trying

    XWPFDocument document = new XWPFDocument();
	String path = System.getProperty(&quot;user.home&quot;)+ &quot;/Desktop/&quot;+ array.get(0); //&quot;array&quot; is an ArrayList&lt;String&gt;
	path = path.replace(&quot;\\&quot;,&quot;/&quot;);
			
	XWPFParagraph paragraph = document.createParagraph();
	XWPFRun run = paragraph.createRun();
	String str1 = &quot;Price: &quot;;
    String str2 = array.get(1); // This needs to be Bold
	run.setFontSize(9);
	run.setFontFamily(&quot;Arial&quot;);
	run.setText(str1);
    run.setBold(true);
    run.setText(str2);
	paragraph.setSpacingBetween(1);
	paragraph.setAlignment(ParagraphAlignment.RIGHT);
    try {
		FileOutputStream output = new FileOutputStream(ruta);
		document.write(output);
		output.close();
		document.close();
	}catch(Exception e) {
		e.printStackTrace();
	}

I know that "run.setBold(true)" its supposed to apply it to the whole parapraph but its the only thing i found for word documents so i need some help to fix this. Thanks for your help.

答案1

得分: 3

一般来说,一个"run"就是一个"run",一个段落就是一个段落。两者是不同的。你可以将"run"设置成单个词、相邻的词或整个段落。唯一重要的是:a)如果你想要"加粗"某些内容,那么b)你需要"加粗"相应的"run"。

在POI中,有点令人困惑的是,你在"段落"方面创建一个"run" 如何在Apache POI中部分加粗一个段落?(Word文档)

... 但是...

你可以在同一个段落中创建多个"run",并且它们可以具有不同的属性。

例如:

XWPFParagraph p = doc.createParagraph();
XWPFRun r1 = p.createRun();
r1.setText("Some Text");
r1.setBold(true);
r2 = p.createRun();
r2.setText("Goodbye");

我没有尝试过这段代码,但我相信"Some Text"将会是粗体,而"Goodbye"则不会。你也可以尝试不同的语法,看看哪种对你最有效。

希望这有所帮助,祝你好运!

英文:

In general, a run is a run, and a paragraph is a paragraph. Different things. You can make the run a single word, adjacent words or an entire paragraph. The only thing that matters is a) if you want to "bold" something, then b) you need to "bold" the corresponding run.

Confusing things, in POI you create a "run" in terms of a "paragraph" 如何在Apache POI中部分加粗一个段落?(Word文档)

... BUT ...

You can have multiple runs - with different attributes - in the SAME paragraph.

For example:

  XWPFParagraph p = doc.createParagraph();
  XWPFRun r1 = p.createRun();
  r1.setText(&quot;Some Text&quot;);
  r1.setBold(true);
  r2 = p.createRun();
  r2.setText(&quot;Goodbye&quot;);

I haven't tried this code, but I believe "Some Text" will be bold, and "Goodbye" won't. You can also experiment with different syntax, to see what works best for you.

I hope that helps - and good luck!

huangapple
  • 本文由 发表于 2020年7月30日 12:27:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63166189.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定