如何更改段落边框的颜色。段落边框红色
发布于 2022-02-22 07:12:42
虽然apache poi XWPF提供使用XWPFParagraph.setBorder...方法来设置段落边框- for ex。XWPFParagraph.html#setBorderLeft -它现在不提供设置边框颜色。所以我们需要使用底层的低级别org.openxmlformats.schemas.wordprocessingml.x2006.main.*类。
段落边框在段落属性(pPr)中设置。有段落边框(pBdr)元素,具有左、上、右和底边框的设置。这些left、top、right和bottom元素具有行类型(这是apache poi已经提供的)、线条大小和线条颜色的设置。color被设置为RGB。
下面的示例提供了设置左边框颜色的方法void setBorderLeftColor(XWPFParagraph paragraph, String rgb)。
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
public class CreateWordParagraphBorderColor {
private static void setBorderLeftColor(XWPFParagraph paragraph, String rgb) {
if (paragraph.getCTP().getPPr() == null) return; // no paragraph properties = no borders
if (paragraph.getCTP().getPPr().getPBdr() == null) return; // no paragraph border
if (paragraph.getCTP().getPPr().getPBdr().getLeft() == null) return; // no left border
paragraph.getCTP().getPPr().getPBdr().getLeft().setColor(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 left and border left color:");
paragraph = document.createParagraph();
paragraph.setBorderLeft(Borders.SINGLE);
setBorderLeftColor(paragraph, "FF0000");
run = paragraph.createRun();
run.setText("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.");
paragraph = document.createParagraph();
FileOutputStream out = new FileOutputStream("CreateWordParagraphBorderColor.docx");
document.write(out);
out.close();
document.close();
}
}https://stackoverflow.com/questions/71214434
复制相似问题