简体   繁体   English

使用itext的特殊对齐

[英]Special alignment using itext

how can i get this alignment on itext : 我如何在itext上获得此对齐方式:

From this lines : 从这行:

itext
I use java, itext to write pdf docs
java

Can i get this : 我能得到这个吗?

                        itext
                        I use java, itext to write pdf docs
                        java

The second line is centered. 第二行居中。

What you want is not so easy to be obtained with iText but this can be a possible way to do it. 使用iText很难获得想要的东西,但这可能是实现它的一种可能方式。 You can create a 1 row 3 column table and putting your text in the middle column with a ALIGN_LEFT . 您可以创建1行3列的表格,并使用ALIGN_LEFT将文本放在中间的列中。 The only problem with this solution is that column's width in iText must be set manually, so you have to compute at run time the needed width of the middle cell to obtain your desired output. 此解决方案的唯一问题是,必须手动设置iText中列的宽度,因此您必须在运行时计算中间单元格所需的宽度,以获取所需的输出。

Here an example: 这里是一个例子:

String[] rows = {"line 1", "line 222222222222222222", "line 3", "line 4 QWERTOPASDFVBNM"};

Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("C:/SimplePDF.pdf"));
document.open();

document.newPage();

// Creating PdfPTable with 3 cell [Empty][Your Test][Empty]
PdfPTable table = new PdfPTable(3);
PdfPCell fake = new PdfPCell();

fake.setBorder(Rectangle.NO_BORDER); // Hiding Border
table.addCell(fake);

// Creating middle cell
PdfPCell c = new PdfPCell();
c.setHorizontalAlignment(Element.ALIGN_LEFT);
c.setBorder(Rectangle.NO_BORDER);

// Adding strings to the middle cell
for (String string : rows) {
    c.addElement(new Paragraph(string));
}

table.addCell(c);

table.addCell(fake);

// Setting manually column widths
// Depending on String length added before, you should get the
// max length string and compute the width for the middle cell, 
// then the others 2 are just (100% - middle_cell_width)/2
float[] columnWidths = {30f, 40f, 30f};
table.setWidths(columnWidths);

document.add(table);
document.close();

Here how it will be displayed: 这里将如何显示它:

输出截图

you can set tab setting in itext, please refer this link itext-tab 您可以在itext中设置标签设置,请参考此链接itext-tab

this code will work and you can customise the tab width 此代码将起作用,您可以自定义标签宽度

public class TabSpacing {

    public static final String DEST = "/home/sunil/Desktop/tab.pdf";

    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new TabSpacing().createPdf(DEST);
    }

    public void createPdf(String dest) throws FileNotFoundException, DocumentException {
        Document document = new Document();

        PdfWriter.getInstance(document, new FileOutputStream(dest));

        document.open();

        Paragraph p = new Paragraph();
        p.setTabSettings(new TabSettings(200f));
        p.add(Chunk.TABBING);
        p.add(new Chunk("itext"));
        document.add(p);

        p = new Paragraph();
        p.setTabSettings(new TabSettings(200f));
        p.add(Chunk.TABBING);
        p.add(new Chunk("I use java, itext to write pdf docs"));

        document.add(p);

        p = new Paragraph();
        p.setTabSettings(new TabSettings(200f));
        p.add(Chunk.TABBING);
        p.add(new Chunk("java"));
        document.add(p);

        document.close();
    }

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM