为XWPFParagraph设置背景颜色

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

Set background color for XWPFParagraph

问题

我想要为段落更改背景颜色,但我找不到如何操作的方法。我只能找到如何突出显示单词的方法。我希望我的文本看起来像是在下面的图片中:

图片链接

英文:

I want to change the background color for paragraph but I cannot find the way on how to do it. I could find only how to highlight words. I want my text to look like in 为XWPFParagraph设置背景颜色

答案1

得分: 1

你的截图并不太清晰。它可能展示了多种不同的内容。但是因为你在谈论“Word”段落,我怀疑它显示了一个带有边框和底纹的段落。

以下代码创建了一个包含带有边框和底纹的段落的“Word”文档。边框设置可以通过XWPFParagraph的方法实现。底纹设置直到现在为止尚未提供在那里的方法和类的底层ooxml-schemas是必需的。

import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.*;

public class CreateWordParagraphBackground {

 private static void setParagraphShading(XWPFParagraph paragraph, String rgb) {
  if (paragraph.getCTP().getPPr() == null) paragraph.getCTP().addNewPPr();
  if (paragraph.getCTP().getPPr().getShd() != null) paragraph.getCTP().getPPr().unsetShd();
  paragraph.getCTP().getPPr().addNewShd();
  paragraph.getCTP().getPPr().getShd().setVal(org.openxmlformats.schemas.wordprocessingml.x2006.main.STShd.CLEAR);
  paragraph.getCTP().getPPr().getShd().setColor("auto");
  paragraph.getCTP().getPPr().getShd().setFill(rgb);
 }

 public static void main(String[] args) throws Exception {

  XWPFDocument document = new XWPFDocument();

  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run = paragraph.createRun();  
  run.setText("Following paragraph with border and shading:");

  paragraph = document.createParagraph();  
  paragraph.setBorderLeft(Borders.SINGLE);
  paragraph.setBorderTop(Borders.SINGLE);
  paragraph.setBorderRight(Borders.SINGLE);
  paragraph.setBorderBottom(Borders.SINGLE);

  setParagraphShading(paragraph, "BFBFBF");

  run = paragraph.createRun();  
  run.setText("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, ");
  run = paragraph.createRun();
  run.addBreak(BreakType.TEXT_WRAPPING);
  run.setText("sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.");
  run.addBreak(BreakType.TEXT_WRAPPING);

  paragraph = document.createParagraph();

  FileOutputStream out = new FileOutputStream("CreateWordParagraphBackground.docx");
  document.write(out);
  out.close();
  document.close();
 }
}
英文:

Your screenshot is not really clear. It could show multiple different things. But as you are talking about Word paragraph, I suspect it shows a paragraph having a border and a shading.

Following code creates a Word document having a paragraph having having a border and a shading. The border settings can be achieved using methods of XWPFParagraph. The shading settings are not provided there until now. So methods and classes of underlying ooxml-schemas are needed.

import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
public class CreateWordParagraphBackground {
private static void setParagraphShading(XWPFParagraph paragraph, String rgb) {
if (paragraph.getCTP().getPPr() == null) paragraph.getCTP().addNewPPr();
if (paragraph.getCTP().getPPr().getShd() != null) paragraph.getCTP().getPPr().unsetShd();
paragraph.getCTP().getPPr().addNewShd();
paragraph.getCTP().getPPr().getShd().setVal(org.openxmlformats.schemas.wordprocessingml.x2006.main.STShd.CLEAR);
paragraph.getCTP().getPPr().getShd().setColor("auto");
paragraph.getCTP().getPPr().getShd().setFill(rgb);
}
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();  
run.setText("Folollowing paragraph with border and shading:");
paragraph = document.createParagraph();  
paragraph.setBorderLeft(Borders.SINGLE);
paragraph.setBorderTop(Borders.SINGLE);
paragraph.setBorderRight(Borders.SINGLE);
paragraph.setBorderBottom(Borders.SINGLE);
setParagraphShading(paragraph, "BFBFBF");
run = paragraph.createRun();  
run.setText("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, ");
run = paragraph.createRun();
run.addBreak(BreakType.TEXT_WRAPPING);
run.setText("sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.");
run.addBreak(BreakType.TEXT_WRAPPING);
paragraph = document.createParagraph();
FileOutputStream out = new FileOutputStream("CreateWordParagraphBackground.docx");
document.write(out);
out.close();
document.close();
}
}

huangapple
  • 本文由 发表于 2020年8月17日 19:41:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63450101.html
匿名

发表评论

匿名网友

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

确定