Align Paragraph at the center of the page

后端 未结 7 1355
执笔经年
执笔经年 2021-01-03 21:03

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


        
相关标签:
7条回答
  • 2021-01-03 21:20
     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();
    }
    
    0 讨论(0)
  • 2021-01-03 21:22

    I have searching solution for this to align PdfPCell text into right and also center. After modify and change the code sequence it is work.

    This code is not work to align text to center.

                  PdfPCell cell = new PdfPCell();
                  cell.addElement(new Phrase("Testing Page");
                  cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                  table.addCell(cell);
    

    After modifying code with this , it is working now.

                  Paragraph p = new Paragraph("Testing Page");
                  //Pass Paragraph object into PdfPCell 
                  PdfPCell cell = new PdfPCell(p);
                  cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                  table.addCell(cell);
    
    0 讨论(0)
  • 2021-01-03 21:29

    If you are looking for a solution to Itext7 then you can use the method setTextAlignment(...).

    Example:

    Paragraph preface = new Paragraph();
    // add text
    preface.setTextAlignment(TextAlignment.CENTER);
    
    0 讨论(0)
  • 2021-01-03 21:35

    If any one is looking for .NET/C# version, below is how I achieved the CENTER alignment.

    I am using iText7 library for .NET/C#, and I achieved this using :

    Paragraph preface = new Paragraph();
    preface.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
    
    0 讨论(0)
  • 2021-01-03 21:36

    This is what worked for me (itext 5.0.5 ):

    Paragraph p3= new Paragraph("Hello" );
      p3.setAlignment(Element.ALIGN_CENTER);
    
    0 讨论(0)
  • 2021-01-03 21:38

    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;
    
    0 讨论(0)
提交回复
热议问题