Itext pdf - 将文本对齐到右侧

3

我正在使用Itext PDF API生成PDF文件。我想让一些文本靠右对齐。我尝试了手动设置间距的方法,但出现了问题(下面是代码)。如果有动态实现的方法那就太好了,谢谢!

String dest = "\\location\\";
    PdfWriter writer;

        writer = new PdfWriter(dest);

        // Creating a PdfDcoument
        PdfDocument pdf = new PdfDocument(writer);

        // Creating a Document
        Document document = new Document(pdf);

        // Creating a String
        String para1 = "TEXT";

        //Spacing length
        while (para1.length() < 50) {

              para1 = " " + para1;

            }


        //Creating Paragraphs
        Paragraph paragraph1 = new Paragraph(para1);

        //paragraph1.setAlignment(Element.ALIGN_CENTER);

        //Adding Paragraphs to document
        document.add(paragraph1);

        // Closing the document
        document.close();

提前感谢您!

2个回答

6

itext7中的com.itextpdf.layout.element.Paragraph类有一个setTextAlignment方法。我希望这是您要寻找的内容:

...                 
            paragraph1.setTextAlignment(TextAlignment.RIGHT);    
...

确实。对于这个奇怪(而且非常琐碎)的问题,这是一个好答案。 - Bruno Lowagie
谢谢 mistyk。+1。@BrunoLowagie,我真的不明白这个问题有多奇怪或琐碎。不过还是感谢您的评论... - Tiger

2

我正在使用com.itextpdf:itextpdf:5.5.10,看起来相关内容已经有所改动。

    paragraph1.setAlignment(com.itextpdf.text.Element.ALIGN_RIGHT)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接