简体   繁体   中英

Special alignment using itext

how can i get this alignment on 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. You can create a 1 row 3 column table and putting your text in the middle column with a 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.

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

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

} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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