Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align Paragraph at the center of the page

Tags:

java

pdf

itext

I am using itext to generate pdf file. I want to align my title in the middle of the page. Presently i am using like this

Paragraph preface = new Paragraph();  
for (int i = 0; i < 10; i++) {
    preface.add(new Paragraph(" "));
}

Is it correct or is there any another best way to do this.

like image 521
PSR Avatar asked Jan 17 '13 06:01

PSR


People also ask

How do I align text to the center of a page?

1 Select the text you want to center between the top and bottom margins. 2 On the Page Layout tab, click the Page Setup Dialog Box Launcher. 3 Select the Layout tab. 4 In the Vertical alignment box, click Center 5 In the Apply to box, click Selected text, and then click OK.

How do you center align paragraphs in HTML?

To set text alignment in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <p> tag, with the CSS property text-align for the center, left and right alignment.

How do you center align a page in HTML?

How To Center Your Website. Use a container element and set a specific max-width . A common width many websites use is 960px. To actually center the page, add margin: auto .


2 Answers

Use Paragraph#setAlignment(int) :

Paragraph preface = new Paragraph(); 
preface.setAlignment(Element.ALIGN_CENTER);

See the ALIGN_* constants in the Element interface for more possible values.

like image 194
Alexis Pigeon Avatar answered Oct 14 '22 02:10

Alexis Pigeon


Not sure if this is an old version, but for PdfWriter these methods weren't there. Instead I used:

Paragraph p = new Paragraph("This too shall pass");
p.Alignment = Element.ALIGN_CENTER;
like image 6
Andy Brown Avatar answered Oct 14 '22 00:10

Andy Brown