texto pagina numero justificar headerfooter examples alinear align java pdf itext

java - pagina - Alinear párrafo en el centro de la página.



justificar texto itext java (4)

Estoy usando itext para generar el archivo pdf. Quiero alinear mi título en el medio de la página. Actualmente estoy usando como este

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

¿Es correcto o hay alguna otra manera mejor de hacer esto?


No estoy seguro si esta es una versión antigua, pero para PdfWriter estos métodos no estaban allí. En su lugar usé:

Paragraph p = new Paragraph("This too shall pass"); p.Alignment = Element.ALIGN_CENTER;


Si alguien está buscando la versión .NET / C #, a continuación se muestra cómo logré la alineación del CENTRO.

Estoy usando la biblioteca iText7 para .NET / C #, y logré esto usando:

Paragraph preface = new Paragraph(); preface.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);


Use Paragraph#setAlignment(int) :

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

Vea las constantes ALIGN_* en la interfaz del Element para obtener más valores posibles.


public static final String DEST = "results/tables/centered_text.pdf"; public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new CenteredTextInCell().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); Font font = new Font(FontFamily.HELVETICA, 12, Font.BOLD); Paragraph para = new Paragraph("Test", font); para.setLeading(0, 1); PdfPTable table = new PdfPTable(1); table.setWidthPercentage(100); PdfPCell cell = new PdfPCell(); cell.setMinimumHeight(50); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.addElement(para); table.addCell(cell); document.add(table); document.close(); }