XWPF POI 边框左段落跟随文本位置

发布于 2025-01-10 06:16:09 字数 390 浏览 2 评论 0原文

如何更改边框左段落,但边框跟随文本位置,例如,如果文本缩进,则边框也会缩进。或者文本格式项目符号和数字,边框也将跟随第二行。

在此处输入图片说明

在此处输入图像描述

在此处输入图片描述

How to change Border Left Paragraph, but the border follow text position, example if text indent so the border will indent too. Or text format bullet and number, the border will follow second line too.

enter image description here

enter image description here

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

任性一次 2025-01-17 06:16:09

无论您在 Word GUI 的 Word 文档视图中看到段落标记 ¶,该段落都会结束并开始新的段落。因此,所有红色左边框线都是不同单段落的左边框线。

因此,您所看到的与以下完整示例相同:

import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTAbstractNum;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTNumbering;
import java.math.BigInteger; 

public class CreateWordParagraphBordersAndIndent {
    
 static String cTAbstractNumDecimalXML = 
  "<w:abstractNum xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" w:abstractNumId=\"1\">"
+ "<w:multiLevelType w:val=\"hybridMultilevel\"/>"
+ "<w:lvl w:ilvl=\"0\"><w:start w:val=\"1\"/><w:numFmt w:val=\"decimal\"/><w:lvlText w:val=\"%1\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"720\" w:hanging=\"360\"/></w:pPr></w:lvl>"
+ "<w:lvl w:ilvl=\"1\" w:tentative=\"1\"><w:start w:val=\"1\"/><w:numFmt w:val=\"decimal\"/><w:lvlText w:val=\"%1.%2\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"1440\" w:hanging=\"360\"/></w:pPr></w:lvl>"
+ "<w:lvl w:ilvl=\"2\" w:tentative=\"1\"><w:start w:val=\"1\"/><w:numFmt w:val=\"decimal\"/><w:lvlText w:val=\"%1.%2.%3\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"2160\" w:hanging=\"360\"/></w:pPr></w:lvl>"
+ "</w:abstractNum>";
    
 private static BigInteger createNumbering(XWPFDocument document, String abstractNumXML) throws Exception {
  CTNumbering cTNumbering = CTNumbering.Factory.parse(abstractNumXML);
  CTAbstractNum cTAbstractNum = cTNumbering.getAbstractNumArray(0);
  XWPFAbstractNum abstractNum = new XWPFAbstractNum(cTAbstractNum);
  XWPFNumbering numbering = document.createNumbering();
  BigInteger abstractNumID = numbering.addAbstractNum(abstractNum);
  BigInteger numID = numbering.addNum(abstractNumID);
  return numID;
 }
    
 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);
 }
 
 private static void setTabStopLeaderHYPHEN(double tabStopPosInInches, XWPFParagraph paragraph) {
  paragraph.getCTP().getPPr().addNewTabs().addNewTab();
  paragraph.getCTP().getPPr().getTabs().getTabArray(0).setVal(
   org.openxmlformats.schemas.wordprocessingml.x2006.main.STTabJc.LEFT);
  paragraph.getCTP().getPPr().getTabs().getTabArray(0).setLeader(
   org.openxmlformats.schemas.wordprocessingml.x2006.main.STTabTlc.HYPHEN);
  paragraph.getCTP().getPPr().getTabs().getTabArray(0).setPos(java.math.BigInteger.valueOf(Math.round(tabStopPosInInches * 1440))); 
 }
 
 private static void setPageSize(XWPFDocument document, double widthInInches, double heightInInches, double leftPageMarginInInches, double rightPageMarginInInches) {
  if (document.getDocument().getBody().getSectPr() == null) document.getDocument().getBody().addNewSectPr();
  if (document.getDocument().getBody().getSectPr().getPgSz() == null) document.getDocument().getBody().getSectPr().addNewPgSz();
  document.getDocument().getBody().getSectPr().getPgSz().setW(java.math.BigInteger.valueOf(Math.round(widthInInches * 1440)));
  document.getDocument().getBody().getSectPr().getPgSz().setH(java.math.BigInteger.valueOf(Math.round(heightInInches * 1440)));
  if (document.getDocument().getBody().getSectPr().getPgMar() == null) document.getDocument().getBody().getSectPr().addNewPgMar();
  document.getDocument().getBody().getSectPr().getPgMar().setLeft(java.math.BigInteger.valueOf(Math.round(leftPageMarginInInches * 1440)));
  document.getDocument().getBody().getSectPr().getPgMar().setRight(java.math.BigInteger.valueOf(Math.round(rightPageMarginInInches * 1440))); 
 }

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

  XWPFDocument document = new XWPFDocument();

  BigInteger numIDDecimalList = createNumbering(document, cTAbstractNumDecimalXML);

  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run = paragraph.createRun();  
  run.setText("The text:");
  
  paragraph = document.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.BOTH); // alingment justify
  // set tab stop at position 6.5 inches 
  // (right page margin for page size letter and 1 inch left and right page margin)
  setTabStopLeaderHYPHEN(6.5, paragraph);
  paragraph.setBorderLeft(Borders.SINGLE);
  setBorderLeftColor(paragraph, "FF0000");
  run=paragraph.createRun();  
  run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line.");
  run.addTab();
  
  paragraph = document.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.BOTH); // alingment justify
  setTabStopLeaderHYPHEN(6.5, paragraph);
  paragraph.setBorderLeft(Borders.SINGLE);
  setBorderLeftColor(paragraph, "FF0000");
  paragraph.setIndentationLeft((int)Math.round(0.5 * 1440));
  run=paragraph.createRun();  
  run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. Additional this paragraph is left indented. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. Additional this paragraph is left indented.");
  run.addTab();

  paragraph = document.createParagraph();
  paragraph.setNumID(numIDDecimalList);
  paragraph.setNumILvl(BigInteger.valueOf(0));
  paragraph.setAlignment(ParagraphAlignment.BOTH);
  setTabStopLeaderHYPHEN(6.5, paragraph);
  paragraph.setBorderLeft(Borders.SINGLE);
  setBorderLeftColor(paragraph, "FF0000");
  run=paragraph.createRun();  
  run.setText("This is a numbered paragraph which is long enough to go over multiple lines. This is a numbered paragraph which is long enough to go over multiple lines.");
  run.addTab();
  
  paragraph = document.createParagraph();
  paragraph.setNumID(numIDDecimalList);
  paragraph.setNumILvl(BigInteger.valueOf(0));
  paragraph.setAlignment(ParagraphAlignment.BOTH);
  setTabStopLeaderHYPHEN(6.5, paragraph);
  paragraph.setBorderLeft(Borders.SINGLE);
  setBorderLeftColor(paragraph, "FF0000");
  run=paragraph.createRun();  
  run.setText("This is another numbered paragraph which is long enough to go over multiple lines. This is another numbered paragraph which is long enough to go over multiple lines.");
  run.addTab();
  
  paragraph = document.createParagraph();
  run = paragraph.createRun();  
  run.setText("End of example.");
  
  // set page size letter format (8.5 x 11 inches) and 1 inch left and right page margin
  setPageSize(document, 8.5, 11d, 1d, 1d);

  FileOutputStream out = new FileOutputStream("./CreateWordParagraphBordersAndIndent.docx");
  document.write(out);
  out.close();
  document.close();
 }
}

