Align a page paragraph vertically center or bottom

Hi,
I need to add a new page after special word ("|attachment| is the word in sample document) and need to add a content which should align vertically and horizontally center. I am tracking the special word with find and replace method and created the document builder. Attaching the sample file test (2).docx (37.4 KB)
I am tried below code,

builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
Section currentSection = builder.getCurrentSection();
PageSetup pageSetup = currentSection.getPageSetup();

builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
pageSetup.setVerticalAlignment(PageVerticalAlignment.CENTER);

In normal file it working fine. But if the first page of uploaded word has cover page. then the newly created page is adapting the cover page(first page) style and not getting the page header and footer. Attaching the output output (3).docx (37.1 KB)
Please help me to sort this issue.

Thank you

@Gptrnt The problem occurs because your document has different headers and footers for first page and when you insert section break the newly created page is the first page in the newly created section and it shows the first page header. Also there are page borders applied only to the first page of section. There are several options to resolve the problem:

  1. Disable DifferentFirstPageHeaderFooter and reset borders for the newly created section, like shown in the following code:
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
Section currentSection = builder.getCurrentSection();
PageSetup pageSetup = currentSection.getPageSetup();

// Disable inheriting the first page header/footer from previous section.
pageSetup.setDifferentFirstPageHeaderFooter(false);
// Reset page borders.
pageSetup.getBorders().clearFormatting();

builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
pageSetup.setVerticalAlignment(PageVerticalAlignment.CENTER);
  1. Insert the centered text as a shape. In this case you can use page break instead of section break:
builder.insertBreak(BreakType.PAGE_BREAK);

Shape textShape = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
textShape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
textShape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
textShape.setWrapType(WrapType.NONE);
textShape.isLayoutInCell(false); // Display the shape outside of table cell if it will be placed into a cell.

textShape.setWidth(300);
textShape.setHeight(70);
textShape.setHorizontalAlignment(HorizontalAlignment.CENTER);
textShape.setVerticalAlignment(VerticalAlignment.CENTER);


textShape.getFill().setColor(Color.RED);
textShape.setStrokeColor(Color.RED);

textShape.getTextPath().setText("This is centered text");
textShape.getTextPath().setFontFamily("Arial");

builder.writeln();
builder.insertNode(textShape);

Hi

I have tried above solutions

First solution : - your manually clearing all the boarders. In case all the pages of uploaded template contains page boarders, then newly created page will not have any boarders.
Second solution :- While using shape if the vertical alignment is need to add as top or bottom, text is showing inside header or footer resp. I want the shape should place below header or above footer if top or bottom is added resp.

Please help me where i need to change for the solution

Thank you

@Gptrnt For the first solution you can add a condition and clear borders only if they are applied to the first page:

 // Disable inheriting the first page header/footer from previous section.
pageSetup.setDifferentFirstPageHeaderFooter(false);
// Reset page borders.
if(pageSetup.getBorderAppliesTo() == PageBorderAppliesTo.FIRST_PAGE)
    pageSetup.getBorders().clearFormatting();
if(pageSetup.getBorderAppliesTo() == PageBorderAppliesTo.OTHER_PAGES)
    pageSetup.setBorderAppliesTo(PageBorderAppliesTo.ALL_PAGES);

For the second solution. You can use margin relative position in this case:

textShape.setRelativeHorizontalPosition(RelativeHorizontalPosition.MARGIN);
textShape.setRelativeVerticalPosition(RelativeVerticalPosition.MARGIN);

Hi,
I am using the first solution and the paragraph is aligned vertically center or bottom accordingly. But paragraph in the next page also aligned on that way, which is an issue. So I tried to add an extra section-break after the vertical aligned paragraph. it fixing the alignment issue but creating an extra page break(because I am adding a page break in the starting. and can’t remove it).So can you please help me to sort this issue.

Thank you

@Gptrnt In this case you can use continuous section break instead of new page section break. See BreakType.SECTION_BREAK_CONTINUOUS. This section break does not produce page break.

Hi,

While I am using section break, only to put the paragraph at vertically center or bottom, it creating other issues like page number is restarting in every section. Then I have tried with text shape as mentioned in the above code. But which creating font size issue as my font size is dynamic. It is not taking the font size mentioning like below code
textShape.getTextPath().setSize(fontSize);
instead taking the height of the shape. Also, if the text is bigger it is not coming in next line, instead all text shrink and showing in one line. Please help me to solve it.

Thank you

@Gptrnt

Page numbering is restarted for each section because PageSetup.RestartPageNumbering option is set. You can reset this option to continue numbering.

Regarding the shape text. In your case you can try using text box instead of WordArt shape. For example see the following code:

builder.insertBreak(BreakType.PAGE_BREAK);

Shape textShape = new Shape(doc, ShapeType.TEXT_BOX);
textShape.setStroked(false);
textShape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
textShape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
textShape.setWrapType(WrapType.NONE);
textShape.isLayoutInCell(false); // Display the shape outside of table cell if it will be placed into a cell.

textShape.setWidth(300);
textShape.setHeight(300);
textShape.setHorizontalAlignment(HorizontalAlignment.CENTER);
textShape.setVerticalAlignment(VerticalAlignment.CENTER);

Paragraph textPara = new Paragraph(doc);
textPara.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
Run textRun = new Run(doc, "This is centered text");
textRun.getFont().setSize(20);
textPara.appendChild(textRun);

textShape.appendChild(textPara);

builder.writeln();
builder.insertNode(textShape);

doc.save("C:\\Temp\\out.docx");