【问题标题】:Horizontaly align Paragraph in iText水平对齐 iText 中的段落
【发布时间】:2019-06-02 22:52:37
【问题描述】:

我正在尝试将段落的对齐方式设置为右对齐。

这是我试过的,没有任何结果:

Paragraph paragraph1 = new Paragraph(str1);
paragraph1.setHorizontalAlignment(HorizontalAlignment.RIGHT);
document.add(paragraph1);

我已经找到了很多examples,但它们似乎都处理表格中的单元格。就我而言,它只是一个段落。

【问题讨论】:

    标签: java pdf itext


    【解决方案1】:

    请尝试 setTextAlignment 方法:

    Paragraph paragraph1 = new Paragraph(str1);
    paragraph1.setTextAlignment(TextAlignment.RIGHT);
    document.add(paragraph1);
    

    值得一提的是,setHorizo​​ntalAlignment 和 setTextAlignment 方法有不同的目标。前者是关于放置段落本身(作为一个元素),而后者是关于放置其内容。

    要查看差异,可以在段落上设置宽度并将该段落添加到 div 元素。

    我创建了一个小测试来演示它:

        PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
        Document document = new Document(pdfDocument);
    
        Div div = new Div()
                .setWidth(500)
                .setBackgroundColor(ColorConstants.YELLOW);
    
        Paragraph paragraph = new Paragraph("Hello World!")
                .setTextAlignment(TextAlignment.CENTER)
                .setHorizontalAlignment(HorizontalAlignment.RIGHT)
                .setWidth(300)
                .setBackgroundColor(ColorConstants.BLUE);
    
        div.add(paragraph);
        document.add(div);
    

    如您所见,这里我创建了一个段落,其中水平对齐设置为 RIGHT,文本对齐设置为 CENTER。

    结果如下:

    【讨论】:

      猜你喜欢
      • 2017-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多