所以任何地方都没有魔法。

图片中编号文本的出现是对文字处理的滥用。编号文本的第一行位于单独的段落中。下面的段落被设置为左缩进,以保证其正确性。但事实不应该如此。编号的文本属于一起。这就是为什么它也应该在一个段落中。那么缩进也会自动正确。人们不应通过插入不正确的段落分隔符来滥用文字处理。

Wherever you see a paragraph mark ¶ in Word document view in Word GUI a paragraph ends and a new one starts. So all your red left border lines are left border lines of different single paragraphs.

So what you see is the same as in following complete example:

import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTAbstractNum;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTNumbering;
import java.math.BigInteger; 

public class CreateWordParagraphBordersAndIndent {
    
 static String cTAbstractNumDecimalXML = 
  "<w:abstractNum xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" w:abstractNumId=\"1\">"
+ "<w:multiLevelType w:val=\"hybridMultilevel\"/>"
+ "<w:lvl w:ilvl=\"0\"><w:start w:val=\"1\"/><w:numFmt w:val=\"decimal\"/><w:lvlText w:val=\"%1\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"720\" w:hanging=\"360\"/></w:pPr></w:lvl>"
+ "<w:lvl w:ilvl=\"1\" w:tentative=\"1\"><w:start w:val=\"1\"/><w:numFmt w:val=\"decimal\"/><w:lvlText w:val=\"%1.%2\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"1440\" w:hanging=\"360\"/></w:pPr></w:lvl>"
+ "<w:lvl w:ilvl=\"2\" w:tentative=\"1\"><w:start w:val=\"1\"/><w:numFmt w:val=\"decimal\"/><w:lvlText w:val=\"%1.%2.%3\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"2160\" w:hanging=\"360\"/></w:pPr></w:lvl>"
+ "</w:abstractNum>";
    
 private static BigInteger createNumbering(XWPFDocument document, String abstractNumXML) throws Exception {
  CTNumbering cTNumbering = CTNumbering.Factory.parse(abstractNumXML);
  CTAbstractNum cTAbstractNum = cTNumbering.getAbstractNumArray(0);
  XWPFAbstractNum abstractNum = new XWPFAbstractNum(cTAbstractNum);
  XWPFNumbering numbering = document.createNumbering();
  BigInteger abstractNumID = numbering.addAbstractNum(abstractNum);
  BigInteger numID = numbering.addNum(abstractNumID);
  return numID;
 }
    
 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);
 }
 
 private static void setTabStopLeaderHYPHEN(double tabStopPosInInches, XWPFParagraph paragraph) {
  paragraph.getCTP().getPPr().addNewTabs().addNewTab();
  paragraph.getCTP().getPPr().getTabs().getTabArray(0).setVal(
   org.openxmlformats.schemas.wordprocessingml.x2006.main.STTabJc.LEFT);
  paragraph.getCTP().getPPr().getTabs().getTabArray(0).setLeader(
   org.openxmlformats.schemas.wordprocessingml.x2006.main.STTabTlc.HYPHEN);
  paragraph.getCTP().getPPr().getTabs().getTabArray(0).setPos(java.math.BigInteger.valueOf(Math.round(tabStopPosInInches * 1440))); 
 }
 
 private static void setPageSize(XWPFDocument document, double widthInInches, double heightInInches, double leftPageMarginInInches, double rightPageMarginInInches) {
  if (document.getDocument().getBody().getSectPr() == null) document.getDocument().getBody().addNewSectPr();
  if (document.getDocument().getBody().getSectPr().getPgSz() == null) document.getDocument().getBody().getSectPr().addNewPgSz();
  document.getDocument().getBody().getSectPr().getPgSz().setW(java.math.BigInteger.valueOf(Math.round(widthInInches * 1440)));
  document.getDocument().getBody().getSectPr().getPgSz().setH(java.math.BigInteger.valueOf(Math.round(heightInInches * 1440)));
  if (document.getDocument().getBody().getSectPr().getPgMar() == null) document.getDocument().getBody().getSectPr().addNewPgMar();
  document.getDocument().getBody().getSectPr().getPgMar().setLeft(java.math.BigInteger.valueOf(Math.round(leftPageMarginInInches * 1440)));
  document.getDocument().getBody().getSectPr().getPgMar().setRight(java.math.BigInteger.valueOf(Math.round(rightPageMarginInInches * 1440))); 
 }

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

  XWPFDocument document = new XWPFDocument();

  BigInteger numIDDecimalList = createNumbering(document, cTAbstractNumDecimalXML);

  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run = paragraph.createRun();  
  run.setText("The text:");
  
  paragraph = document.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.BOTH); // alingment justify
  // set tab stop at position 6.5 inches 
  // (right page margin for page size letter and 1 inch left and right page margin)
  setTabStopLeaderHYPHEN(6.5, paragraph);
  paragraph.setBorderLeft(Borders.SINGLE);
  setBorderLeftColor(paragraph, "FF0000");
  run=paragraph.createRun();  
  run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line.");
  run.addTab();
  
  paragraph = document.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.BOTH); // alingment justify
  setTabStopLeaderHYPHEN(6.5, paragraph);
  paragraph.setBorderLeft(Borders.SINGLE);
  setBorderLeftColor(paragraph, "FF0000");
  paragraph.setIndentationLeft((int)Math.round(0.5 * 1440));
  run=paragraph.createRun();  
  run.setText("This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. Additional this paragraph is left indented. This is justify aligned paragraph having fill characters (leaders) up to tab stop in last line. Additional this paragraph is left indented.");
  run.addTab();

  paragraph = document.createParagraph();
  paragraph.setNumID(numIDDecimalList);
  paragraph.setNumILvl(BigInteger.valueOf(0));
  paragraph.setAlignment(ParagraphAlignment.BOTH);
  setTabStopLeaderHYPHEN(6.5, paragraph);
  paragraph.setBorderLeft(Borders.SINGLE);
  setBorderLeftColor(paragraph, "FF0000");
  run=paragraph.createRun();  
  run.setText("This is a numbered paragraph which is long enough to go over multiple lines. This is a numbered paragraph which is long enough to go over multiple lines.");
  run.addTab();
  
  paragraph = document.createParagraph();
  paragraph.setNumID(numIDDecimalList);
  paragraph.setNumILvl(BigInteger.valueOf(0));
  paragraph.setAlignment(ParagraphAlignment.BOTH);
  setTabStopLeaderHYPHEN(6.5, paragraph);
  paragraph.setBorderLeft(Borders.SINGLE);
  setBorderLeftColor(paragraph, "FF0000");
  run=paragraph.createRun();  
  run.setText("This is another numbered paragraph which is long enough to go over multiple lines. This is another numbered paragraph which is long enough to go over multiple lines.");
  run.addTab();
  
  paragraph = document.createParagraph();
  run = paragraph.createRun();  
  run.setText("End of example.");
  
  // set page size letter format (8.5 x 11 inches) and 1 inch left and right page margin
  setPageSize(document, 8.5, 11d, 1d, 1d);

  FileOutputStream out = new FileOutputStream("./CreateWordParagraphBordersAndIndent.docx");
  document.write(out);
  out.close();
  document.close();
 }
}

So no magic anywhere.

The appearance of the numbered texts in your picture is a misusing of word processing. There the first line of the numbered text is in a separate paragraph. And the following paragraph is set left indented as much that it stands correct. But that should not be so. The text which is numbered belongs together. That's why it also should be in one paragraph. Then the indention also is correct automatically. One should not misusing word processing by inserting not correct paragraph breaks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文