简体   繁体   中英

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.

I am using org.apache.poi version 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. It sets the vertical alignment within the text line. 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. For example if there is text having various font sizes in one text line.

TextAlignment has following enum constants:

AUTO
Specifies that all text in the parent object shall be aligned automatically when displayed.

BASELINE
Specifies that all text in the parent object shall be aligned to the baseline of each character when displayed.

BOTTOM
Specifies that all text in the parent object shall be aligned to the bottom of each character when displayed.

CENTER
Specifies that all text in the parent object shall be aligned to the center of each character when displayed.

TOP
Specifies that all text in the parent object shall be aligned to the top of each character when displayed.

The following complete code sample shows the effect of the different text alignment settings.

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.

Unfortunately does apche poi not provide setting section properties up to now. So the low level org.openxmlformats.schemas.wordprocessingml.x2006.main.* classes must be used to achieve the same.

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();

 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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