简体   繁体   English

如何在 Apache POI Java 中垂直居中段落?

[英]How to center a paragraph vertically in Apache POI Java?

I am currently build a word formatter tool and have some problems with Apache POI.我目前正在构建一个字格式化工具,但在使用 Apache POI 时遇到了一些问题。

I am using org.apache.poi version 5.2.0我正在使用 org.apache.poi 版本 5.2.0

Have a look at this picture:看看这张照片:

在此处输入图像描述

I need it centered both horizontally and vertically.我需要它水平和垂直居中。

Currently it only centers horizontally but not vertically.目前它只是水平居中而不是垂直居中。

And I dont know why.我不知道为什么。 Here is the code I am using:这是我正在使用的代码:

for (XWPFParagraph p : docx.getParagraphs()) {
        System.out.println(p.getText());

        boolean setBold = false;
        boolean setItalic = false;
        int fontSizeToSet = 16;

        if (counter == 0) {
            p.setAlignment(ParagraphAlignment.CENTER);
            p.setVerticalAlignment(TextAlignment.CENTER);
        }

        if (counter == 1) {
            p.setPageBreak(true);
        }

        List<XWPFRun> runs = p.getRuns();
        if (runs != null) {
            for (XWPFRun r : runs) {
                if (counter == 0) {
                    r.setFontSize(14);
                    r.setBold(true);
                    r.setFontFamily("Bookman Old Style");
                }
                
            }
        }

        counter++;
    }

What am I doing wrong?我究竟做错了什么?

XWPFParagraph.setVerticalAlignment is not made for vertical aligning a paragraph on the page. XWPFParagraph.setVerticalAlignment不适用于垂直对齐页面上的段落。 It sets the vertical alignment within the text line.它在文本行中设置垂直 alignment。 This is similar to what vertical-align does in CSS. It only takes effect if the text line is higher than the single elements in that line.这类似于vertical-align在 CSS 中的作用。它仅在文本行高于该行中的单个元素时才生效。 For example if there is text having various font sizes in one text line.例如,如果在一个文本行中存在具有各种字体大小的文本。

TextAlignment has following enum constants: TextAlignment具有以下枚举常量:

AUTO
Specifies that all text in the parent object shall be aligned automatically when displayed.指定父 object 中的所有文本在显示时应自动对齐。

BASELINE
Specifies that all text in the parent object shall be aligned to the baseline of each character when displayed.指定父 object 中的所有文本在显示时应与每个字符的基线对齐。

BOTTOM
Specifies that all text in the parent object shall be aligned to the bottom of each character when displayed.指定父 object 中的所有文本在显示时应与每个字符的底部对齐。

CENTER
Specifies that all text in the parent object shall be aligned to the center of each character when displayed.指定父 object 中的所有文本在显示时应与每个字符的中心对齐。

TOP
Specifies that all text in the parent object shall be aligned to the top of each character when displayed.指定父 object 中的所有文本在显示时应与每个字符的顶部对齐。

The following complete code sample shows the effect of the different text alignment settings.以下完整代码示例显示了不同文本 alignment 设置的效果。

To vertically center a paragraph (or more) on the page, those paragraphs must be on a single page.要在页面上垂直居中放置一个(或多个)段落,这些段落必须位于单个页面上。 And there must be section properties set for this page.并且必须为此页面设置部分属性。 In section properties one then can set VAlign for the section above.在部分属性中,然后可以为上面的部分设置VAlign

Unfortunately does apche poi not provide setting section properties up to now.不幸的是, apche poi到目前为止还没有提供设置部分属性。 So the low level org.openxmlformats.schemas.wordprocessingml.x2006.main.* classes must be used to achieve the same.因此必须使用低级org.openxmlformats.schemas.wordprocessingml.x2006.main.*类来实现相同的目的。

The folowing complete example also shows this.下面的完整示例也显示了这一点。 It puts a paragraph with section break next page for section above.它在上一节的下一页放置一个带有分节符的段落。 So the first paragraph is on it's own page.所以第一段在它自己的页面上。 Then it sets page vertical align to center for page above (section above).然后它将页面垂直对齐设置为上面页面的中心(上面的部分)。

import java.io.*;

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

public class CreateWordParagraphAndPageAlignment {
    
 static void createSomeRichTextContent(XWPFParagraph paragraph) {
  XWPFRun run = paragraph.createRun();  
  run.setText("Aligned ");
  run.setFontSize(11);
  run = paragraph.createRun();  
  run.setText("paragraph ");
  run.setFontSize(22);
  run = paragraph.createRun();  
  run.setText("having ");
  run.setFontSize(33);
  run = paragraph.createRun();  
  run.setText("various ");
  run.setFontSize(22);
  run = paragraph.createRun();  
  run.setText("font sizes");
  run.setFontSize(11);
 }

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

  XWPFDocument document = new XWPFDocument();
  
  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run = paragraph.createRun();  
  run.setText("Default paragraph in first page, which has page vertical alignment set.");
  run.setFontSize(44);
  paragraph = document.createParagraph();

  //paragraph with section break next page for section above
  paragraph = document.createParagraph();
  org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr ctSectPr = paragraph.getCTP().addNewPPr().addNewSectPr();
  ctSectPr.addNewType().setVal(org.openxmlformats.schemas.wordprocessingml.x2006.main.STSectionMark.NEXT_PAGE);
  //set page vertical align center for page above (section above)
  ctSectPr.addNewVAlign().setVal(org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc.CENTER);
  //page size setting (A4) for the section above
  org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageSz ctPageSz = ctSectPr.addNewPgSz();
  ctPageSz.setW(java.math.BigInteger.valueOf(Math.round(8.27d*72d*20d))); //A4 = 8.27" * 72 * 20 = Twips
  ctPageSz.setH(java.math.BigInteger.valueOf(Math.round(11.69d*72d*20d))); //A4 = 11.69" * 72 * 20 = Twips


  paragraph = document.createParagraph();
  run = paragraph.createRun();  
  run.setText("Default paragraph");

  paragraph = document.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.CENTER);
  paragraph.setVerticalAlignment(TextAlignment.AUTO);
  createSomeRichTextContent(paragraph);

  paragraph = document.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.CENTER);
  paragraph.setVerticalAlignment(TextAlignment.BASELINE);
  createSomeRichTextContent(paragraph);
  
  paragraph = document.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.CENTER);
  paragraph.setVerticalAlignment(TextAlignment.BOTTOM);
  createSomeRichTextContent(paragraph);

  paragraph = document.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.CENTER);
  paragraph.setVerticalAlignment(TextAlignment.CENTER);
  createSomeRichTextContent(paragraph);
  
  paragraph = document.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.CENTER);
  paragraph.setVerticalAlignment(TextAlignment.TOP);
  createSomeRichTextContent(paragraph);
  
  paragraph = document.createParagraph();
  
  // page size setting (A4) for the last section above must be at last in body
  ctSectPr = document.getDocument().getBody().addNewSectPr();
  ctPageSz = ctSectPr.addNewPgSz();
  ctPageSz.setW(java.math.BigInteger.valueOf(Math.round(8.27d*72d*20d))); //A4 = 8.27" * 72 * 20 = Twips
  ctPageSz.setH(java.math.BigInteger.valueOf(Math.round(11.69d*72d*20d))); //A4 = 11.69" * 72 * 20 = Twips

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

 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM